2个 相关联的配置文件分别为:
Tuser.hbm.xml
<?xml version=
"1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.randy.connection">
<class name="Tuser" table="t_user">
<id name="id">
<generator class="foreign">
<param name="property">passport</param>
</generator>
</id>
<property name="age" />
<property name="name"/>
<one-to-one name="passport" cascade="all" class="Tpassport" constrained="true"/>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.randy.connection">
<class name="Tuser" table="t_user">
<id name="id">
<generator class="foreign">
<param name="property">passport</param>
</generator>
</id>
<property name="age" />
<property name="name"/>
<one-to-one name="passport" cascade="all" class="Tpassport" constrained="true"/>
</class>
</hibernate-mapping>
Tpassport.hbm.xml
<?xml version=
"1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.randy.connection">
<class name="Tpassport" table="t_passport">
<id name="id">
<generator class="native" />
</id>
<property name="serial" />
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.randy.connection">
<class name="Tpassport" table="t_passport">
<id name="id">
<generator class="native" />
</id>
<property name="serial" />
</class>
</hibernate-mapping>
执行的代码
public
void testSelect(){
String hql = "from Item";
List userlist = session.createQuery(hql).list();
}
String hql = "from Item";
List userlist = session.createQuery(hql).list();
}
输出的sql
select tuser0_.id as id0_0_,
tuser0_.age as age0_0_,
tuser0_.name as name0_0_
from t_user tuser0_
where tuser0_.id = ?
----------------------------------------
select tpassport0_.id as id1_0_, tpassport0_.serial as serial1_0_
from t_passport tpassport0_
where tpassport0_.id = ?
tuser0_.age as age0_0_,
tuser0_.name as name0_0_
from t_user tuser0_
where tuser0_.id = ?
----------------------------------------
select tpassport0_.id as id1_0_, tpassport0_.serial as serial1_0_
from t_passport tpassport0_
where tpassport0_.id = ?
共2条SQL 语句
实验:
当把 Tuser.hbm.xml文件中<one-to-one>标签中的contained="true"去掉了
输出的sql:
select tuser0_.id as id0_1_,
tuser0_.age as age0_1_,
tuser0_.name as name0_1_,
tpassport1_.id as id1_0_,
tpassport1_.serial as serial1_0_
from t_user tuser0_
left outer join t_passport tpassport1_ on tuser0_.id = tpassport1_.id
where tuser0_.id = ?
tuser0_.age as age0_1_,
tuser0_.name as name0_1_,
tpassport1_.id as id1_0_,
tpassport1_.serial as serial1_0_
from t_user tuser0_
left outer join t_passport tpassport1_ on tuser0_.id = tpassport1_.id
where tuser0_.id = ?
一条sql语句。
测试代码:
public
void testDelete(){
Transaction tc = null;
try{
tc = session.beginTransaction();
Tuser user =(Tuser)session.load(Tuser. class, 5);
//Tuser user = new Tuser();
// user.setId(3);
session.delete(user);
tc.commit();
} catch(HibernateException e){
tc.rollback();
e.printStackTrace();
}
}
Transaction tc = null;
try{
tc = session.beginTransaction();
Tuser user =(Tuser)session.load(Tuser. class, 5);
//Tuser user = new Tuser();
// user.setId(3);
session.delete(user);
tc.commit();
} catch(HibernateException e){
tc.rollback();
e.printStackTrace();
}
}
这里的load() 可以替换为get()
1.当Tuser.hbm.xml 中one-to-one 标签中的casecade="true"的时候
输出 2条sql
delete from t_user where id=?
delete from t_passport where id=?
2.当Tuser.hbm.xml 中one-to-one 标签中的casecade="false"的时候
只输出一条sql
delete from t_user where id=?
测试代码:
public
void testDelete(){
Transaction tc = null;
try{
tc = session.beginTransaction();
Tuser user = new Tuser();
user.setId(3);
session.delete(user);
tc.commit();
} catch(HibernateException e){
tc.rollback();
e.printStackTrace();
}
}
Transaction tc = null;
try{
tc = session.beginTransaction();
Tuser user = new Tuser();
user.setId(3);
session.delete(user);
tc.commit();
} catch(HibernateException e){
tc.rollback();
e.printStackTrace();
}
}
1.当Tuser.hbm.xml 中one-to-one 标签中的casecade="false"的时候
输出一条sql
delete from t_user where id=?
2.当Tuser.hbm.xml 中one-to-one 标签中的casecade="false"的时候
delete from t_user where id=?
太奇怪了,失效了casecade 属性没用了。
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/245945,如需转载请自行联系原作者