1.映射文件
一
<?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="one2many">
<class name="Tuser" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="addresses" lazy="true" cascade="all" >
<key column="user_id" not-null="true"/>
<one-to-many class="Taddress" />
</set>
</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="one2many">
<class name="Tuser" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="addresses" lazy="true" cascade="all" >
<key column="user_id" not-null="true"/>
<one-to-many class="Taddress" />
</set>
</class>
</hibernate-mapping>
多
<?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="one2many">
<class name="Taddress" table="t_address">
<id name="cid">
<generator class="native"/>
</id>
<property name="tel" />
<property name="zipcode"/>
</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="one2many">
<class name="Taddress" table="t_address">
<id name="cid">
<generator class="native"/>
</id>
<property name="tel" />
<property name="zipcode"/>
</class>
</hibernate-mapping>
测试代码:
public void testInsert(){
Transaction tc = null;
try{
tc = session.beginTransaction();
Tuser user = new Tuser();
user.setName("xiaozhao2");
Set adds = new HashSet();
Taddress add1 = new Taddress();
add1.setTel("123");
add1.setZipcode("897");
Taddress add2 = new Taddress();
add2.setTel("123");
add2.setZipcode("897");
adds.add(add1);
adds.add(add2);
user.setAddresses(adds);
session.save(user);
tc.commit();
}catch(HibernateException e){
tc.rollback();
e.printStackTrace();
}
}
Transaction tc = null;
try{
tc = session.beginTransaction();
Tuser user = new Tuser();
user.setName("xiaozhao2");
Set adds = new HashSet();
Taddress add1 = new Taddress();
add1.setTel("123");
add1.setZipcode("897");
Taddress add2 = new Taddress();
add2.setTel("123");
add2.setZipcode("897");
adds.add(add1);
adds.add(add2);
user.setAddresses(adds);
session.save(user);
tc.commit();
}catch(HibernateException e){
tc.rollback();
e.printStackTrace();
}
}
生成的sql
Hibernate: insert into t_user (name, id) values (?, ?)
Hibernate: insert into t_address (tel, zipcode, user_id, cid) values (?, ?, ?, ?)
Hibernate: insert into t_address (tel, zipcode, user_id, cid) values (?, ?, ?, ?)
Hibernate: update t_address set user_id=? where cid=?
Hibernate: update t_address set user_id=? where cid=?
Hibernate: insert into t_address (tel, zipcode, user_id, cid) values (?, ?, ?, ?)
Hibernate: insert into t_address (tel, zipcode, user_id, cid) values (?, ?, ?, ?)
Hibernate: update t_address set user_id=? where cid=?
Hibernate: update t_address set user_id=? where cid=?
注意:并没有报错,《深入浅出Hibernate》 说是会报错的,
<key column="user_id" not-null="true"/>
将t_address 表的user_id 字段设置为非空了。
书上说 会报错的。我怎么没有报错呢
一对多双向关联
一的一方:
<?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="one2many">
<class name="Tuser" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="addresses" lazy="true" cascade="all" inverse="true" >
<key column="user_id" />
<one-to-many class="Taddress" />
</set>
</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="one2many">
<class name="Tuser" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="addresses" lazy="true" cascade="all" inverse="true" >
<key column="user_id" />
<one-to-many class="Taddress" />
</set>
</class>
</hibernate-mapping>
多的一方:
<?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="one2many">
<class name="Taddress" table="t_address">
<id name="cid">
<generator class="native"/>
</id>
<property name="tel" />
<property name="zipcode"/>
<many-to-one name="user" column="user_id"/>
</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="one2many">
<class name="Taddress" table="t_address">
<id name="cid">
<generator class="native"/>
</id>
<property name="tel" />
<property name="zipcode"/>
<many-to-one name="user" column="user_id"/>
</class>
</hibernate-mapping>
测试代码:
public void testInsert(){
Transaction tc = null;
try{
tc = session.beginTransaction();
Tuser user = new Tuser();
user.setName("xiaozhao2");
Set adds = new HashSet();
Taddress add1 = new Taddress();
add1.setTel("123");
add1.setZipcode("897");
Taddress add2 = new Taddress();
add2.setTel("123");
add2.setZipcode("897");
adds.add(add1);
adds.add(add2);
user.setAddresses(adds);
session.save(user);
tc.commit();
}catch(HibernateException e){
tc.rollback();
e.printStackTrace();
}
}
Transaction tc = null;
try{
tc = session.beginTransaction();
Tuser user = new Tuser();
user.setName("xiaozhao2");
Set adds = new HashSet();
Taddress add1 = new Taddress();
add1.setTel("123");
add1.setZipcode("897");
Taddress add2 = new Taddress();
add2.setTel("123");
add2.setZipcode("897");
adds.add(add1);
adds.add(add2);
user.setAddresses(adds);
session.save(user);
tc.commit();
}catch(HibernateException e){
tc.rollback();
e.printStackTrace();
}
}
发出sql:
Hibernate: insert into t_user (name, id) values (?, ?)
Hibernate: insert into t_address (tel, zipcode, user_id, cid) values (?, ?, ?, ?)
Hibernate: insert into t_address (tel, zipcode, user_id, cid) values (?, ?, ?, ?)
Hibernate: insert into t_address (tel, zipcode, user_id, cid) values (?, ?, ?, ?)
Hibernate: insert into t_address (tel, zipcode, user_id, cid) values (?, ?, ?, ?)
1.如果我把<key column="user_id" /> 修改为<key column="user_id" not-null="true"/>
这时候会把错。 为什么 这时候才报错呢?
发出的sql 都一样。 个人认为:可能是inverse的用法
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/246180,如需转载请自行联系原作者