我想将一个实体映射到数据库上现有的PostgreSQL视图。本文解释了如何使用Hibernate实现这一点,它看起来非常简单:将@Immutable
添加到实体中,并确保sur-@Table
名称与视图名称匹配。
所以,这是我在Panache的实体:
@Entity
@Immutable
@Table(name = "test_view")
public class TestView extends PanacheEntityBase {
@Id
public Long id;
@ManyToOne
@JoinColumn(name = "other_test_view_id")
public OtherTestView otherTestView;
}
这确实有效,而且我可以毫无问题地获取视图数据。但Panache似乎忽略了@Immutable
注释,因为如果我用这个参数启动Quarkus应用程序:
quarkus.hibernate-orm.database.generation=update
然后我得到这个错误:
Error executing DDL "alter table if exists test_view add constraint FKs1taqbpciifj90ycsy164ofpw foreign key (other_test_view_id) references other_test_view"
这让我认为Quarkus实际上是在把我的实体视为任何其他实体,而不是一个视图。如果它将其视为一个视图,就不应该试图更改表。
@SubSelect
注释有效,Panache在创建/更新数据库模式时忽略该表:
@Entity
@Immutable
@SubSelect(select * from test_view)
@Table(name = "test_view")
public class TestView extends PanacheEntityBase {
@Id
public Long id;
@ManyToOne
@JoinColumn(name = "other_test_view_id")
public OtherTestView otherTestView;
}