日期:2014-05-16 浏览次数:20566 次
package com.supan.bean; import java.util.HashSet; import java.util.List; import java.util.Set; public class Person { private int id; private String name; private int age; private Set<Address> addresss = new HashSet<Address>(); 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; } public Set<Address> getAddresss() { return addresss; } public void setAddresss(Set<Address> addresss) { this.addresss = addresss; } }
<?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 default-lazy="true" default-access="property" package="com.supan.bean" > <class name="Person" dynamic-update="true" dynamic-insert="true" table="myperson"> <id name="id" column="id" type="integer"> <generator class="native"/> </id> <property name="name" type="string" column="name" length="20"/> <property name="age" type="integer" column="age" length="3"/> <set name="addresss" inverse="true" cascade="all" > <key column="personid"/> <one-to-many class="Address"/> </set> </class> </hibernate-mapping>
package com.supan.bean; public class Address { private int id; private String name; private String info; private String remark; private Person person; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } 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 String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } }
<?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 default-lazy="true" default-access="property" package="com.supan.bean" > <class name="Address" dynamic-update="true" dynamic-insert="true" table="address"> <id name="id" column="id" type="integer"> <generator class="native"/> </id> <property name="name" type="string" column="NAME" length="20"/> <property name="info" type="string" column="INFO" length="30"/> <property name="remark" type="string" column="REMARK" length="30"/> <many-to-one name="person" class="Person" column="personid" not-null="true"></many-to-one> </class> </hibernate-mapping>
/** * 该测试类,结果是,人,和两个地址都保存不成功,事务回滚 */ public void testInsert() { Configuration config = new Configuration().configure(); SessionFactory sf = config.buildSessionFactory(); Session sess = sf.openSession(); Transaction tx = null; try { sess = sf.openSession(); tx = sess.beginTransaction();// 相当于JDBC的connection.setAutoCommit(false); Person person = new Person();