码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-04-30 06:32:07      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

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

需要在一端添加<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);
        }
        
    }
}

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

标签:

原文地址:http://www.cnblogs.com/zhengcheng/p/5447968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!