3

请考虑以下两个实体:

@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 会在连接表上创建一个复合索引。是什么赋予了?

4

1 回答 1

2

将类型从 更改ListSet成功了:

改变:

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

至:

private Set<Profile> taggedProfiles = new HashSet<Profile>();
于 2012-06-01T09:12:01.073 回答