hibernate中的双向一对多关联问题
我做了一这样的项目,居然报这样的错误:
ids for this class must be manually assigned before calling save(): onlyfun.caterpillar.TAddress.
哪位好心的大大帮我看下吧,
这是Tuser类:
public class Tuser implements Serializable {
private Integer id;
private String name;
private Integer age;
private Set address=new HashSet();
}
这是TAddress类:
public class TAddress implements Serializable {
private Integer id;
private String address;
private String ripcode;
private String tel;
private Integer uid;
private Tuser use;
}
set和get方法就省略不打了.
-------------------------
这是Tuser.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">
<hibernate-mapping>
<class name="onlyfun.caterpillar.Tuser" table="Tuser" catalog="" dynamic-insert="true" dynamic-update="true">
<id name="id" type="integer" column="id">
<generator class="native" />
</id>
<property name="name" type="string" column="name" />
<property name="age" type="integer" column="age" />
<set name="address" table="TAddress" inverse="true" lazy="false"
cascade="all" sort="unsorted">
<key column="uid" not-null="true"></key>
<one-to-many class="onlyfun.caterpillar.TAddress" />
</set>
</class>
</hibernate-mapping>
这是TAddress.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">
<hibernate-mapping>
<class name="onlyfun.caterpillar.TAddress" table="TAddress"
dynamic-insert="false" dynamic-update="false">
<id name="id" type="integer" column="id"></id>
<property name="address" type="string" column="address" />
<property name="ripcode" type="string" column="ripcode" />
<property name="tel" type="string" column="tel"></property>
<property name="uid" type="integer" column="uid" not null="true"></property>
<many-to-one name="use" class="onlyfun.caterpillar.Tuser"
cascade="none" outer-join="auto" update="false" insert="false"
access="property" not-null="true" column="uid">
</many-to-one>
</class>
</hibernate-mapping>
这是我写的测试类
package onlyfun.caterpillar;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.collection.PersistentBag;
public class TAdd {
public static void main(String[] args) {
Configuration config = new Configuration().configure();
SessionFactory sessFactory = config.buildSessionFactory();
IUserDAO userDAO = new UserDAO(sessFactory);//IUserDAO 是一个接口,UserDAO是实现接口,进行查询数据库细节的
Tuser user=new Tuser();
TAddress tadd=new TAddress();
tadd.setAddress("cccccccc");
tadd.setRipcode("bbbbbbb");
tadd.setTel("ggggggg");
tadd.setUid(new Integer(1));
tadd.setUse(user);
Set set=new HashSet();
set.add(tadd);
user.setAddress(set);
userDAO.insert(user);
}
}
反正就是不能保存,可以查询,真奇怪,