浏览 4167 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2003-12-18
java.lang.NullPointerException at net.sf.hibernate.persister.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:675) at net.sf.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:33) at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:614) at test.Test.createHusband(Test.java:86) at test.Test.main(Test.java:48)[/color] 另外配置文件如下: <id name="id" column="husband_id" type="int" unsaved-value="0"> <generator class="foreign"> <param name="table">B_WIFES</param> </generator> </id> <one-to-one name="wife" class="test.B_wife" constrained="true" cascade="all" outer-join="false"/> /************************************************************/ <id name="id" column="wife_id" type="int"> <generator class="sequence"/> </id> <property name="name"/> 出错代码如下: B_husband husband = new B_husband(); husband.setName(name); husband.setChilds( new ArrayList() ); husband.setWife( wife ); Session session = _sessions.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save( husband ); ////出错 tx.commit(); }[color=red] 我跟踪源码时发现, ForeignGenerator.generate(ForeignGenerator.java:33)和.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:675) 方法中用到的propertyName 都是null, 这是报错的直接原因, 但是我不知道为什么, 希望能有人帮助解答. 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2003-12-18
贴出完整代码,映射。
|
|
返回顶楼 | |
发表时间:2003-12-19
代码:
public class B_husband { private int id; private String name; private List childs; private B_wife wife;} /***********************************/ public class B_wife { private int id; private String name;} 配置: <hibernate-mapping> <class name="test.B_wife" table="B_WIFES"> <id name="id" column="wife_id" type="int"> <generator class="sequence"/> </id> <property name="name"/> </class> </hibernate-mapping> /****************************************/ <hibernate-mapping> <class name="test.B_husband" table="B_HUSBANDS" proxy="test.B_husband"> <id name="id" column="husband_id" type="int" unsaved-value="0"> <generator class="foreign"> <param name="table">B_WIFES</param> </generator> </id> <property name="name" column="name" not-null="true" unique="false"/> <one-to-one name="wife" class="test.B_wife" constrained="true" cascade="all" outer-join="false"/> </class> </hibernate-mapping> 出错处理的代码: public B_husband createHusband(String name, test.B_wife wife) throws HibernateException{ B_husband husband = new B_husband(); husband.setName(name); husband.setChilds( new ArrayList() ); husband.setWife( wife ); //wife.setHusband( husband ); Session session = _sessions.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save( husband ); //////////////出错 tx.commit(); } catch (HibernateException he) { if (tx!=null) tx.rollback(); throw he; } finally { session.close(); } return husband; } [color=brown][/color] |
|
返回顶楼 | |
发表时间:2003-12-19
引用 <generator class="foreign"> <param name="table">B_WIFES</param> </generator> 看看文档去。 |
|
返回顶楼 | |
发表时间:2003-12-19
<generator class="foreign">
<param name="property">id</param> </generator> 不好意思阿, 看过了, 改成上面的还是报一样的错阿, 我看过源码了, 这样写的话Hibernate 会把propertyName设为id, 但是 还是不知道为什么会出错阿, 谁能告诉我到底是什么原因阿! |
|
返回顶楼 | |
发表时间:2003-12-19
难道文档上说得还不够清楚吗?
不要动不动就去看源码,先把文档看懂。 <param name="property">wife</param> |
|
返回顶楼 | |