Hibernate OneToOne

来源:互联网 发布:json的数据交换接口 编辑:程序博客网 时间:2024/05/01 18:05

SQL:

--花边
CREATE TABLE Lace
(
id 
INT IDENTITY,
name 
VARCHAR(10NOT NULL UNIQUE,            --花边名称
PRIMARY KEY(id) 
)

--胚布
CREATE TABLE Cloth
(
id 
INT REFERENCES Lace(id),            --花边
totalQuantity INT DEFAULT 0 NOT NULL,        --总匹数
totalKg    DECIMAL(18,2DEFAULT 0.0 NOT NULL,    --总重量
PRIMARY KEY(id)
)

花边跟胚布的关系是1 v 1
在Hibernate如何表达他们之间的关系?
步骤:
在各自的实体类里面相互包含,看代码( 注意红色部分):

//Lace.java

/**
 * 
@author DoItPerfect
 * 
 
*/

public class Lace {

    
private int id;

    
private String name;

    
private Cloth cloth;

    
public Cloth getCloth() {
        
return this.cloth;
    }


    
public void setCloth(Cloth cloth) {
        
this.cloth = cloth;
    }


    
public int getId() {
        
return this.id;
    }


    
public void setId(int id) {
        
this.id = id;
    }


    
public String getName() {
        
return this.name;
    }


    
public void setName(String name) {
        
this.name = name;
    }

}



//Cloth.java

import java.math.BigDecimal;

/**
 * 
@author DoItPerfect
 * 
 
*/

public class Cloth {
    
private int id;

    
private int totalQuantity;

    
private BigDecimal totalKg;

    
private Lace lace;

    
public int getId() {
        
return this.id;
    }


    
public void setId(int id) {
        
this.id = id;
    }


    
public Lace getLace() {
        
return this.lace;
    }


    
public void setLace(Lace lace) {
        
this.lace = lace;
    }


    
public BigDecimal getTotalKg() {
        
return this.totalKg;
    }


    
public void setTotalKg(BigDecimal totalKg) {
        
this.totalKg = totalKg;
    }


    
public int getTotalQuantity() {
        
return this.totalQuantity;
    }


    
public void setTotalQuantity(int totalQuantity) {
        
this.totalQuantity = totalQuantity;
    }


}

接下来编写映射文件:

<!-- Lace.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="Lace" dynamic-insert="true">
        
<id name="id" type="int">
            
<generator class="identity" />
        
</id>
        
<property name="name" />
        
<one-to-one name="cloth" class="Cloth"   cascade="all /> <!-- 这里只需要指定one-to-one 对应的属性名称和类名即可,还要级联,否则一旦修改数据会丢失完整性.-->
    </class>
</hibernate-mapping>

<!-- Cloth.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="Cloth" dynamic-insert="true">
        
<id name="id" type="integer" column="id" unsaved-value="0">
                <-- 这里是重点,首先Cloth表的主键字段(id)为Lace表的外健,所以要指定该主键为一个外健-->
            
<generator class="foreign">
                    <!--指定当前实体(也就是Cloth)的哪个属性跟Lace关联-->
                
<param name="property">lace</param>            
               </generator>
        
</id>
        
<property name="totalQuantity" />
        
<property name="totalKg" type="big_decimal" />
           <!-- constrained="true"  表示Cloth的主键参照Lace的主键-->
        
<one-to-one name="lace" class="Lace" constrained="true" />    
        </class>
</hibernate-mapping>

接下来在hibernate.cfg.xml添加以上两个文件的映射.

最后测试...(懒得把代码改了看测试代码就能理解了,偶直接copy项目里面的)

/**
 * 
 
*/

package com.hts.mis.test.dao;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.hts.mis.dao.ILaceDAO;
import com.hts.mis.model.Cloth;
import com.hts.mis.model.Lace;
import com.hts.mis.util.DAOFactory;
import com.hts.mis.util.HibernateUtil;

/**
 * 
@author DoItPerfect
 * 
 
*/

public class TestOneLaceToOneCloth {

    
/**
     * 
@throws java.lang.Exception
     
*/

    @Before
    
public void setUp() throws Exception {
        HibernateUtil.getSessionFactory().getCurrentSession()
                .beginTransaction();
    }


    
/**
     * 
@throws java.lang.Exception
     
*/

    @After
    
public void tearDown() throws Exception {
        HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
                .commit();
    }


    @Test
    
public void testAddLaceAndCloth() {

        
        ILaceDAO dao 
= DAOFactory.getInstance().getLaceDAO();
        Lace lace 
= new Lace();
        lace.setName(
"test");
        lace.setHandle(DAOFactory.getInstance().getHandleDAO().getAll().get(
3));
        Cloth cloth 
= new Cloth();
        lace.setCloth(cloth);
        cloth.setLace(lace);
        dao.save(lace);
        Assert.assertNotNull("save lace success!",lace.getId());
        Assert.assertNotNull("save cloth success!",cloth.getId());
    }

}

 

 

 

原创粉丝点击