不使用多对多关联映射的原因是为了提高数据库的运行性能,所以采用componenet映射,这个componenet类随着主类的消失而消失,不需要建立单独的映射文件。只能由主类看到componenet的信息。
component在表中没有对应的oid,
可以关联其它持久化类
component不能单独持久化(不能单独保存)
生命周期依赖于主类的生命周期
eg:
package com.bjpowernode.hibernate; import java.util.HashSet; import java.util.Set; public class Monkey { private Integer id; private String name; //是它身体的一部分 private Set monkeyAndTeacher = new HashSet(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getMonkeyAndTeacher() { return monkeyAndTeacher; } public void setMonkeyAndTeacher(Set monkeyAndTeacher) { this.monkeyAndTeacher = monkeyAndTeacher; } }
package com.bjpowernode.hibernate; public class MonkeyAndTeacher { private Monkey monkey; private Teacher teacher; private String gongfu ; public Monkey getMonkey() { return monkey; } public void setMonkey(Monkey monkey) { this.monkey = monkey; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public String getGongfu() { return gongfu; } public void setGongfu(String gongfu) { this.gongfu = gongfu; } }
package com.bjpowernode.hibernate; import java.util.HashSet; import java.util.Set; public class Teacher { private Integer id; private String name; private Set monkeyAndTeacher = new HashSet(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getMonkeyAndTeacher() { return monkeyAndTeacher; } public void setMonkeyAndTeacher(Set monkeyAndTeacher) { this.monkeyAndTeacher = monkeyAndTeacher; } }
<?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> <class name="com.bjpowernode.hibernate.Monkey" table="t_monkey"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="monkeyAndTeacher" table="t_monkey_teacher" lazy="true"> <key column="monkey_id"/> <composite-element class="com.bjpowernode.hibernate.MonkeyAndTeacher"> <parent name="monkey"/> <many-to-one name="teacher" not-null="true" column="teacher_id" class="com.bjpowernode.hibernate.Teacher"/> <property name="gongfu" type="string" not-null="true"/> </composite-element> </set> </class> </hibernate-mapping>
<?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> <class name="com.bjpowernode.hibernate.Teacher" table="t_teacher"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="monkeyAndTeacher" table="t_monkey_teacher" lazy="true" inverse="true"> <key column="teacher_id"/> <composite-element class="com.bjpowernode.hibernate.MonkeyAndTeacher"> <parent name="teacher"/> <many-to-one name="monkey" not-null="true" column="monkey_id" class="com.bjpowernode.hibernate.Monkey"/> <property name="gongfu" type="string" not-null="true"/> </composite-element> </set> </class>
package com.bjpowernode.hibernate; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.hibernate.Hibernate; import org.hibernate.Session; import junit.framework.TestCase; public class Many2ManyTest extends TestCase { public void testSave1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); Teacher t1 = new Teacher(); t1.setName("红孩儿"); session.save(t1); Teacher t2 = new Teacher(); t2.setName("二郎神"); session.save(t2); Monkey m = new Monkey(); m.setName("猴子一"); session.save(m); MonkeyAndTeacher mt1= new MonkeyAndTeacher(); mt1.setTeacher(t1); mt1.setGongfu("三位真火"); mt1.setMonkey(m); MonkeyAndTeacher mt2= new MonkeyAndTeacher(); mt2.setTeacher(t2); mt2.setGongfu("七十三变"); mt2.setMonkey(m); m.getMonkeyAndTeacher().add(mt1); m.getMonkeyAndTeacher().add(mt2); session.save(m); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } public void testLoad1() { Session session = null; Monkey m = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); m = (Monkey)session.load(Monkey.class, 1); System.out.println(m.getName()); for (Iterator iter=m.getMonkeyAndTeacher().iterator(); iter.hasNext();) { MonkeyAndTeacher role = (MonkeyAndTeacher)iter.next(); Hibernate.initialize(role.getTeacher()); //System.out.println(role.getName()); } session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } System.out.println(m.getName()); for(Iterator i = m.getMonkeyAndTeacher().iterator();i.hasNext();){ MonkeyAndTeacher mt = (MonkeyAndTeacher)i.next(); System.out.println(mt.getTeacher().getName()); System.out.println("我的功夫是:"+ mt.getGongfu()); } } }