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);
}
}
反正就是不能保存,可以查询,真奇怪,



...全文
314 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" />看看。
依赖对象(Dependent objects) 组件(Component)是一个被包含的对象,在持久化的过程,它被当作值类型,而并非一个实体的引用。在这篇文档,组件这一术语指的是面向对象的合成概念(而并不是系统构架层次上的组件的概念)。举个例子, 你对人(Person)这个概念可以像下面这样来建模: public class Person { private java.util.Date birthday; private Name name; private String key; public String getKey() { return key; } private void setKey(String key) { this.key=key; } public java.util.Date getBirthday() { return birthday; } public void setBirthday(java.util.Date birthday) { this.birthday = birthday; } public Name getName() { return name; } public void setName(Name name) { this.name = name; } ...... ...... } public class Name { char initial; String first; String last; public String getFirst() { return first; } void setFirst(String first) { this.first = first; } public String getLast() { return last; } void setLast(String last) { this.last = last; } public char getInitial() { return initial; } void setInitial(char initial) { this.initial = initial; } } 在持久化的过程,姓名(Name)可以作为人(Person)的一个组件。需要注意的是:你应该为姓名的持久化属性定义getter和setter方法,但是你不需要实现任何的接口或申明标识符字段。 以下是这个例子的Hibernate映射文件: <!-- class attribute optional --> 人员(Person)表将包括pid, birthday, initial, first和 last等字段。 就像所有的值类型一样, 组件不支持共享引用。 换句话说,两个人可能重名,但是两个Person对象应该包含两个独立的Name对象,只不过这两个Name对象具有“同样”的值。 组件的值可以为空,其定义如下。 每当Hibernate重新加载一个包含组件的对象,如果该组件的所有字段为空,Hibernate将假定整个组件为空。 在大多数情况下,这样假定应该是没有问题的。 组件的属性可以是任意一种Hibernate类型(包括集合, 多对多关联, 以及其它组件等等)。嵌套组件不应该被当作一种特殊的应用(Nested components should not be
HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值类型的集合 1.3.5. 双向关联 1.3.6. 使双向连起来 1.4. 第三部分 - EventManager web应用程序 1.4.1. 编写基本的servlet 1.4.2. 处理与渲染 1.4.3. 部署与测试 1.5. 总结 2. 体系结构(Architecture) 2.1. 概况(Overview) 2.2. 实例状态 2.3. JMX整合 2.4. 对JCA的支持 2.5. 上下文相关的(Contextual)Session 3. 配置 3.1. 可编程的配置方式 3.2. 获得SessionFactory 3.3. JDBC连接 3.4. 可选的配置属性 3.4.1. SQL方言 3.4.2. 外连接抓取(Outer Join Fetching) 3.4.3. 二进制流 (Binary Streams) 3.4.4. 二级缓存与查询缓存 3.4.5. 查询语言的替换 3.4.6. Hibernate的统计(statistics)机制 3.5. 日志 3.6. 实现NamingStrategy 3.7. XML配置文件 3.8. J2EE应用程序服务器的集成 3.8.1. 事务策略配置 3.8.2. JNDI绑定的SessionFactory 3.8.3. 在JTA环境下使用Current Session context (当前session上下文)管理 3.8.4. JMX部署 4. 持久化类(Persistent Classes) 4.1. 一个简单的POJO例子 4.1.1. 实现一个默认的(即无参数的)构造方法(constructor) 4.1.2. 提供一个标识属性(identifier property)(可选) 4.1.3. 使用非final的类 (可选) 4.1.4. 为持久化字段声明访问器(accessors)和是否可变的标志(mutators)(可选) 4.2. 实现继承(Inheritance) 4.3. 实现equals()和hashCode() 4.4. 动态模型(Dynamic models) 4.5. 元组片断映射(Tuplizers) 5. 对象/关系数据库映射基础(Basic O/R Mapping) 5.1. 映射定义(Mapping declaration) 5.1.1. Doctype 5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-one) 5.1.11. 一对一 5.1.12. 自然ID(natural-id) 5.1.13. 组件(component), 动态组件(dynamic-component) 5.1.14. properties 5.1.15. 子类(subclass) 5.1.16. 连接的子类(joined-subclass) 5.1.17. 联合子类(union-subclass) 5.1.18. 连接(join) 5.1.19. 键(key) 5.1.20. 字段和规则元素(column and formula elements) 5.1.21. 引用(import) 5.1.22. any 5.2. Hibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本值类型 5.2.3. 自定义值类型 5

67,540

社区成员

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

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