多对多关系一般指两个类都拥有对方集合的成员变量,比如说文章类Article和标签类Tag,一个Arirtle类可以拥有多个Tag,一个Tag也适用于多篇文章,它们的类图如下:
它们也分别对应数据库中的实体表Articles和Tags,当然仅靠这两个表实现多对多能力是有限的,我们还需要第三个表ArticleTag的帮忙,它们的ER图如下:
实际上多对多关系并不复杂,加入一个中间表问题就迎刃而解,这和在耦合强烈的两个类之间加入一个中间类以降低耦合的思维是一致的.
下面的代码演示了如何将两个类和数据库映射起来:
Article类:
package com.sitinspring.articletag;
import java.util.HashSet;
import java.util.Set;
public class Article{
private long id;
private String name;
private Set<Tag> tags = new HashSet<Tag>();
public Article(){
}
public Article(long id,String name){
this.id=id;
this.name=name;
}
public String toString(){
String retval="Article name="+name;
for(Tag tag:tags){
retval+="\n"+tag;
}
return retval;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getTags() {
return tags;
}
public void setTags(Set tags) {
this.tags = tags;
}
}
Article.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.auction">
<class name="com.sitinspring.articletag.Article" table="ARTICLE">
<id name="id" column="ARTICLEID" />
<property name="name" column="NAME" />
<set name="tags" table="ARTICLETAG" cascade="all" lazy="false">
<key column="ARTICLEID" />
<many-to-many column="TAGID" class="com.sitinspring.articletag.Tag" />
</set>
</class>
</hibernate-mapping>
Tag类:
package com.sitinspring.articletag;
import java.util.HashSet;
import java.util.Set;
public class Tag{
private long id;
private String name;
private Set articles = new HashSet();
public Tag(){
}
public String toString(){
return "Tag name="+name;
}
public Tag(long id,String name){
this.id=id;
this.name=name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public java.lang.String getName() {
return name;
}
public void setName(java.lang.String name) {
this.name = name;
}
public Set getArticles() {
return articles;
}
public void setArticles(Set articles) {
this.articles = articles;
}
}
Tag.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.auction">
<class name="com.sitinspring.articletag.Tag" table="TAG">
<id name="id" column="TAGID" />
<property name="name" column="NAME" />
<set name="articles" table="ARTICLETAG" cascade="all" lazy="false">
<key column="TAGID" />
<many-to-many column="ARTICLEID" class="com.sitinspring.articletag.Article" />
</set>
</class>
</hibernate-mapping>
测试代码:
package com.sitinspring.articletag;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import com.sitinspring.util.HibernateUtil;
public class Main {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
// --插入代码开始
Article article = new Article(1, "论语学而");
Set tags = new HashSet();
Tag tag1 = new Tag(8, "军事");
tags.add(tag1);
Tag tag2 = new Tag(9, "政治");
tags.add(tag2);
article.setTags(tags);
session.save(article);
// --插入代码结束
// --读取代码开始
long i = 1;
Article articleLoaded = (Article) session.load(Article.class, i);
System.out.println(articleLoaded);
// --读取代码开始
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
}
}
测试结果如下:
代码下载:
http://www.blogjava.net/Files/sitinspring/HibernateClasses20071109085612.rar