浏览 3363 次
锁定老帖子 主题:请教两个问题!
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2004-04-03
Person parent = session.load(xx,xx); Person child = new Person(); child.setID("haha"); child.setName("child"); parent.addChild(child); session.flush(). 在Person.hbm.xml中id的 <genrator class="assinged"> 1.flush出现数据库与内存不能同步错误。 2.若不指定ID,则提示需要指定ID 这是为什么? 问题2: Person parent = new Person(); parent.setName("parent"); Person child = new Child(); child.setName("child"); parent.addChild(child); 出现NullException,在执行parent.getChildren().add(chld) 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2004-04-03
实体:
public class Person { private long id; private String name; private Person parent; private List children; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List getChildren() { return children; } public void setChildren(List children) { this.children = children; } public Person getParent() { return parent; } public void setParent(Person parent) { this.parent = parent; } } 配置文件 <class name="Person" table="Person"> <id name="id"> <generator class="assigned"/> </id> <property name="name"column="name"/> <bag name="children" lazy="true" inverse="true" cascade="all" outer-join="auto" > <key column="parentID"/> <one-to-many class="Person"/> </bag> <many-to-one name="parent" column="parentID" class="Person"> </many-to-one> </class> |
|
返回顶楼 | |
发表时间:2004-04-03
The childern attribute need to initialize in Person's constructor:
public class Person { ...... public Person(); { childern = new ArrayList();; } ...... } Here is the similar sample I did for your reference: http://forum.iteye.com/viewtopic.php?p=19486 |
|
返回顶楼 | |
发表时间:2004-04-03
但是hibernate自己实现了collection,这样做可以吗?
|
|
返回顶楼 | |
发表时间:2004-04-03
nihongye 写道 但是hibernate自己实现了collection,这样做可以吗?
That's not relate to hibernate, whatever how hibernate work, you can't use a object without initialize it, it's absolutely a java problem. |
|
返回顶楼 | |
发表时间:2004-04-03
!!i see.
|
|
返回顶楼 | |