zoukankan      html  css  js  c++  java
  • 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);
        }
    }

  • 相关阅读:
    VC++删除浮动工具条中“关闭”按钮
    automation无法创建对象
    SQL Server 不产生日志
    收缩数据文件
    VB DoEvents用法
    Sql Server添加用户
    Winsock错误代码一览表
    监控数据库性能的sql
    数据库日志文件清理脚本
    VB 中资源文件的多种使用技巧
  • 原文地址:https://www.cnblogs.com/daochong/p/4950279.html
Copyright © 2011-2022 走看看