标签:hibernate
一:项目截图
二:业务代码
package com.cloud.domain;
import java.util.Date;
public class IdCard {
private Integer id;
private Date validateDte;
private Person person;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getValidateDte() {
return validateDte;
}
public void setValidateDte(Date validateDte) {
this.validateDte = validateDte;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
package com.cloud.domain;
public class Person {
private Integer id;
private String name;
private IdCard idCard;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
}
三:项目配置文件
IdCard.hbm.xml
<?xml version="1.0"encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/HibernateMapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.cloud.domain">
<class name="IdCard"table="idCard">
<!--<id name="id" type="java.lang.Integer">-->
<!--因为这里是基于主键的one-to-one,所以要使用外键策略 -->
<!--<generator class="foreign">-->
<!--这里的值是指哪个属性one-to-one -->
<!--
<paramname="property">person</param>
</generator>
</id>
-->
<!-- 这里配置演示基于外键的one-to-one -->
<id name="id"type="java.lang.Integer">
<generator class="assigned"/>
</id>
<property name="validateDte" type="java.util.Date">
<column name="validateDte"/>
</property>
<!-- 配置idcard和person一对一关系,先有一个人,才有一张身份证 -->
<!--
<one-to-onename="person"></one-to-one>
-->
<!-- 可以有多条数据指向同一个主键 -->
<many-to-one name="person" unique="true"/>
</class>
</hibernate-mapping>
Person.hbm.xml
<?xml version="1.0"encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/HibernateMapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.cloud.domain">
<class name="Person"table="person">
<id name="id"type="java.lang.Integer">
<!-- 这里手动分配id -->
<generator class="assigned"/>
</id>
<property name="name"type="java.lang.String">
<column name="name"length="128"/>
</property>
<!-- 配置person和idcard一对一关系,先有一个人,才有一张身份证 -->
<one-to-one name="idCard"></one-to-one>
</class>
</hibernate-mapping>
Hibernate.cfg.xml
<?xml version=‘1.0‘ encoding=‘UTF-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse HibernateTools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/zjgm01
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<!-- 配置hibernate自动创建关系模型表 -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/cloud/domain/Person.hbm.xml" />
<mapping resource="com/cloud/domain/IdCard.hbm.xml" />
</session-factory>
</hibernate-configuration>
四:测试代码package com.cloud.view;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.cloud.domain.IdCard;
import com.cloud.domain.Person;
import com.cloud.util.HibernateUtil;
public class TestMain {
public static void main(String[] args) {
//添加一组学生和IdCard
Session s=null;
Transaction ts=null;
try {
s=HibernateUtil.getCurrentSession();
ts=s.beginTransaction();
//测试基于主键的one-to-one
/*
Person p1=new Person();
p1.setId(123);
p1.setName("朝阳");
IdCard idCard=new IdCard();
idCard.setValidateDte(new Date());
//这里表示idCard是属于p1对象的
idCard.setPerson(p1);
//必须先保存人
s.save(p1);
s.save(idCard);
*/
//基于外键测试one-to-one
Person p1=new Person();
p1.setId(11);
p1.setName("春天");
IdCard idCard=new IdCard();
idCard.setId(2015);
idCard.setValidateDte(new Date());
//这里表示idCard是属于p1对象的
idCard.setPerson(p1);
//必须先保存人
s.save(p1);
s.save(idCard);
ts.commit();
} catch (Exception e) {
if(ts!=null){
ts.rollback();
}
}finally{
if(s!=null && s.isOpen()){
s.close();
}
}
}
}
五:测试结果
Hibernate: insert into person (name, id) values (?, ?)
Hibernate: insert into idCard (validateDte, person, id) values (?, ?, ?)
版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21
标签:hibernate
原文地址:http://blog.csdn.net/dzy21/article/details/47812851