Hibernate一对一关联映射

1.一对一的主键单向关联

(a)Card.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>
	<class name="com.yyming.hibernate.Card">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="idCard" />
	</class>
</hibernate-mapping>

 (b)Person.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>
      <class name="com.yyming.hibernate.Person">
	<id name="id">
		<generator class="foreign" >
			  <!-- person中的id来源于card,即共享card中的主键 -->
			   <param name="property">card</param>
		</generator>
	</id>
	<property name="name" />
	<!-- constrained="true",当前主键上存在一个约束,表明person表中的主键作为外键参照了card表 -->
	<one-to-one name="card" constrained="true"/>
       </class>
</hibernate-mapping>

 (c)Person.java和Card.java

public class Person {
	private int  id;
	private String name;
	private Card card;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Card getCard() {
		return card;
	}
	public void setCard(Card card) {
		this.card = card;
	}
}

 

public class Card {
	private int id;
	private String idCard;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getIdCard() {
		return idCard;
	}
	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}
}

 2.一对一的主键双向关联

<?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.yyming.hibernate.Card">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="idCard" />
		<one-to-one name="person"/>
	</class>
</hibernate-mapping>

 

public class Card {
	private int id;
	private String idCard;
	private Person person;
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getIdCard() {
		return idCard;
	}
	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}
}

   其它的不变。以card作为主的一方,只能进行查询person,而不能级联的改变person。

3.一对一唯一外键关联(单向关联)

    一对一唯一外键关联实际上是多对一关联映射的特例

   可以采用<many-to-one>标签,指定多的一端unique="true",即限制了多的一端的多重性为一。

    (person.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>
	<class name="com.yyming.hibernate.Person">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" />
		<many-to-one name="card" unique="true"/>
	</class>
</hibernate-mapping>

    (Card.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>
	<class name="com.yyming.hibernate.Card">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="cardNo" />
	</class>
</hibernate-mapping>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值