Hibernate学习文档_Many2Many


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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值