Rhythmk 学习 Hibernate 06 - Hibernate 表间关系 [One To One]

1、One To One 单相

 背景:

      古代一个老婆  只能关联一个老公

husband.java

package com.rhythmk.model;

public class husband {
	
	public Integer getHusbandId() {
		return husbandId;
	}
	public void setHusbandId(Integer husbandId) {
		this.husbandId = husbandId;
	}
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	private Integer husbandId;
	private String Name;

}

  hasband.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping  package="com.rhythmk.model">
    <class name="husband" table="t_husband">
        <id name="husbandId" type="int">
            <column name="husbandId" />
            <generator class="native" />
        </id>
        <property name="Name" type="string">
        
        </property>
    
     
    </class>
</hibernate-mapping>

 

wife.java

package com.rhythmk.model;

public class wife {

	private Integer wifeId;
	private String name;

	public Integer getWifeId() {
		return wifeId;
	}

	public void setWifeId(Integer wifeId) {
		this.wifeId = wifeId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public husband getHb() {
		return hb;
	}

	public void setHb(husband hb) {
		this.hb = hb;
	}

	private husband  hb;

	@Override
	public String toString() {
		return "wife [wifeId=" + wifeId + ", name=" + name + ", hb=" + hb + "]";
	}
	
}

  wife.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping  package="com.rhythmk.model">
    <class name="wife" table="t_wife">
        <id name="wifeId" type="int">
            <column name="wifeId" />
            <generator class="native" />
        </id>
        <property name="Name" type="string">
        </property>
  <!--   one  to  one 类似于  one to many   只需要添加 unique =true 即可 -->
      <many-to-one name="hb"  column="husbandId"  ></many-to-one>
    </class>
</hibernate-mapping>

注意:

    one to one 类似于 one to many 只需要添加 unique =true 即可

 2、One To One 双向

      跟单相查不多  就是两边都配上  many to one

调整 hasband.hbm.xml 添加:

<!--   one  to  one 类似于  one to many   只需要添加 unique =true 即可 -->
      <many-to-one name="mywife"  column="wifeId"  ></many-to-one>

调整 husband.java :

public wife getMywife() {
		return mywife;
	}
	public void setMywife(wife mywife) {
		this.mywife = mywife;
	}
	private wife mywife;

  测试:

@Test
	public void test02_add() {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();

			husband hb = new husband();
			hb.setName("王大1");
			wife w = new wife();
			w.setName("张妞 2");
			w.setHb(hb);
			hb.setMywife(w);
			session.save(hb);
			session.save(w);
			System.out.println(w.toString());
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}
	}

  输出:

Hibernate: insert into t_husband (Name, wifeId) values (?, ?)
Hibernate: insert into t_wife (Name, husbandId) values (?, ?)
wife [wifeId=6, name=张妞 2, hb=com.rhythmk.model.husband@8b677f]
Hibernate: update t_husband set Name=?, wifeId=? where husbandId=?

 

 

代码下载地址:

http://pan.baidu.com/s/1gdFt7ur

转载于:https://my.oschina.net/u/926126/blog/302381

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值