我有一个映射到数据库视图的实体,例如
@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创建数据库设置。因为我没有将实体表直接标记为视图,所以hibernate尝试创建数据库。
我可以使用flyway修复此问题并添加迁移脚本
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
创建的特定表吗?
谢谢!
答案 0 :(得分:1)
在这种情况下,我将使用@Subselect而不是@Table(https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/annotations/Subselect.html)
关于文档@Subselect
将不可变且只读的实体映射到给定的SQL选择 表达。