大家在做开发的时候一定会用到表与表之间一对一的关系.
在学习数据的关联关系时候,请把容器映射看一看.不然不容易理解关联关系.
环境: Myeclispe+mysql.
(1) 创建表
--刪除表
drop table if exists email ;
drop table if exists person ;
--新建表
create table person
(
id int not null auto_increment primary key ,
name varchar(20) not null ,
age int
) ;
create table email
(
id int ,
ema varchar(20) not null,
time varchar(20) ,
foreign key (id) references person(id) on delete cascade
) ;
--提交
commit ;
(2) Peson.java 类
package org.hibernate.onetoone.vo;
public class Person {
private int id ;
private String name ;
private int age ;
private Email e ;
public Email getE() {
return e;
}
public void setE(Email e) {
this.e = e;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
(3) Person.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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="org.hibernate.onetoone.vo.Person" table="person" >
<id name="id" type="java.lang.Integer" >
<column name="id"></column>
<generator class="increment"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name"></column>
</property>
<property name="age" type="java.lang.Integer">
<column name="age"></column>
</property>
<one-to-one name="e" class="org.hibernate.onetoone.vo.Email"
lazy="false" cascade="all" outer-join="true" >
</one-to-one>
</class>
</hibernate-mapping>
(4) Email.java类
package org.hibernate.onetoone.vo;
public class Email {
private int id ;
private String ema ;
private String time ;
private Person p ;
public Person getP() {
return p;
}
public void setP(Person p) {
this.p = p;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEma() {
return ema;
}
public void setEma(String ema) {
this.ema = ema;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
(5) Email.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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="org.hibernate.onetoone.vo.Email" table="email" >
<id name="id" type="java.lang.Integer">
<column name="id"></column>
<generator class="foreign">
<param name="property">p</param>
</generator>
</id>
<property name="ema" type="java.lang.String">
<column name="ema"></column>
</property>
<property name="time" type="java.lang.String">
<column name="time"></column>
</property>
<one-to-one name="p" class="org.hibernate.onetoone.vo.Person"
constrained="true" >
</one-to-one>
</class>
</hibernate-mapping>
(6)测试类.
package org.hibernate.onetoone.dao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Expression;
import org.hibernate.onetoone.vo.Email;
import org.hibernate.onetoone.vo.Person;
public class PersonDao {
private Session session ;
public PersonDao()
{
session = new Configuration().configure().buildSessionFactory()
.openSession() ;
}
public void insert(Person p)
{
session.save(p) ;
session.beginTransaction().commit() ;
session.close() ;
}
public void deletebyid(int id)
{
String hql = "delete from Person as p where p.id=?" ;
Query q = session.createQuery(hql) ;
q.setInteger(0, id) ;
q.executeUpdate() ;
session.beginTransaction().commit() ;
session.close() ;
}
public Person fingbyid(int id)
{
Criteria c = session.createCriteria(Person.class) ;
c.add(Expression.eq("id",id)) ;
List l = c.list() ;
if(l.size()>0)
return (Person)l.get(0) ;
return null ;
}
public void update(Person p)
{
session.update(p) ;
session.beginTransaction().commit() ;
session.close() ;
}
public static void main(String[] args)
{
PersonDao pd = new PersonDao() ;
//级联插入
//如果在Person.hbm.xml中将 <one-to-one name="e" class="org.hibernate.onetoone.vo.Email"
//lazy="false" cascade="all" outer-join="true" > 中的 cascade="all" 去掉的话,那么
//下面的插入将只能将Peson插入到数据库中,而不能同时将Email插入到数据库中, 因为 cascade=all是这是级联关系属性.
//all是设置成所以的级联, 当然还可以设置成其他的,如果只是更新时级联,只是删除时级联,只是插入时级联.
// Person p = new Person () ;
// p.setName("lisi") ;
// p.setAge(10) ;
// Email e = new Email() ;
// e.setEma("[email protected]") ;
// e.setTime("10") ;
// //Person中有Email. Email中有Person.
// e.setP(p) ;
// p.setE(e) ;
// pd.insert(p) ;
//级联更新
//此处的Person对象必须到数据库中查询,而不能通过new出来.因为,数据库中查出来的Person
//中把他的Email带出来,大家可以在fingbyid()在后台输出的sql语句就知道.
// Person p = pd.fingbyid(2) ;
// p.setName("wangwu") ;
// p.setAge(100) ;
// p.getE().setEma("[email protected]") ;
// pd.update(p) ;
//级联删除
//Email.hbm.xml中的one-to-one节点的 constrained="true"
//属性是设置约束, 如果不设置这个约束在在删除Person的时候,不会把Person的Emil删除.
pd.deletebyid(5) ;
}
}
分析:
在Person.hbm.xml中将 <one-to-one name="e" class="org.hibernate.onetoone.vo.Email"
lazy="false" cascade="all" outer-join="true" >中cascade="all" 去掉的话,那么插入函数将只
能将Peson插入到数据库中,而不能同时将Email插入到数据库中, 因为 cascade=all是这是级联关系属性.
所谓级联关系:当主控方执行操作时,关联(被动方)是否同步执行同一操作.当然还可以设置成其他的,如果只是更新时
级联,只是删除时级联,只是插入时级联.
Email.hbm.xml中的one-to-one节点的 constrained="true"属性是设置约束, 如果不设置这个约束在在删除
Person的时候,不会把Person的Emil删除.