2012-05-31 44 views
3

请考虑以下两个实体:JPA /休眠@ManyToMany联结表中没有创建索引

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

+0

更正:休眠应的两列标记为复合主键,其在转会创建一个聚集索引。 – kmansoor

回答

2

ListSet更改类型没有特技:

更改:

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

到:

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