gpt4 book ai didi

java - 为什么我会收到 org.hibernate.id.IdentifierGenerationException?

转载 作者:行者123 更新时间:2023-12-02 06:56:41 27 4
gpt4 key购买 nike

当我运行我的主类 (Runner) 程序时,出现以下异常:

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;
}

}

这是尝试提交事务的类:

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 :

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

最佳答案

问题出在country变量上。在尝试进行某些事务之前,您应该初始化所有属性。

编辑:在 Hibernate 文件中,您希望根据 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); 添加到您的代码中。

关于java - 为什么我会收到 org.hibernate.id.IdentifierGenerationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17253573/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com