gpt4 book ai didi

java - Hibernate hbm2ddl.auto create/update 跳过查看

转载 作者:行者123 更新时间:2023-12-02 09:14:32 24 4
gpt4 key购买 nike

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

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

我的sql挂件就像create view my_view as select * from thisAndThat和愚蠢的逻辑;

在我的生产环境中,它就像一个魅力。现在,在我的集成测试中,我使用 Hibernate 属性 hibernate.hbm2ddl.auto = update 在 HSQL 中使用 DbUnit 动态创建数据库设置。因为我没有将实体表标记为 View ,直接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 创建特定表吗?

提前致谢!

最佳答案

在这种情况下,我将使用 @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.

关于java - Hibernate hbm2ddl.auto create/update 跳过查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59105798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com