hibernate中的双向一对多关联问题

shenruyi1980 2007-08-04 10:17:21
我做了一这样的项目,居然报这样的错误:
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);
}
}
反正就是不能保存,可以查询,真奇怪,



...全文
284 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
TNT_abc 2007-08-09
  • 打赏
  • 举报
回复
<class name="onlyfun.caterpillar.TAddress" table="TAddress"
dynamic-insert="false" dynamic-update="false">
<id name="id" type="integer" column="id"></id>
没有指定ID生成方式


<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>
set中的<key column="uid" not-null="true"></key>,这个column到底指的是哪个id啊

<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>
还有<many-to-one>中的column又到底指的是哪个表的id还是bean中的 id啊,我一直没有搞明白这2个问题

这两个column都是指定的address表中的外键
freedom2001 2007-08-08
  • 打赏
  • 举报
回复
<class name="onlyfun.caterpillar.TAddress" table="TAddress"
dynamic-insert="false" dynamic-update="false">
<id name="id" type="integer" column="id"></id>
这没指定ID生成方式
TAddress tadd=new TAddress();
tadd.setAddress("cccccccc");
tadd.setRipcode("bbbbbbb");
tadd.setTel("ggggggg");
tadd.setUid(new Integer(1));这里面设置的也没有ID,
你加上tadd.setId(new Integer(1));估计就可以了,
ids for this class must be manually assigned before calling save(): 其中assigned就是用户在程序里自己设置ID的主键生成方式
tuke0001 2007-08-08
  • 打赏
  • 举报
回复
TAddress 没有id generator
yztommyhc 2007-08-08
  • 打赏
  • 举报
回复
<class name="onlyfun.caterpillar.TAddress" table="TAddress"
dynamic-insert="false" dynamic-update="false">
<id name="id" type="integer" column="id"></id>
他的id你没指定生成方式吧?

shenruyi1980 2007-08-04
  • 打赏
  • 举报
回复
<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>
set中的<key column="uid" not-null="true"></key>,这个column到底指的是哪个id啊

<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>
还有<many-to-one>中的column又到底指的是哪个表的id还是bean中的 id啊,我一直没有搞明白这2个问题
shenruyi1980 2007-08-04
  • 打赏
  • 举报
回复
id有啊,照你说的做了还是报一样的错误,TAddress中的uid是外键
yztommyhc 2007-08-04
  • 打赏
  • 举报
回复
id有么?

<generator class="increment" />看看。

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧