JPA / Hibernate @ManyToMany没有在连接表上创建索引

时间:2012-05-31 21:03:01

标签: hibernate indexing many-to-many jpa-2.0 jointable

请考虑以下两个实体:

@Entity
@Table(name = "PROFILE")
public class Profile extends BaseEntity {

private Long id;
private Set<Tag> tags = new HashSet<Tag>();

    @ManyToMany(mappedBy = "taggedProfiles")
public Set<Tag> getTags() {
    return tags;
}

public void setTags(Set<Tag> tags) {
    this.tags = tags;
}
}

////////////////////////////////////////////////////////////////////////
@Entity
@Table(name = "TAG", uniqueConstraints = @UniqueConstraint(columnNames = {
    "TAG_ID", "USERS_ID" }))
public class Tag extends BaseEntity {
private Long id;

private List<Profile> taggedProfiles = new ArrayList<Profile>();

@ManyToMany
@JoinTable(
        name = "PROFILE_TAG", 
        joinColumns = @JoinColumn(name = "TAG_ID"), 
        inverseJoinColumns = @JoinColumn(name = "PROFILE_ID"), 
        uniqueConstraints = @UniqueConstraint(
                columnNames = {"PROFILE_ID", "TAG_ID" }))
// @ForeignKey(name = "FK__TAG__PROFILE", inverseName = "FK__PROFILE__TAG")
// @Index(name = "IDX__PROFILE__PROFILE_ID")
public List<Profile> getTaggedProfiles() {
    return taggedProfiles;
}

/**
 * @param taggedProfiles
 *            the taggedProfiles to set
 */
public void setTaggedProfiles(List<Profile> taggedProfiles) {
    this.taggedProfiles = taggedProfiles;
}
}

连接表PROFILE_TAG被创建得很好,但是,没有创建索引,我希望hibernate将在连接表上创建一个复合索引。是什么给了什么?

1 个答案:

答案 0 :(得分:2)

将类型从List更改为Set可以解决问题:

更改:

private List<Profile> taggedProfiles = new ArrayList<Profile>();

要:

private Set<Profile> taggedProfiles = new HashSet<Profile>();