hibernate一对一关系实现

时间:2024-07-20 21:04:20

按照主键映射,按照外键映射

hibernate一对一关系实现hibernate一对一关系实现

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="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="model.User" constrained="true"></one-to-one>
    </class>

</hibernate-mapping>

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="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="model.User2" property-ref="address"></one-to-one>
    </class>

</hibernate-mapping>

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="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="model.Address" cascade="all"></one-to-one>
    </class>

</hibernate-mapping>

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="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="model.Address2" column="addressId" cascade="all" unique="true"></many-to-one>
    </class>

</hibernate-mapping>

UserTest.java:

package service;

import model.Address;
import model.Address2;
import model.User;
import model.User2;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import util.HibernateUtil;

public class UserTest {

SessionFactory sessionfactory=HibernateUtil.getSessionFactory();//获取Session工厂
    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("1111");
        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("南极");
        address.setZipcode("2222");
        address.setUser(user);
        
        user.setAddress(address);
        session.save(user);
    }
}