Hibernate4之一对一映射关系(十一)
程序员文章站
2022-04-14 11:20:57
...
一对一映射关系
1.按照主键映射
2.按照外键映射
一、按照主键映射
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>
<!--数据库连接设置 -->
<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.MySQL5Dialect</property>
<!-- 控制台显示SQL -->
<property name="show_sql">true</property>
<!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/newbeedaly/model/User.hbm.xml"/>
<mapping resource="com/newbeedaly/model/Address.hbm.xml"/>
<mapping resource="com/newbeedaly/model/User2.hbm.xml"/>
<mapping resource="com/newbeedaly/model/Address2.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.编写HibernateUtil类
package com.newbeedaly.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static final SessionFactory sessionFactory=buildSessionFactory();
private static SessionFactory buildSessionFactory(){
Configuration configuration=new Configuration().configure(); // 实例化配置文件
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); // 实例化服务登记
return configuration.buildSessionFactory(serviceRegistry); // 获取Session工厂
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
3.编写User.java和Address.java
package com.newbeedaly.model;
public class User {
private int id;
private String name;
private Address address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
package com.newbeedaly.model;
public class Address {
private int id;
private String address;
private String zipcode;
private User user;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
4.编写配置文件User.hbm.xml和Address.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.newbeedaly.model">
<class name="User" table="t_user">
<id name="id" column="userId">
<generator class="native"></generator>
</id>
<property name="name" column="userName"></property>
<one-to-one name="address" class="com.newbeedaly.model.Address" cascade="all"></one-to-one>
</class>
</hibernate-mapping>
<?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.newbeedaly.model">
<class name="Address" table="t_address">
<id name="id" column="addressId">
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<property name="address" column="address"></property>
<property name="zipcode" column="zipcode"></property>
<one-to-one name="user" class="com.newbeedaly.model.User" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
5.编写测试类UserTest
package com.newbeedaly.service;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.newbeedaly.model.Address;
import com.newbeedaly.model.Address2;
import com.newbeedaly.model.User;
import com.newbeedaly.model.User2;
import com.newbeedaly.util.HibernateUtil;
public class UserTest {
private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session;
@Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
}
@After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
}
@Test
public void testSave1(){
User user=new User();
user.setName("张三");
Address address=new Address();
address.setAddress("某地方");
address.setZipcode("43242");
address.setUser(user);
user.setAddress(address);
session.save(user);
}
@Test
public void testSave2(){
User2 user=new User2();
user.setName("李四");
Address2 address=new Address2();
address.setAddress("某地方2");
address.setZipcode("432422");
address.setUser(user);
user.setAddress(address);
session.save(user);
}
}
二、按照外键映射
1.编写User2.java和Address2.java
package com.newbeedaly.model;
public class User2 {
private int id;
private String name;
private Address2 address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address2 getAddress() {
return address;
}
public void setAddress(Address2 address) {
this.address = address;
}
}
package com.newbeedaly.model;
public class Address2 {
private int id;
private String address;
private String zipcode;
private User2 user;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public User2 getUser() {
return user;
}
public void setUser(User2 user) {
this.user = user;
}
}
2.编写配置文件User2.bhm.xml和Address2.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.newbeedaly.model">
<class name="User2" table="t_user2">
<id name="id" column="userId">
<generator class="native"></generator>
</id>
<property name="name" column="userName"></property>
<many-to-one name="address" class="com.newbeedaly.model.Address2" column="addressId" cascade="all" unique="true"></many-to-one>
</class>
</hibernate-mapping>
<?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.newbeedaly.model">
<class name="Address2" table="t_address2">
<id name="id" column="addressId">
<generator class="native">
</generator>
</id>
<property name="address" column="address"></property>
<property name="zipcode" column="zipcode"></property>
<one-to-one name="user" class="com.newbeedaly.model.User2" property-ref="address"></one-to-one>
</class>
</hibernate-mapping>
推荐阅读
-
MyBatis从入门到精通(十一):MyBatis高级结果映射之一对多映射
-
Mybatis使用注解实现一对一复杂关系映射及延迟加载
-
SpringDataJpa在一对多、多对多关系映射时出现*Error
-
MyBatis3一对多关系映射
-
MyBatis3一对一关系映射
-
Mybatis使用注解实现一对一复杂关系映射及延迟加载
-
Mybatis04—注解开发实现CRUD以及实现一对一、一对多及多对多复杂关系映射
-
Mybatis常用的注解开发CRUD&&复杂关系映射(一对一,一对多)&&mybatis 基于注解的二级缓存
-
Mybatis—— 使用注解实现一对一复杂关系映射及延迟加载
-
Mybatis使用注解实现一对多复杂关系映射