1.对象只用2个,采用many-to-many标签来处理
关于单双向关联,只需要在某一方设置<set>即可,切双方的属性需要一直
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lohamce.hibernate">
<class name="Killer" table="t_killer">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<set name="specialized" table="t_killer_weapon">
<key column="killerId" />
<many-to-many class="Weapon" column="weaponId"/>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lohamce.hibernate">
<class name="Weapon" table="t_weapon">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<set name="killers" table="t_killer_weapon">
<key column="weaponId" />
<many-to-many class="Killer" column="killerId"/>
</set>
</class>
</hibernate-mapping>
@Test
public void testSaveKillerWeapon(){
Session session = null;
Transaction tx = null;
try{
session = HibernateUtil.openSession();
tx = session.beginTransaction();
Killer killer = new Killer();
killer.setName("BHO");
Killer killer2 = new Killer();
killer2.setName("Devil Angel");
Weapon w = new Weapon();
w.setName("AK47");
Weapon w2 = new Weapon();
w2.setName("M4");
Set<Weapon> weapons = new HashSet<Weapon>();
weapons.add(w);
weapons.add(w2);
killer.setSpecialized(weapons);
Set<Weapon> weapons2 = new HashSet<Weapon>();
weapons2.add(w);
killer2.setSpecialized(weapons2);
session.save(w);
session.save(w2);
session.save(killer);
session.save(killer2);
tx.commit();
} catch (Exception e){
e.printStackTrace();
tx.rollback();
} finally {
HibernateUtil.close(session);
}
}
@Test
public void testLoadKillerWeapon(){
Session session = null;
Transaction tx = null;
try{
session = HibernateUtil.openSession();
tx = session.beginTransaction();
Killer s = (Killer)session.load(Killer.class, 1L);
System.out.println(s.getName());
Set<Weapon> weapons = s.getSpecialized();
for(Weapon w : weapons){
System.out.println(w.getName());
}
tx.commit();
} catch (Exception e){
e.printStackTrace();
if(tx != null) tx.rollback();
} finally {
HibernateUtil.close(session);
}
}
@Test
public void testLoadWeaponKiller(){
Session session = null;
Transaction tx = null;
try{
session = HibernateUtil.openSession();
tx = session.beginTransaction();
Weapon w = (Weapon)session.load(Weapon.class, 1L);
System.out.println(w.getName());
Set<Killer> killers = w.getKillers();
for(Killer k : killers){
System.out.println(k.getName());
}
tx.commit();
} catch (Exception e){
e.printStackTrace();
if(tx != null) tx.rollback();
} finally {
HibernateUtil.close(session);
}
}
2.对象使用3个,采用many-to-one标签
//中间对象
public class Participant {
private Long id;
private Event event;
private Customer customer;
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lohamce.hibernate">
<class name="Participant" table="t_participant">
<id name="id">
<generator class="native" />
</id>
<many-to-one name="event" column="eventId" />
<many-to-one name="customer" column="customerId" />
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lohamce.hibernate">
<class name="Customer" table="t_customer">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lohamce.hibernate">
<class name="Event" table="t_event">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
</class>
</hibernate-mapping>