使用 Hibernate 在 CollectionTable 的列上创建索引
假设我有以下实体,它对订阅者进行建模并使用 CollectionTable
来对订阅列表进行建模,如下所示:
@Entity
@Table(name = "SUBSCRIBER")
public class Subscriber {
@ElementCollection
@CollectionTable(name = "PERSON_ORG_SUBSCRIPTIONS",
joinColumns = { @JoinColumn( name = "PERSON_ID", referencedColumnName = "PERSON_ID" ),
@JoinColumn( name = "ORG_ID", referencedColumnName = "ORG_ID" ) } )
@Column(name = "SUBSCRIPTION_NAME")
protected Set<String> _subscriptionNames;
}
因此,这将创建一个包含 PERSON_ID
、列的表ORG_ID
和 SUBSCRIPTION_NAME
。
我正在尝试在 SUBSCRIPTION_NAME
列上创建数据库索引。但是,如果我在 _subscriptionNames
上添加以下注释:
@org.hibernate.annotations.Index( name="subscription_idx", columnNames={"SUBSCRIPTION_NAMES"} )
我会得到一个异常:
org.hibernate.MappingException: Unable to find logical column name from physical name null in table SUBSCRIBER
我还尝试在 Subscriber
上使用 org.hibernate.annotations.Table
注释code> 实体,但似乎没有办法让它引用 PERSON_ORG_SUBSCRIPTIONS 表。
我正在使用 Hibernate 3.5.3 和 PostgreSQL 9.0。
Suppose I have the following entity which models a subscriber and uses a CollectionTable
to model a list of subscriptions like so:
@Entity
@Table(name = "SUBSCRIBER")
public class Subscriber {
@ElementCollection
@CollectionTable(name = "PERSON_ORG_SUBSCRIPTIONS",
joinColumns = { @JoinColumn( name = "PERSON_ID", referencedColumnName = "PERSON_ID" ),
@JoinColumn( name = "ORG_ID", referencedColumnName = "ORG_ID" ) } )
@Column(name = "SUBSCRIPTION_NAME")
protected Set<String> _subscriptionNames;
}
So this creates a table with columns for PERSON_ID
, ORG_ID
and SUBSCRIPTION_NAME
.
I'm trying to create a database index on the SUBSCRIPTION_NAME
column. But if I put the following annotation on _subscriptionNames
:
@org.hibernate.annotations.Index( name="subscription_idx", columnNames={"SUBSCRIPTION_NAMES"} )
I get an exception:
org.hibernate.MappingException: Unable to find logical column name from physical name null in table SUBSCRIBER
I also tried using the org.hibernate.annotations.Table
annotation on the Subscriber
entity, but there does not seem to be a way to have it reference the PERSON_ORG_SUBSCRIPTIONS
table.
I'm using Hibernate 3.5.3 and PostgreSQL 9.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SUBSCRIBER 表中是否存在名为“SUBSCRIPTION_NAME”的列?
您打算通过代码在表上创建索引吗?您应该正确使用 hibernate.hbm2ddl.auto=create
Is the column with name "SUBSCRIPTION_NAME" present in the table SUBSCRIBER?
Are you planning to create index on table from the code? You should propably use hibernate.hbm2ddl.auto=create