Java进阶知识07 Hibernate一对一双向外键关联(Annotation+XML实现)

1、Annotation 注解版

1.1、创建Husband类和Wife类

 package com.shore.model;

 import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne; import org.hibernate.annotations.Type; /**
* @author DSHORE/2019-9-18
* 一对一,双向关联(注解版)
*/
@Entity
public class Husband {//主表 类
private Integer id;
private String name;
private Boolean sex;
private Wife wife; @Id
@GeneratedValue(strategy = GenerationType.AUTO) //主键生成策略(使用test()测试时,必须加上这个,否会报错:id生成错误)
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;
} @Type(type = "yes_no") //数据库中,会以Y/N的形式插入到sex字段中
public Boolean getSex() {
return sex;
}
public void setSex(Boolean sex) {
this.sex = sex;
} @OneToOne(mappedBy="husband")//映射:从表wife映射主表husband。如果不要(mappedBy="husband"),主表husband将会多一个没必要的字段wife_id(外键)
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}

Wife类

 package com.shore.model;

 import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne; import org.hibernate.annotations.Type; /**
* @author DSHORE/2019-9-18
* 一对一,双向关联(注解版)
*/
@Entity
public class Wife {//从表
private Integer id;
private String name;
private Boolean sex;
private Husband husband; @Id
@GeneratedValue(strategy = GenerationType.AUTO) //主键生成策略(使用test1测试时,必须加上这个,否会报错:id生成错误)
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;
} @Type(type = "yes_no") //数据库中,会以Y/N的形式插入到sex字段中
public Boolean getSex() {
return sex;
}
public void setSex(Boolean sex) {
this.sex = sex;
} @OneToOne //默认创建的外键名称:husband_id
@JoinColumn(name="husbandId") //创建表时,指定该外键名:husbandId
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
}

1.2、创建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://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property> <mapping class="com.shore.model.Husband" />
<mapping class="com.shore.model.Wife" />
</session-factory>
</hibernate-configuration>

1.3、开始测试

 package com.shore.test;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test; import com.shore.model.Husband;
import com.shore.model.Wife; /**
* @author DSHORE/2019-9-18
*
*/
public class AnnotationTest {
public static SessionFactory sessionFactory = null;
public static Session session = null; @BeforeClass
public static void buildSessionFactory() {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} @AfterClass
public static void close() {
session.close();
sessionFactory.close();
} @Test
public void test(){//数据库表创建完后,插入数据
session = sessionFactory.openSession(); //打开一个session
Transaction transaction = session.beginTransaction(); //开启一个事务
Husband husband = new Husband();
husband.setName("黄晓明");
husband.setSex(true);//男
session.save(husband); Wife wife = new Wife();
wife.setName("AnglaBaby");
wife.setSex(false);//女
wife.setHusband(husband);
session.save(wife);
transaction.commit(); //事务提交
}
}

测试结果图:

Java进阶知识07 Hibernate一对一双向外键关联(Annotation+XML实现)

Java进阶知识07 Hibernate一对一双向外键关联(Annotation+XML实现)    Java进阶知识07 Hibernate一对一双向外键关联(Annotation+XML实现)

2、XML实现 版

2.1、创建husband类和wife类

 package com.shore.model;

 /**
* @author DSHORE/2019-9-18
* 一对一,双向关联(xml版)
*/
public class Husband {//主表 类
private Integer id;
private String name;
private Boolean sex;
private Wife wife; 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 Boolean getSex() {
return sex;
}
public void setSex(Boolean sex) {
this.sex = sex;
}
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}

wife类

 package com.shore.model;

 /**
* @author DSHORE/2019-9-18
* 一对一,双向关联(xml版)
*/
public class Wife {//从表 类
private Integer id;
private String name;
private Boolean sex;
private Husband husband; 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 Boolean getSex() {
return sex;
}
public void setSex(Boolean sex) {
this.sex = sex;
}
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
}

2.2、创建 Husband.hbm.xml 配置文件和 Wife.hbm.xml 配置文件

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.shore.model">
<class name="Husband" table="husband_xml">
<id name="id">
<generator class="native"/>
</id>
<property name="name" type="java.lang.String"/>
<property name="sex" type="yes_no"/> <!-- 这里不能这么配置,否则主表husband里面会多一个没必要的外键wife_id
<many-to-one name="wife" column="wife_id" unique="true"/> --> <!-- 应该这样配置 -->
<one-to-one name="wife" property-ref="husband"/>
</class>
</hibernate-mapping>

Wife.hbm.xml 配置文件

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.shore.model">
<class name="Wife" table="wife_xml">
<id name="id">
<generator class="native"/>
</id>
<property name="name" type="java.lang.String"/>
<property name="sex" type="yes_no" /> <!-- many-to-one:多对一,但加了个unique="true",就变成了一对一 -->
<many-to-one name="husband" column="husband_id" unique="true"/>
</class>
</hibernate-mapping>

2.3、创建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://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property> <!-- <mapping class="com.shore.model.Husband" />
<mapping class="com.shore.model.Wife" /> -->
<mapping resource="com/shore/model/Husband.hbm.xml" />
<mapping resource="com/shore/model/Wife.hbm.xml" />
</session-factory>
</hibernate-configuration>

2.4、开始测试

 package com.shore.test;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test; import com.shore.model.Husband;
import com.shore.model.Wife; /**
* @author DSHORE/2019-9-19
*
*/
public class XMLTest {
public static SessionFactory sessionFactory = null;
public static Session session = null; @BeforeClass
public static void buildSessionFactory() {
//用注解版的话,Configuration()方法,得改用AnnotationConfiguration()方法
sessionFactory = new Configuration().configure().buildSessionFactory();
} @AfterClass
public static void close() {
session.close();
sessionFactory.close();
} @Test
public void test(){//数据库表创建完后,插入数据
session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Husband husband = new Husband();
husband.setName("黄晓明");
husband.setSex(true);//男
session.save(husband); Wife wife = new Wife();
wife.setName("AnglaBaby");
wife.setSex(false);//女
wife.setHusband(husband);
session.save(wife);
transaction.commit();//事务提交
}
}

测试结果图:

Java进阶知识07 Hibernate一对一双向外键关联(Annotation+XML实现)

Java进阶知识07 Hibernate一对一双向外键关联(Annotation+XML实现)    Java进阶知识07 Hibernate一对一双向外键关联(Annotation+XML实现)

Hibernate一对一单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11545058.html
Hibernate一对一双向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11545077.html

Hibernate多对一单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11553213.html
Hibernate一对多单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11553215.html
Hibernate一对多和多对一双向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11560433.html

Hibernate多对多单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11568536.html
Hibernate多对多双向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11568963.html

原创作者:DSHORE

作者主页:http://www.cnblogs.com/dshore123/

原文出自:https://www.cnblogs.com/dshore123/p/11545077.html

版权声明:欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

上一篇:hibernate一对一单项关联映射


下一篇:Storm源码阅读之SpoutOutputCollector