I have a mapped entity to a database view e.g.
@Entity
@Immutable
@Table(name = "my_view")
public class MyView {
// some properties and getters
}
and my sql pendant is like create view my_view as select * from thisAndThat and stupid logic;
In my production environment it works like a charme.
Now in my integration tests I use Hibernate property hibernate.hbm2ddl.auto
= update
to create my database setup with DbUnit in HSQL on the fly. Because I did not mark the entity table as view directly hibernate tries to create the database.
I am able to fix this by using flyway and add an migration script
drop table if exists my_view;
create view my_view AS ....;
First at all: It works
BUT: While building my project I get a lot of exceptions
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)
....
which is a) a bit disturbing and b) makes me nervous this could break in (near) future.
Do you know how I can skip creating a specific table dispite using hibernate.hbm2ddl.auto
?
Thanks in advance!
In this case I would use @Subselect instead of @Table (https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/annotations/Subselect.html)
As for documentation @Subselect
Map an immutable and read-only entity to a given SQL select expression.