Hibernate hbm2ddl.auto create/update skip View



>我有一个映射到数据库视图的实体,例如

@Entity
@Immutable
@Table(name = "my_view")
public class MyView {
// some properties and getters
}

我的 SQL 吊坠就像create view my_view as select * from thisAndThat and stupid logic;

在我的生产环境中,它就像一个魅力。 现在,在我的集成测试中,我使用 Hibernate 属性hibernate.hbm2ddl.auto=update在 HSQL 中使用 DbUnit 动态创建我的数据库设置。因为我没有将实体表标记为视图直接休眠尝试创建数据库。

我能够通过使用飞行路线并添加迁移脚本来解决此问题

drop table if exists my_view;
create view my_view AS ....;

首先:它有效

但是:在构建我的项目时,我遇到了很多异常

2019-11-29 14:03:43,023  WARN  ExceptionHandlerLoggedImpl:handleException:27 GenerationTarget encountered exception accepting command : Error executing DDL "alter table my_view add constraint FKj50dxtl46l99jfi90uf4df7vo foreign key (other_table_id) references fonds" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table my_view add constraint FKj50dxtl46l99jfi90uf4df7vo foreign key (other_table_id) references other_table" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applyForeignKeys(AbstractSchemaMigrator.java:433)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:249)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:315)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:615)
....

这 a( 有点令人不安,b( 让我感到紧张,这可能会在(不久的(将来破裂。

您知道如何跳过使用hibernate.hbm2ddl.auto创建特定的表吗?

提前感谢!

在这种情况下,我会使用@Subselect而不是@Table(https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/annotations/Subselect.html(

至于文档@Subselect

将不可变和只读实体映射到给定的 SQL 选择 表达。

最新更新