`
lijunabc
  • 浏览: 47450 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

hibernate 双向一对一基于主键的关联映射

阅读更多
Husband.java
package com.aabnn.vo;

public class Husband {
	private int id;
	private String name;
	private Wife wife;
	public Husband(){}
	public Husband(String name,Wife wife){
		this.name=name;
		this.wife=wife;
	}
	public int getId(){
		return id;
	}
	@SuppressWarnings("unused")
	private void setId(int id){
		this.id=id;
	}
	public String getName(){
		return name;
	}
	public void setName(String name){
		this.name=name;
	}
	public Wife getWife(){
		return wife;
	}
	public void setWife(Wife wife){
		this.wife=wife;
	}
}

Wife.java
package com.aabnn.vo;

public class Wife {
	private int id;
	private String name;
	private Husband husband;
	public Wife(){
		
	}
	public Wife(String name){
		this.name=name;
	}
	public int getId(){
		return id;
	}
	@SuppressWarnings("unused")
	private void setId(int id){
		this.id=id;
	}
	public String getName(){
		return name;
	}
	public void setName(String name){
		this.name=name;
	}
	public Husband getHusband(){
		return husband;
	}
	public void setHusband(Husband husband){
		this.husband=husband;
	}
}

HibernateUtil.java
package com.aabnn.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory sf;
	static{
		sf=new Configuration().configure().buildSessionFactory();
	}
	public static SessionFactory getSessionFactory(){
		return sf;
	}
}

Test.java
package com.aabnn.test;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import com.aabnn.util.HibernateUtil;
import com.aabnn.vo.Husband;
import com.aabnn.vo.Wife;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SessionFactory sf=HibernateUtil.getSessionFactory();
		Session sess=sf.openSession();
		Transaction tx=sess.beginTransaction();
		// TODO Auto-generated method stub
		try{
			
			Wife w1=new Wife("cuihua");
			
			Husband h1=new Husband();
			w1.setHusband(h1);
			h1.setName("huangjian");
			h1.setWife(w1);
			sess.save(h1);
			tx.commit();
			}catch(Exception e){
				tx.rollback();
			}finally{
				sf.close();
			}
		
	}

}

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

<hibernate-configuration>
	<session-factory>
	<!--mysql connection  -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<!--c3p0 connection pool  -->
		<property name="hibernate.c3p0.max_size">20</property>
		<property name="hibernate.c3p0.min_size">2</property>
		<property name="hibernate.c3p0.max_statements">100</property>
		<property name="hibernate.c3p0.idle_test_period">3000</property>
		<property name="hibernate.c3p0.acquire_increment">2</property>
		<property name="hibernate.c3p0.validate">true</property>
		<!-- mysql dialect -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
		<property name="hibernate.show_sql">true</property>
		<mapping resource="com/aabnn/vo/Husband.hbm.xml" />
		<mapping resource="com/aabnn/vo/Wife.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

Husband.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 package="com.aabnn.vo">

	<class name="Husband" table="husband">
		<id name="id" column="id">
			<generator class="identity" />
		</id>	
		<property name="name" type="string" column="name" />
		<one-to-one name="wife" cascade="save-update"/>
	</class>	
</hibernate-mapping>

Wife.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 package="com.aabnn.vo">
	<class	name="Wife" table="wife">
		<id name="id" column="id">
			<generator class="foreign">
				<param name="property">husband</param>
			</generator>
		</id>
		<property name="name" column="name" type="string" />
		<one-to-one name="husband" constrained="true"/>
	</class>
</hibernate-mapping>

mysql数据库
create database hibernate;
create table Husband(
id integer unsigned not null auto_increment,
name varchar(45),
primary key(id)
);
create table Wife(
id integer unsigned not null,
name varchar(45),
primary key(id)
);
分享到:
评论

相关推荐

    Hibernate一对一主键关联映射(双向关联)

    博文链接:https://shaqiang32.iteye.com/blog/201309

    Hibernate关联映射

    Hibernate 一对一外键单向关联 Hibernate 一对一主键单向...Hibernate 一对一主键双向关联 Hibernate 一对一连接表双向关联 Hibernate 一对多外键双向关联 Hibernate 一对多连接表双向关联 Hibernate 多对多双向关联

    hibernate一对一主键关联映射(双项关联)

    博文链接:https://llying.iteye.com/blog/220803

    Hibernate教程07_关系映射之一对一双向主键关联

    http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码

    Hibernate关联关系映射目录

    Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联 │ ├─ 一对一主键单向...├─ 一对一主键双向关联 ├─ 一对一连接表双向关联 ├─ 一对多外键双向关联 ├─ 一对多连接表双向关联 └─ 多对多双向关联

    Hibernate学习笔记

    010 一对一 主键关联映射_双向 011 一对一 唯一外键关联映射_单向 012 一对一 唯一外键关联映射_双向 013 session_flush 014 一对多关联映射 单向 015 一对多关联映射 双向 016 多对多关联映射 单向 017 多对多关联...

    hibernate学习笔记

    hibernate一对一主键关联映射(双向关联Person&lt;----&gt;IdCard) 9 hibernate一对一唯一外键关联映射(单向关联Person----&gt;IdCard) 10 hibernate一对一唯一外键关联映射(双向关联Person&lt;----&gt;IdCard) 11 session ...

    Hibernate_Annotation关联映射

    1.共享主键的一对一关联映射: @Entity @Table(name="Test_Body") public class Body { private Integer id; private Heart heart; @Id public Integer getId() { return id; } public void setId(Integer id...

    Hibernate_实体关联关系映射--学习总结

    Hibernate 实体关联关系映射 学习总结 把一对一 一对多 单向 双向 主键 外键 链接表等讲的比较清楚

    Hibernate学习笔记_songjignhao_1

    自己记录的学习笔记,内容很详细,有代码,包括:一对多双向关联关系、一对多双向自身关联关系、一对一关联关系(主键关联、外键关联)、一对多与一对一结合、多对多关联关系、Map、Set、List与Bag映射关系、查询...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

     15.1 映射一对一关联  15.1.1 按照外键映射   15.1.2 按照主键映射  15.2 映射单向多对多关联  15.3 映射双向多对多关联关系  15.3.1 关联两端使用元素  15.3.2 在inverse端使用元素  15.3.3 使用组件类...

    Hibernate注释大全收藏

    Hibernate注释大全收藏 声明实体Bean @Entity public class Flight implements Serializable { Long id; @Id public Long getId() { return id; } public void setId(Long id) { this.id...一对一 使用 @OneToOne...

    Hibernate教程

    2.3. 第二部分 - 关联映射 2.3.1. 映射Person类 2.3.2. 一个单向的Set-based关联 2.3.3. 使关联工作 2.3.4. 值类型的集合 2.3.5. 双向关联 2.3.6. 使双向关联工作 2.4. 总结 3. 体系结构(Architecture) ...

    Hibernate 中文 html 帮助文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    Hibernate实战(第2版 中文高清版)

     7.1.2 一对一的外键关联   7.1.3 用联结表映射   7.2 多值的实体关联   7.2.1 一对多关联   7.2.2 多对多关联   7.2.3 把列添加到联结表   7.2.4 映射map   7.3 多态关联   7.3.1 多态的多对一...

    最全Hibernate 参考文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    hibernate 体系结构与配置 参考文档(html)

    一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many ...

Global site tag (gtag.js) - Google Analytics