Topic: hibernate 中 one-to-many 关系

  Print this page

1.hibernate 中 one-to-many 关系 Copy to clipboard
Posted by: newpaul
Posted on: 2003-08-03 17:42

试着用 hibernate 中的one-to-many 关系

Foo 类
public class Foo
{ protected int id;
protected Set bars;

public Foo()
{
}

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;
}

public Set getBars()
{
return bars;
}

public void setBars(Set bars)
{
this.bars = bars;
}
}

Bar 类
public class Bar
{
protected int id;

public Bar()
{
}

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;
}
}

相应的table schema
foo
id

Bar
id foo_id

XX.hbm.xml 为
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-1.1.dtd">

<hibernate-mapping>
<class name="Foo" table="foo">
<id name="id" column="id" type="int">
<generator class="assigned"/>
</id>

<set role="bars" table="bar">
<key column="foo_id"/>
<one-to-many class="Bar"/>
</set>
</class>

<class name="Bar" table="bar">
<id name="id" column="id" type="int">
<generator class="assigned"/>
</id>
</class>
</hibernate-mapping>

但运行的时候 报错
。。。。。。。
1625 [main] INFO mapping.Collection - Mapping class: Foo -> foo
1734 [main] INFO mapping.Collection - Mapping class: Bar -> bar
1750 [main] INFO mapping.Collection - Mapping collection: Foo.null -> bar
。。。。。。
。。。。。
net.sf.hibernate.PropertyNotFoundException: Could not find a getter for null in class Foo
  at net.sf.hibernate.util.ReflectHelper.getGetter(ReflectHelper.java:193)
  at net.sf.hibernate.persister.AbstractEntityPersister.<init>(AbstractEntityPersister.java:563)

这是什么原因阿 我觉得可能是 xml文档不对吧 但不知道错在那里 谁能告诉我

2.Re:hibernate 中 one-to-many 关系 [Re: newpaul] Copy to clipboard
Posted by: bruce
Posted on: 2003-08-04 06:49

写得没有错, 我想你错在写client端程序,要用getter,必须首先有setter.

3.Re:hibernate 中 one-to-many 关系 [Re: newpaul] Copy to clipboard
Posted by: ax3536
Posted on: 2003-08-04 12:44

<set role="bars" table="bar">
<key column="foo_id"/>
<one-to-many class="Bar"/>
</set>

把那个role 改成name!

4.Re:hibernate 中 one-to-many 关系 [Re: newpaul] Copy to clipboard
Posted by: newpaul
Posted on: 2003-08-04 20:17

恩 解决了 谢了


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923