SQL:
--花边
CREATE TABLE Lace
(
id INT IDENTITY,
name VARCHAR(10) NOT NULL UNIQUE, --花边名称
PRIMARY KEY(id)
)
--胚布
CREATE TABLE Cloth
(
id INT REFERENCES Lace(id), --花边
totalQuantity INT DEFAULT 0 NOT NULL, --总匹数
totalKg DECIMAL(18,2) DEFAULT 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">
<generator class="foreign"> <!--指定当前实体(也就是Cloth)的哪个属性跟Lace关联-->
<param name="property">lace</param>
<param name="property">lace</param>
</generator>
</id>
<property name="totalQuantity" />
<property name="totalKg" type="big_decimal" />
</id>
<property name="totalQuantity" />
<property name="totalKg" type="big_decimal" /> <!-- constrained="true" 表示Cloth的主键参照Lace的主键-->
<one-to-one name="lace" class="Lace" constrained="true" />
<one-to-one name="lace" class="Lace" constrained="true" />
</class>
</hibernate-mapping>

</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());
}
}
本文介绍如何使用Hibernate框架实现数据库中一对一关系的映射,并通过具体示例详细讲解了实体类设计、映射文件配置及测试过程。
1706

被折叠的 条评论
为什么被折叠?



