论坛首页 Java企业应用论坛

请教两个问题!

浏览 3363 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-04-03  
问题1:
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)
   发表时间: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>
0 请登录后投票
   发表时间: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
0 请登录后投票
   发表时间:2004-04-03  
但是hibernate自己实现了collection,这样做可以吗?
0 请登录后投票
   发表时间: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.
0 请登录后投票
   发表时间:2004-04-03  
!!i see.
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics