用户和地址一对一映射关系实现(按照主键映射)
Address.java
package com.java1234.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;
}
}
User.java
package com.java1234.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;
}
}
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.java1234.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.java1234.model.User" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
解析:addressId既是t_address的主键,又是t_user的外键,constrained="true"配置实现了addressId在两个表中的共享
User.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.java1234.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.java1234.model.Address" cascade="all"></one-to-one>
</class>
</hibernate-mapping>
解析:cascade="all"使用户和地址可以级联更新保存删除
UserTest.java
package com.java1234.service;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.java1234.model.Address;
import com.java1234.model.Address2;
import com.java1234.model.User;
import com.java1234.model.User2;
import com.java1234.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);
}
}
数据库截图:
二:用户和地址一对一映射关系实现(按照外键映射)
实体类和上面差不多
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.java1234.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.java1234.model.User2" property-ref="address"></one-to-one>
</class>
</hibernate-mapping>
解析:property-ref="address"是引用address对象
User2.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.java1234.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.java1234.model.Address2" column="addressId" cascade="all" unique="true"></many-to-one>
</class>
</hibernate-mapping>
解析:通过unique="true"使外键addressId唯一,即实现一对一
运行之后数据库截图: