Hibernate OneToOne
来源:互联网 发布:json的数据交换接口 编辑:程序博客网 时间:2024/05/01 18:05
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)
)
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;
}
}
/** *//**
* @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">
<?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);
*
*/
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 OneToOne
- hibernate--OneToOne
- Hibernate-----@OneToOne
- Hibernate入门之OneToOne
- onetoone hibernate配置
- Hibernate OneToOne注解配置
- hibernate--@OneToOne实例
- Hibernate OneToOne注解配置
- Hibernate OneToOne注解配置
- Hibernate @OneToOne -- 1
- Hibernate @OneToOne -- 2
- Hibernate OneToOne注解配置
- hibernate onetoone 级联保存
- hibernate OnetoOne 级联删除
- Hibernate OneToOne注解
- Hibernate OneToOne关联
- Hibernate Mapping 1 : OneToOne
- hibernate onetoone 注解
- 我的slackware10.2硬盘安装方法
- SQL Server 2005 中能够使用 Try...Catch语句
- J2EE Architecture(6)
- 一个清除自定义通知的小程序【原创.附源代码】
- C# 用Enter代替Tab 实现方法
- Hibernate OneToOne
- Ruby & Rails Resources
- swf文件结构解析
- 定居CSDN
- SQL Server 2005 添加了nvarchar(max),varchar(max)
- 也弄一个试试
- 程序员
- 推荐一个学VC的超好网站!可惜今天才发现。。
- ASP.NET服务器控件之视图状态