问题描述
运行我的主类 (Runner)
程序时,我得到以下异常:
org.hibernate.id.IdentifierGenerationException:试图从null一对一属性中指定id
:country
我不知道原因,为什么我得到这个异常。
映射xml:
< hibernate-mapping>
< class name =pojo.Countrytable =country>
< id name =countryIDcolumn =c_id>
< generator class =increment/>
< / id>
< property name =countryNamecolumn =c_name/>
< / class>
< class name =pojo.PMtable =pm>
< id name =countryIDcolumn =c_id>
< generator class =foreign>
< param name =property> country< / param>
< / generator>
< / id>
< property name =pmNamecolumn =pm_name/>
< one-to-one class =pojo.Countryname =countryconstrained =true/>
< / class>
< / hibernate-mapping>
POJO类:
国家
public class Country {
private int countryID;
private String countryName;
私人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;
私人字符串pmName;
私人国家国家;
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;
}
公共国家getCountry(){
return country;
}
public void setCountry(Country country){
this.country = country;
}
}
这是试图尝试的类提交事务:
public class Runner {
public static void main(String args []){System。通过out.println( dfdf);
Configuration config = new Configuration()。configure();
SessionFactory sessFact = config.buildSessionFactory();
会话会话= sessFact.openSession();
Transaction trans = session.beginTransaction();
国家c =新国家();
PM pm =新PM();
pm.setPmName(Manmohan Singh);
c.setCountryName(India);
c.setPm(pm);
session.save(c);
trans.commit();
$ b}
SQL创建表:
$ bCREATE TABLE country(c_id INTEGER,c_name TEXT,PRIMARY KEY(c_id ));
CREATE TABLE pm(c_id INTEGER,pm_name TEXT);
解决方案问题在于
国家
变量。在尝试执行某些事务之前,您应该初始化所有的attirbutes。
编辑:在您的Hibernate文件中,您希望从
国家/地区的ID生成PM ID。 code>属性。然而,这个属性从未被初始化过。
< class name =pojo.PMtable =pm>
< id name =countryIDcolumn =c_id>
< generator class =foreign>
< param name =property> country< / param>
< / generator>
< / id>
< property name =pmNamecolumn =pm_name/>
< one-to-one class =pojo.Countryname =countryconstrained =true/>
< / class>
因此,添加
pm.setCountry(c);
添加到您的代码中。As I run my main class
(Runner)
program I get the following exception :org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property: country
I don't know the reason, why am I getting this exception.
The mapping 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 Classes :
Country
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; } }
and this is the class that tries to commit the transaction :
public class Runner { public static void main(String args[]) {System.out.println("dfdf"); Configuration config = new Configuration().configure(); SessionFactory sessFact = config.buildSessionFactory(); Session session = sessFact.openSession(); Transaction trans = session.beginTransaction(); Country c = new Country(); PM pm = new PM(); pm.setPmName("Manmohan Singh"); c.setCountryName("India"); c.setPm(pm); session.save(c); trans.commit(); } }
SQL that created table :
CREATE TABLE country(c_id INTEGER,c_name TEXT,PRIMARY KEY(c_id)); CREATE TABLE pm(c_id INTEGER,pm_name TEXT);
解决方案The problem is the
country
variable. You should initialize all the attirbutes before trying to do some transactions.EDIT: In your Hibernate file, you want to generate the PM ID from the ID of the
country
property. However, this property has never been initialized.<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>
So, add
pm.setCountry(c);
to your code.这篇关于为什么我会得到org.hibernate.id.IdentifierGenerationException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!