hibernate单边的一对一关系
来源:互联网 发布:麒麟970与骁龙835知乎 编辑:程序博客网 时间:2024/03/28 21:34
一对一关系用于保证数据库中的关系是一对一的,不允许出现一个表的两行记录共用另一个表中的同一个记录的情况。可以通过设置外键的唯一性约束或者利用主键本来的唯一性实现。
demo实例
该例程引用网络上可以查到的舰船关系实例。一个舰船上有若干个船员以及一个船长。舰船与船员是一对多关系,而与船长则是一对一关系,即一艘船可以有多个船员,但是只能有一个船长,一个船长只能管理一艘舰船。
1、建立java工程
2、导入hibernate和MySql相关类库。
详见之前的博客hibernate单边一对多关系中的配置。
3、建立实体类
Ship为舰船的实体类,相关代码如下
将captain属性的unique属性设置为true,利用数据库的unique约束来保证一个船长不会被两艘船使用。
船员属性sailors是一对多,所以使用OneToMany配置,使用mappedBy="ship"制定具备配置在对方实体类的ship属性中。
package com.arvinfei.hibernate.bean;import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToMany;import javax.persistence.OneToOne;import javax.persistence.Table;@Entity@Table(name = "tb_ship")public class Ship {@Id@GeneratedValue(strategy = GenerationType.AUTO)private Integer id;private String name;@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.REFRESH })@JoinColumn(name = "captain_id", unique = true)private Sailor captain;@OneToMany(mappedBy = "ship", cascade = CascadeType.PERSIST)private Set<Sailor> sailors = new HashSet<Sailor>();public Sailor getCaptain() {return captain;}public void setCaptain(Sailor captain) {this.captain = captain;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set<Sailor> getSailors() {return sailors;}public void setSailors(Set<Sailor> sailors) {this.sailors = sailors;}}
salior为船员和船长的实体类,相关代码如下:
package com.arvinfei.hibernate.bean;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.OneToOne;import javax.persistence.Table;@Entity@Table(name = "tb_sailor")public class Sailor {@Id@GeneratedValue(strategy = GenerationType.AUTO)private Integer id;private String name;@ManyToOne(cascade = CascadeType.PERSIST)@JoinColumn(name = "ship_id")private Ship ship;@OneToOne(mappedBy = "captain")private Ship captainedShip;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Ship getShip() {return ship;}public void setShip(Ship ship) {this.ship = ship;}public Ship getCaptainedShip() {return captainedShip;}public void setCaptainedShip(Ship catainedShip) {this.captainedShip = catainedShip;}}
4、在hibernate的配置文件中添加实体类映射关系
<?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"><!-- Generated by MyEclipse Hibernate Tools. --><hibernate-configuration><session-factory><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/one2one?characterEncoding=UTF-8</property><property name="connection.username">root</property><property name="connection.password">admin</property><property name="dialect">org.hibernate.dialect.MySQL5Dialect</property><property name="show_sql">true</property><property name="hbm2ddl.auto">create</property><!-- 要加这一句 否则可能会遇到异常 --><property name="current_session_context_class">thread</property><mapping class="com.arvinfei.hibernate.bean.Sailor" /><mapping class="com.arvinfei.hibernate.bean.Ship" /></session-factory></hibernate-configuration>
5、新建测试代码
package com.arvinfei.hibernate.test;import java.util.List;import org.hibernate.Session;import com.arvinfei.hibernate.bean.Sailor;import com.arvinfei.hibernate.bean.Ship;import com.arvinfei.hibernate.util.HibernateSessionFactory;public class One2One {@SuppressWarnings("all")public static void main(String[] args) throws Exception {Ship ship = new Ship();ship.setName("泰坦尼克");Sailor captain = new Sailor();captain.setName("史密斯");captain.setShip(ship);Sailor sailor = new Sailor();sailor.setName("杰克");sailor.setShip(ship);ship.setCaptain(captain);ship.getSailors().add(captain);ship.getSailors().add(sailor);Session session = HibernateSessionFactory.getSession();session.beginTransaction();session.persist(ship);List<Sailor> list = session.createQuery(" select s from Sailor s where s.ship.name = :name ").setParameter("name", "泰坦尼克").list();for (Sailor s : list) {System.out.println("水手:" + s.getName());System.out.println("舰船:" + s.getShip().getName());System.out.println("船长:" + s.getShip().getCaptain().getName());System.out.println("-----------------");}session.getTransaction().commit();session.close();}}
6、修改HibernateSessionFactory
修改HibernateSessionFactory中的sessionFactory配置方式,默认为xml配置方式,demo中使用注解进行配置,所以需要进行简单修改。
7、运行测试
(1)、打开并连接到mysql数据库;
(2)、在mysql中创建Many2Many数据库
create database one2one character set 'utf8';
(3)、运行本测试工程,查看log输出。
8、源码下载
http://download.csdn.net/detail/yxtouch/9161227
附件中代码为双边的一对一关系,如果需要修改为单边的一对一关系请去掉博客http://blog.csdn.net/smilefyx/article/details/48951789中添加的代码即可。
0 0
- hibernate单边的一对一关系
- Hibernate实体关系映射——单边的一对一关系
- Hibernate单边的一对多关系
- hibernate单边的多对多关系
- Hibernate的一对一关系
- Hibernate,一对一的关联关系
- hibernate双边的一对一关系
- Hibernate的一对一关联关系
- Hibernate中表的一对一关系
- Hibernate实体映射——单边的一对多关系
- Hibernate 一对一的关系(one-to-one)
- Hibernate一对一关联关系的使用
- hibernate主键相同的一对一关系
- Hibernate基于主键的一对一映射关系
- Hibernate映射一对一的关联关系
- Hibernate 单边多对多关系 @ManyToMany
- Hibernate实体关系映射——单边的多对一关系
- hibernate实体关系映射——单边的多对多关系
- C#学习之多线程开发技术(十)
- 一段好玩的程序
- 对某个Android应用的某一功能测试
- UVA11971 - Polygon
- 项目33.1利用循环求和
- hibernate单边的一对一关系
- 从今天起,做一个幸福的人
- 2015-10-7网络应用初认识--1
- Sort List
- 1.4 本章小结
- 评审分类
- 1021. 个位数统计 (15)
- PS将图层保存为文件
- 呵呵