简体   繁体   中英

Use annotation @Index hibernate

I would like to add index(@Index) but I can't. When I add @Index on the other property(displayName) it works and create this index on DB but when I add it on "contacts" property it does not work.

@Entity
@Table(name = "TBL_PARTY")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "PARTY_TYPE", 
discriminatorType = DiscriminatorType.STRING)
@SequenceGenerator(name = "idGenerator", sequenceName = "SEQ_PARTY", 
  allocationSize = 1)
public  abstract class Party extends BaseEntity {
.....
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "PARTY_ID", nullable = false,unique = true)
@Cascade(value = {CascadeType.SAVE_UPDATE, CascadeType.DELETE})
**@Index(name = "IDX_CONTACTS")**
public List<Contact> getContacts() {
    return contacts;
}
**@Index(name="IDX_NAME)**
@Column(name = "DISPLAY_NAME")
public String getDisplayName() {
    return displayName;
}

Do you have any suggestion?

I think you should use @IndexColumn instead of @Index for getContacts() as you are using a collection return type.

You can also use the @OrderedColumn annotation which is compatible with the JPA API as specified here link . I don't know if your Contact object is embeddable so I would advise against using the @ElementCollection as it will be mapped to a collection table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM