使休眠下降表级联 [英] Make Hibernate Drop Table Cascade
问题描述
我具有hibernate.hbm2ddl.auto=create-drop
设置,但是在hibernate使用hibernate.import_files=db/create.sql
生成数据库之后,我也基于SQL中的一个表创建视图.
I have the hibernate.hbm2ddl.auto=create-drop
setting but I also create a view based on one of the tables in SQL after hibernate generates the database using hibernate.import_files=db/create.sql
.
下次运行时,休眠将无法删除现有表,因为视图依赖该表.
The next time it runs hibernate cannot drop the existing table because the view depends on it.
如何使休眠放置表级联,以使视图也被放置?
How do I make hibernate drop table cascade so that the view gets dropped as well?
像这样定义实体似乎没有效果
Defining the entity like this doesn't seem to have an effect
@Entity
@OnDelete(action = OnDeleteAction.CASCADE)
@Table(name = "MY_TABLE")
推荐答案
我遇到了完全相同的问题-视图定义(具有相应的JPA映射)阻止了其他表的删除.
I had exactly the same issue - a view definition (with corresponding JPA mapping) that blocked other table to be dropped.
最适合我的解决方案是使用 javax.persistence.* 属性,该属性可让您更好地控制创建/删除脚本.因此,我使用的是javax.persistence.schema-generation.database.action
而不是普通的hibernate.hbm2ddl.auto
.这样,我可以先删除视图,然后再引用表.
The solution that works for me was to use javax.persistence.* properties that gives you more control over create/drop scripts. So instead of using plain hibernate.hbm2ddl.auto
I've used javax.persistence.schema-generation.database.action
. That way I was able to first drop the view and then referenced table.
通过使用<property name="javax.persistence.schema-generation.create-source" value="script-then-metadata"/>
,您可以将用户定义的脚本与架构自动创建结合起来.
By using <property name="javax.persistence.schema-generation.create-source" value="script-then-metadata"/>
you can combine user-defined script with schema auto-creation.
您可以在此处了解有关这些属性的更多信息: https://docs.oracle.com/javaee/7/tutorial/persistence-intro005.htm
You can read more about these properties here: https://docs.oracle.com/javaee/7/tutorial/persistence-intro005.htm
这篇关于使休眠下降表级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!