开发者

Create an index on a column of a CollectionTable using Hibernate

开发者 https://www.devze.com 2023-01-30 04:34 出处:网络
Suppose I have the following entity which models a subscriber and uses a CollectionTable to model a list of subscriptions like so:

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, b开发者_开发问答ut 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消