论坛首页 Java企业应用论坛

出错: 一对一时主键设为foreign的那一端如何得到主键值?

浏览 4166 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2003-12-18  
我原来的做法是一端用sequence,一端用assigned, 后来看了不少文档, 发现最好是要用 foreign的, 改了之后发现老是会出现下列错误提示:
     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, 这是报错的直接原因, 但是我不知道为什么,
希望能有人帮助解答.
   发表时间:2003-12-18  
贴出完整代码,映射。
0 请登录后投票
   发表时间: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]
0 请登录后投票
   发表时间:2003-12-19  
引用

&lt;generator class="foreign"&gt;
&lt;param name="table"&gt;B_WIFES&lt;/param&gt;
&lt;/generator&gt;

看看文档去。
0 请登录后投票
   发表时间:2003-12-19  
&lt;generator class="foreign"&gt;
&lt;param name="property"&gt;id&lt;/param&gt;
            &lt;/generator&gt;

不好意思阿, 看过了, 改成上面的还是报一样的错阿, 我看过源码了, 这样写的话Hibernate 会把propertyName设为id, 但是
还是不知道为什么会出错阿, 谁能告诉我到底是什么原因阿!
0 请登录后投票
   发表时间:2003-12-19  
难道文档上说得还不够清楚吗?
不要动不动就去看源码,先把文档看懂。
&lt;param name="property"&gt;wife&lt;/param&gt;
0 请登录后投票
论坛首页 Java企业应用版

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