[Hibernate] - one to one
两种不同方式的一对一映射关系:
1)配置文件:
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1/testdb</property>
<property name="connection.username">root</property>
<property name="connection.password"></property> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property> <!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property> <mapping resource="com/my/hbm/Employer.hbm.xml"/>
<mapping resource="com/my/hbm/Address.hbm.xml"/>
<mapping resource="com/my/hbm/Detial.hbm.xml"/> </session-factory> </hibernate-configuration>
Employer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.my.bean.Employer" table="employer">
<id name="id" type="long">
<column name="id"></column>
<generator class="native"></generator>
</id>
<property name="companyName" type="java.lang.String" length="100" column="company_name" not-null="true" />
<property name="createTime" type="java.util.Date">
<column name="create_time" not-null="true"/>
</property>
<one-to-one name="address"></one-to-one>
<one-to-one name="detial" property-ref="employer"></one-to-one>
</class>
</hibernate-mapping>
Address.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.my.bean.Address" table="address">
<id name="id" type="long" column="id">
<generator class="foreign">
<param name="property">employer</param>
</generator>
</id>
<property name="location" column="location" length="200" not-null="true" type="java.lang.String"></property>
<one-to-one name="employer" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
Detial.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.my.bean.Detial" table="detial">
<id name="id" type="long" column="id">
<generator class="native"></generator>
</id>
<many-to-one name="employer" column="employer_id" unique="true"></many-to-one>
<property name="info" column="info" type="java.lang.String" length="3000"></property>
</class>
</hibernate-mapping>
2)Java bean:
package com.my.bean; import java.util.Date; public class Employer { private long id;
private String companyName;
private Date createTime;
private Address address;
private Detial detial; public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Detial getDetial() {
return detial;
}
public void setDetial(Detial detial) {
this.detial = detial;
} }
package com.my.bean; public class Address { private long id;
private String location;
private Employer employer; public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Employer getEmployer() {
return employer;
}
public void setEmployer(Employer employer) {
this.employer = employer;
} }
package com.my.bean; public class Detial {
private long id;
private String info;
private Employer employer; public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Employer getEmployer() {
return employer;
}
public void setEmployer(Employer employer) {
this.employer = employer;
}
}
3)测试:
package com.my.init; import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set; import javax.xml.soap.Detail; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction; import com.my.bean.Address;
import com.my.bean.Detial;
import com.my.bean.Employer;
import com.my.bean.User;
import com.my.bean.UserAccount;
import com.my.dao.util.HibernateUtil; public class Test { @SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction(); // new employer
Employer employer = new Employer();
employer.setCompanyName("my company");
employer.setCreateTime(new Date()); // new address
Address address = new Address();
address.setLocation("china"); // new detail
Detial detail = new Detial();
detail.setInfo("info"); // add relation
employer.setAddress(address);
employer.setDetial(detail);
address.setEmployer(employer);
detail.setEmployer(employer); session.save(employer);
session.save(address);
session.save(detail); // Get employer
String hsqlSelect = "from Employer";
Query query = session.createQuery(hsqlSelect);
List<Employer> es = query.list();
if(es != null && es.size() > 0){
Employer e = es.get(0);
System.out.println(e.getCompanyName());
System.out.println(e.getDetial().getInfo());
} tx.commit();
session.close();
} }
关注点:
1)Employer.hbm.xml
<generator class="native"></generator>
这个generator的class="native"值有好几种,具体使用方法可参见:http://blog.csdn.net/ye1992/article/details/19632001
<one-to-one name="address"></one-to-one>
<one-to-one name="detial" property-ref="employer"></one-to-one>
这里有两个one-to-one,两种一对一的不同映射方式。
其中第二种one-to-one中有一个属性引用,对应的是在detial中的employer属性。
property-ref的使用说明可参见:http://blog.csdn.net/fengyuanfa/article/details/5096764
2)Address.hbm.xml
<id name="id" type="long" column="id">
<generator class="foreign">
<param name="property">employer</param>
</generator>
</id>
这是声明一个外部键字段的一对一关系用法。
<one-to-one name="employer" constrained="true"></one-to-one>
这里这个contrained设为true是必需的。
3)Detial.hbm.xml
<many-to-one name="employer" column="employer_id" unique="true"></many-to-one>
这是一对一关系的另一种现实方式,使用的是many-to-one,但unique设为true是必需的。
这两种不同的一对一关系映射,参考资料:
http://blog.csdn.net/mr__fang/article/details/8592000
http://www.tutorialspoint.com/hibernate/hibernate_one_to_one_mapping.htm
[Hibernate] - one to one的更多相关文章
- hibernate多对多关联映射
关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...
- 解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用
问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...
- hibernate多对一双向关联
关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...
- Hibernate中事务的隔离级别设置
Hibernate中事务的隔离级别,如下方法分别为1/2/4/8. 在Hibernate配置文件中设置,设置代码如下
- Hibernate中事务声明
Hibernate中JDBC事务声明,在Hibernate配置文件中加入如下代码,不做声明Hibernate默认就是JDBC事务. 一个JDBC 不能跨越多个数据库. Hibernate中JTA事务声 ...
- spring applicationContext.xml和hibernate.cfg.xml设置
applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...
- [原创]关于Hibernate中的级联操作以及懒加载
Hibernate: 级联操作 一.简单的介绍 cascade和inverse (Employee – Department) Casade用来说明当对主对象进行某种操作时是否对其关联的从对象也作类似 ...
- hibernate的基本xml文件配置
需要导入基本的包hibernate下的bin下的required和同bin下optional里的c3p0包下的所有jar文件,当然要导入mysql的驱动包了.下面需要注意的是hibernate的版本就 ...
- Maven搭建SpringMVC+Hibernate项目详解 【转】
前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...
- 1.Hibernate简介
1.框架简介: 定义:基于java语言开发的一套ORM框架: 优点:a.方便开发; b.大大减少代码量; c.性能稍高(不能与数据库高手相比,较一般数据库使用者 ...
随机推荐
- 基于 Qt的聊天工具
主要参考:http://blog.csdn.net/zouxy09/article/details/9140881
- eclipse中文乱码
问题描述: 在导入log4j.properties文件时里面出现中文乱码,如下: ### 设置### log4j.rootLogger = debug,stdout,D,E ### è¾å ...
- vcffilter 工具bug以及解决办法
1,使用说明: usage: vcffilter [options] <vcf file> options: -f, --info-filter specifies a filte ...
- 235. Lowest Common Ancestor of a Binary Search Tree
代码如下: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; ...
- 如何优雅的写C++代码(一)
// get the greatest power of two that is a divisor of n: return n&-n; // swap two integers a and ...
- js&jquery验证邮箱和手机号是否正确范例
实现源码: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ...
- Android开源项目大全之工具库
http://www.neast.cn/forum.php?mod=viewthread&tid=5487&fromuid=5
- 一些基本的C/C++数据类型
size_t size_t. A basic unsigned integer C/C++ type. It is the type of the result returned by sizeof ...
- poj1502 最短路
题意:有n个处理器,给出n*n的邻接矩阵的一半,表示相互之间传输信息的花费(另一半与给出的一半对称,即双向都可传输),x表示不能传输,问从第一个处理器传输到所有处理器的最小花费总和是多少. 就是跑一遍 ...
- Python-属性(property)
在2.6版本中,添加了一种新的类成员函数的访问方式--property. 原型 class property([fget[, fset[, fdel[, doc]]]]) fget:获取属性 fset ...