hibernate的many_to_many配置

many-to-many配置,article表与tags表之间,以articletagsrelation关系表进行关联。


article.java:
private Set<BaseTag> basetags = new HashSet<BaseTag>();

public Set<BaseTag> getBasetags() {
 return basetags;
}

public void setBasetags(Set<BaseTag> basetags) {
 this.basetags = basetags;
}

article.hbm.xml:
<set name="basetags" table="articletagsrelation" lazy="true" cascade="save-update" >
  <key>
    <column name="AID" length="10" not-null="true"/>
  </key>
  <many-to-many class="gooweb.model.BaseTag">
    <column name="TID" length="10" not-null="true"/>
  </many-to-many>
</set>


tags.java:
private Set<Article> articles = new HashSet<Article>();

public Set<Article> getArticles() {
 return articles;
}

public void setArticles(Set<Article> articles) {
 this.articles = articles;
}

tags.hbm.xml:
<set name="articles" cascade="save-update" inverse="true"  table="articletagsrelation">
  <key>
    <column name="AID" length="10" not-null="true" unique="true"/>
  </key>
  <many-to-many class="gooweb.model.Article">
     <column name="TID" length="20" not-null="true"/>
  </many-to-many>
</set>

 

你可能感兴趣的:(xml,Hibernate)