Flyway Spring JPA2集成-可以保持架构验证吗? [英] Flyway Spring JPA2 integration - possible to keep schema validation?

查看:76
本文介绍了Flyway Spring JPA2集成-可以保持架构验证吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有一个Web应用程序,正在尝试集成JPA2(Hibernate)+ Spring + Flyway

Hy, i have a webapplication where i am trying to integrate JPA2(Hibernate)+Spring+Flyway

我这样向我的ApplicationContext添加了飞路:

I added flyway to my ApplicationContext like this:

<bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate">
    <property name="baselineOnMigrate" value="true" />
    <property name="dataSource" ref="dataSource" />
</bean>

从理论上讲,这可以正常工作,并使用我保存在db/migration下的脚本更新架构.到目前为止一切顺利.

Theoretically this works fine and updates the schema with scripts that i save under db/migration. So far so good.

留给我的一个问题是,如果我更改了某些内容(例如,将String字段添加到Entity),则应用程序甚至都无法做到这一点,因为Hibernates Schema-Validator会抛出以下内容:原因:org.hibernate.HibernateException:缺少列:demo.testEntity中的showCaseField .发生这种情况是因为我已将"hibernate.hbm2ddl.auto"设置为验证"

The one problem that is left for me is that if i change something (e.g. adding a String field to an Entity) the application won't even get this far because Hibernates Schema-Validator will throw something like this: Caused by: org.hibernate.HibernateException: Missing column: showCaseField in demo.testEntity. This happens because i have set "hibernate.hbm2ddl.auto" to "validate"

现在,我已经了解到有关Hibernate在某些(罕见)情况下无法识别性能有效模式的信息,并且有一天我可能(也可能不会)达到完全禁用此功能的地步.但是到目前为止,我实际上很喜欢额外的验证,并且不想将其关闭.

Now i have read about Hibernate failing to recognize perfeclty valid schemas in some (rare?) cases and i MAY (or not) reach a point someday where i disable this feature altogether. But as of now i actually like the extra-validation and don't want to turn it off.

是否可以在保持Hibernates-Schema-Validation的同时集成Spring和Flyway?我猜这可能是个问题,因为Flyway可能依赖于DataSource-bean或某种东西,并且最终要求对applicationContext进行完全初始化,这又由于架构不匹配而使Hibernate阻止了该操作.

Is it possible to integrate Spring and Flyway while still keeping Hibernates-Schema-Validation? I guess this could be a problem, because Flyway probably depends on a DataSource-bean or something and in conclusion requires the applicationContext to be completely initialized, which in turn Hibernate prevents because of the schema mismatch..

有什么想法吗?

推荐答案

现在找到了答案.基本上,您要做的就是让您的EntityManagerFactory-bean 依赖于您的Flyway bean(有一个属性).现在,首先初始化Flyway(以及您的dataSource),并在Hibernates模式验证之前执行Flyway脚本

Found the answer now. Basically all you have to do is letting your entityManagerFactory-bean depend on your Flyway bean (there's an attribute for that). Now Flyway (and in turn your dataSource) is initialized first and the Flyway-Scripts are executed before Hibernates schema-validation

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    depends-on="flyway"> ....
</bean>
<bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate">
    <property name="baselineOnMigrate" value="true"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

这篇关于Flyway Spring JPA2集成-可以保持架构验证吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆