• Hibernate关系映射(二) 基于外键的双向一对一


    基于外键的双向一对一关联映射

    需要在一端添加<one-to-one>标签,用property-ref来指定反向属性引用。

    还是通过刚才用户和地址来演示双向一对一关联。

    代码演示

    一、实体类

    Account.cs,需要添加被控端的引用

    复制代码
    package com.lxit.entity;
    
    import java.io.Serializable;
    
    public class Account implements Serializable{
        public Account(){
            
        }
        private int id;
        private String name;
        private String password;
        //需要添加被控端的引用
        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 String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public Address getAddress() {
            return address;
        }
        public void setAddress(Address address) {
            this.address = address;
        }
        
    }
    复制代码

    Address.cs,被控端也需要添加主控端的引用

    复制代码
    package com.lxit.entity;
    
    import java.io.Serializable;
    
    public class Address implements Serializable{
        public Address(){
            
        }
        private int id;
        private String name;
        
        //双向1对1,被控端也知道主控端的引用
        private Account account;
        
        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 Account getAccount() {
            return account;
        }
        public void setAccount(Account account) {
            this.account = account;
        }    
    }
    复制代码

    二、映射文件

    Account.hbm.xml

    复制代码
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
     <class name="com.lxit.entity.Account" table="Account">
      <id column="id" name="id">
       <generator class="native"/>
      </id>
      <property column="name" generated="never" lazy="false" name="name"/>
      <property column="password" generated="never" lazy="false" name="password"/>
      <!-- 通过many-to-one 标签添加唯一属性约束,建立一对一关联关系 -->
      <many-to-one column="address_id" name="address" unique="true"/>
      <!-- 必须通过映射关系生成的表才会生成唯一约束,否则不会生成 -->
     </class>
    </hibernate-mapping>
    复制代码

    Address.hbm.xml

    复制代码
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
     <class name="com.lxit.entity.Address" table="Address">
      <id column="id" name="id">
       <generator class="native"/>
      </id>
      <property column="name" generated="never" lazy="false" name="name"/>
      
      <!-- one-to-one 映射一对一关联,name对应当前类的引用属性,property-ref属性:指定account类的属性引用 -->
      <one-to-one name="account" property-ref="address"></one-to-one>
     </class>
    </hibernate-mapping>
    复制代码

    jUtilTest

    复制代码
    package com.lxit.demo2.test;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    import org.junit.Test;
    
    import com.lxit.entity.Account;
    import com.lxit.entity.Address;
    import com.lxit.util.HibernateUtil;
    
    
    public class AccountTest {
        
        @Test
        public void createTable(){
            Configuration cfg = new Configuration().configure();
            SchemaExport se = new SchemaExport(cfg);
            se.create(true, true);
        }
        
        @Test
        public void Add(){
            Transaction tx = null;
            Session session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            
            Address address= new Address();
            address.setName("深圳宝安");        
            session.save(address);
            
            Account account = new Account();
            account.setName("zhangsan");
            account.setPassword("123");
            account.setAddress(address);
            
            try {
                session.save(account);
                tx.commit();
            } catch (Exception e) {
                e.printStackTrace();
                tx.rollback();
            }finally{
                HibernateUtil.CloseSession(session);
            }
            
        }
        
        @Test
        public void Add2(){
            Transaction tx = null;
            Session session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            
            Address address= new Address();
            address.setName("深圳福田");        
            session.save(address);
            
            Account account1 = new Account();
            account1.setName("lisi");
            account1.setPassword("123");
            account1.setAddress(address);
            
            Account account2 = new Account();
            account2.setName("wangwu");
            account2.setPassword("123");
            account2.setAddress(address);
            
            try {
                //重复添加第二个用户,地址相同,则会报错
                session.save(account1);
                session.save(account2);
                tx.commit();
            } catch (Exception e) {
                e.printStackTrace();
                tx.rollback();
            }finally{
                HibernateUtil.CloseSession(session);
            }
            
        }
    }
    复制代码
  • 相关阅读:
    MySQL-子查询,派生表,通用表达式
    MySQL-插入数据(INSERT)
    IDEA中如何使用debug调试项目 一步一步详细教程
    Java相对路径/绝对路径总结
    jsp九个内置对象、四个域对象及Servlet的三大域对象
    浅析MVC模式与三层架构的区别
    三层架构详解
    Java集合中List,Set以及Map等集合体系详解
    POJ3233 [C
    HDU 2829 [Lawrence] DP斜率优化
  • 原文地址:https://www.cnblogs.com/Jeely/p/11226148.html
Copyright © 2020-2023  润新知