Hibernate学习总结:OneToOne单向外键关联

ex: 例子是一夫一妻(OneToOne单向外键关联)

一、首先是Annotation版本:

Husband.java:

package com.buaa.hibernate.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Husband {
	private int id;
	private String name;
	private Wife wife;
	
	@Id
	@GeneratedValue
	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;
	}
	@OneToOne
	@JoinColumn(name="wifeId")
	public Wife getWife() {
		return wife;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}
	
}
Wife.java:

package com.buaa.hibernate.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Wife {
	private int id;
	private String name;
	
	@Id
	@GeneratedValue
	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;
	}
	
}
hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
	   
	   <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
	
	   <!-- Assume test is the database name -->
	   <property name="connection.url">jdbc:mysql://localhost/test</property>
	   <property name="connection.username">root</property>
	   <property name="connection.password">123456</property>
	   
	   <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
	   <property name="show_sql">true</property>
	   
	   <property name="format_sql">true</property>
	   <property name="hbm2ddl.auto">create</property>
	
	<mapping class="com.buaa.hibernate.bean.Husband"/>
	<mapping class="com.buaa.hibernate.bean.Wife"/>
	</session-factory>
</hibernate-configuration>
二、是xml版本:

Husband.hbm.xml:

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

<hibernate-mapping package="com.buaa.hibernate.bean">
	<class name="Husband">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="name"/>
		<many-to-one name="wife" column="wifeId" not-null="true"
				unique="true" />
	</class>

	<class name="Wife">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="name"/>
	</class>


</hibernate-mapping>

这里需要注意的是:Hiberante使用了many-to-one来实现one-to-one的关系映射,而且其中外键字段必须添加唯一约束。这样映射出来的表才能保证一对一的关系。

hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
	   
	   <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
	
	   <!-- Assume test is the database name -->
	   <property name="connection.url">jdbc:mysql://localhost/test</property>
	   <property name="connection.username">root</property>
	   <property name="connection.password">123456</property>
	   
	   <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
	   <property name="show_sql">true</property>
	   
	   <property name="format_sql">true</property>
	   <property name="hbm2ddl.auto">create</property>
	
		<mapping resource="com/buaa/hibernate/bean/Husband.hbm.xml"/>
	</session-factory>
</hibernate-configuration>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值