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

ex: 举的例子是一夫一妻制(一对一双向外键关联)

一、首先是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;
import javax.persistence.OneToOne;

@Entity
public class Wife {
	private int id;
	private String name;
	private Husband husband;
	
	@OneToOne(mappedBy="wife")
	public Husband getHusband() {
		return husband;
	}
	public void setHusband(Husband husband) {
		this.husband = husband;
	}
	@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>
测试类OneToOneTest.java:

package com.buaa.hibernate.bean;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.junit.Test;


public class OneToOneTest {
	@Test
	public void test(){
		Wife wife = new Wife();
		wife.setName("rose");
		Husband t = new Husband();
		t.setName("jack");
		t.setWife(wife);
		
		Configuration configuration = new AnnotationConfiguration();
		SessionFactory factory = configuration.configure().buildSessionFactory();
		Session session = factory.openSession();
		session.beginTransaction();
		session.save(wife);
		session.save(t);
		session.getTransaction().commit();
		session.close();
		factory.close();
	}
}
二、是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" unique="true"/>
	</class>

	<class name="Wife">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="name"/>
		<!-- 此时的property-ref相当于annotation中的mappedBy -->
		<one-to-one name="husband" property-ref="wife"></one-to-one>
	</class>


</hibernate-mapping>

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>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值