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 org.hibernate.annotations.Type; /**
* @author DSHORE/2019-9-18
* 一对一,单向关联(注解版)
*/
@Entity
public class Husband {//主表
private Integer id;
private String name;
private Boolean sex; @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;
}
}
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 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.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
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 test1(){//数据库表创建完后,插入数据
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();//事务提交
}*/ // 要进行save操作,先save(husband),后save(wife)
@Test
public void test2() {//只创建数据库表,不插入任何数据,可以这样测试。(hibernate.cfg.xml配置文件用的是create)
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
}
}
测试结果图:
2、XML实现 版
2.1、创建husband类和wife类
package com.shore.domel; /**
* @author DSHORE/2019-9-18
* 一对一,单向关联(xml版)
*/
public class Husband {//主表
private Integer id;
private String name;
private Boolean sex; 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;
}
}
wife类
package com.shore.domel; /**
* @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.domel">
<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"/>
</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.domel">
<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/domel/Husband.hbm.xml" />
<mapping resource="com/shore/domel/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.domel.Husband;
import com.shore.domel.Wife; /**
* @author DSHORE/2019-9-18
*
*/
public class XMLTest1 {
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();
}
}
测试结果图:
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/11545058.html 版权声明:欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!) |