休眠 hbm2ddl.auto create/update 跳过查看

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 动态创建我的数据库设置。因为我没有将实体 table 标记为视图直接休眠尝试创建数据库。

我可以通过使用 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 跳过创建特定的 table dispite 吗?

提前致谢!

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

至于文档@Subselect

Map an immutable and read-only entity to a given SQL select expression.