2013-06-22 54 views
0

当我跑我主类(Runner)程序,我得到以下异常:为什么我会得到org.hibernate.id.IdentifierGenerationException?

org.hibernate.id.IdentifierGenerationException: attempted to assign id 
from null one-to-one property: country 

我不知道原因,为什么我得到这个例外。

的XML映射:

<hibernate-mapping> 
    <class name="pojo.Country" table="country"> 
     <id name="countryID" column="c_id"> 
      <generator class="increment" /> 
     </id> 
     <property name="countryName" column="c_name" /> 
     <one-to-one class="pojo.PM" name="pm" cascade="all" /> 
    </class> 

    <class name="pojo.PM" table="pm"> 
     <id name="countryID" column="c_id"> 
      <generator class="foreign"> 
       <param name="property">country</param> 
      </generator> 
     </id> 
     <property name="pmName" column="pm_name" /> 
     <one-to-one class="pojo.Country" name="country" constrained="true" /> 
    </class> 
</hibernate-mapping> 

POJO类:

国家

public class Country { 
    private int countryID; 
    private String countryName; 
    private PM pm; 

    public PM getPm() { 
     return pm; 
    } 

    public void setPm(PM pm) { 
     this.pm = pm; 
    } 

    public int getCountryID() { 
     return countryID; 
    } 

    public void setCountryID(int countryID) { 
     this.countryID = countryID; 
    } 

    public String getCountryName() { 
     return countryName; 
    } 

    public void setCountryName(String countryName) { 
     this.countryName = countryName; 
    } 
} 

PM

public class PM { 
    private int countryID; 
    private String pmName; 
    private Country country; 

    public int getCountryID() { 
     return countryID; 
    } 

    public void setCountryID(int countryID) { 
     this.countryID = countryID; 
    } 

    public String getPmName() { 
     return pmName; 
    } 

    public void setPmName(String pmName) { 
     this.pmName = pmName; 
    } 

    public Country getCountry() { 
     return country; 
    } 

    public void setCountry(Country country) { 
     this.country = country; 
    } 

} 

,这是一种尝试提交事务类:

SQL创建表:

CREATE TABLE country(c_id INTEGER,c_name TEXT,PRIMARY KEY(c_id)); 
CREATE TABLE pm(c_id INTEGER,pm_name TEXT); 

回答

2

的问题是country变量。在尝试执行某些事务之前,您应该初始化所有的attirbutes。

编辑:在您的休眠文件中,您想要从country属性的ID生成PM ID。但是,此属性从未初始化。

<class name="pojo.PM" table="pm"> 
     <id name="countryID" column="c_id"> 
      <generator class="foreign"> 
       <param name="property">country</param> 
      </generator> 
     </id> 
     <property name="pmName" column="pm_name" /> 
     <one-to-one class="pojo.Country" name="country" constrained="true" /> 
    </class> 

因此,将pm.setCountry(c);添加到您的代码中。

+1

我无法删除异常。请你帮忙 –

+0

好的......谢谢! –

相关问题