gpt4 book ai didi

java - hibernate/ Spring : Not-null property references a null or transient value

转载 作者:行者123 更新时间:2023-12-02 07:34:16 26 4
gpt4 key购买 nike

我正在 Java 中使用 Hibernate、Spring 和 GWT 开发一个应用程序。我在 Hibernate 下使用逆向工程(使用 JBoss Developer Studio)从现有 MySQL 数据库中获取 POJO 和配置文件。这是一个非常简单的数据库,只有两个实体:国家和公民。他们之间有OneToMany关系。

代码如下:

应用程序入口点:

...

Country country = new Country();
country.setName("NameOfCountry"+i);
country.setPopulation(10000);

Citizen ctz = new Citizen();
ctz.setName("John");
ctz.setSurname("Smith");


ctz.setCountry(country);
country.getCitizens().add(ctz);

service.saveCitizen(ctz, new AsyncCallback<Boolean>(){

@Override
public void onFailure(Throwable caught) {
System.out.println("Problem saving citizen");

}

@Override
public void onSuccess(Boolean result) {
System.out.println("Citizen successfully saved");

}

});

service.saveCountry(country, new AsyncCallback<Boolean>(){

@Override
public void onFailure(Throwable caught) {
System.out.println("Problem saving country");

}

@Override
public void onSuccess(Boolean result) {
System.out.println("Country successfully saved");

}
});

...

-- 服务向服务器提供简单的 GWT-RPC 调用

服务器上的服务:

@Service("componentService")
public class ComponentServiceImpl implements ComponentService{

@Autowired
private CountryDAO daoCnt;

@Autowired
private CitizenDAO daoCtz;

@Transactional(readOnly=false)
@Override
public boolean saveCitizen(Citizen citizen) {
daoCtz.saveOrUpdate(citizen);
return true;
}

@Transactional(readOnly=false)
@Override
public boolean saveCountry(Country country) {
daoCnt.saveOrUpdate(country);
return true;
}
}

现在 SpringDAO:

CitizenDAO:

@Repository
public class CitizenDAO {

...
public void saveOrUpdate(Citizen citizen){
sessionFactory.getCurrentSession().saveOrUpdate(citizen);
}
...

CountryDAO:

@Repository
public class CountryDAO {

...
public void saveOrUpdate(Country country){
sessionFactory.getCurrentSession().saveOrUpdate(country);
}
...

终于

Citizen.hbm.xml:

<hibernate-mapping>
<class name="sk.jakub.mod.shared.model.Citizen" table="citizen" catalog="modeldb">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="country" class="sk.jakub.mod.shared.model.Country" fetch="select">
<column name="Country_id" not-null="true" />
</many-to-one>
<property name="name" type="string">
<column name="name" length="45" not-null="true" />
</property>
<property name="surname" type="string">
<column name="surname" length="45" not-null="true" />
</property>
</class>
</hibernate-mapping>

Country.hbm.xml:

<hibernate-mapping>
<class name="sk.jakub.mod.shared.model.Country" table="country" catalog="modeldb">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="name" length="45" not-null="true" />
</property>
<property name="population" type="int">
<column name="population" not-null="true" />
</property>
<set name="citizens" table="citizen" inverse="true" lazy="true" fetch="select">
<key>
<column name="Country_id" not-null="true" />
</key>
<one-to-many class="sk.jakub.mod.shared.model.Citizen" />
</set>
</class>
</hibernate-mapping>

我没有列出 Citizen.java 和 Country.java,因为它们只是基本的 POJO(如果需要,我会提供它们)。

当我启动应用程序并且想要将数据保存到数据库中时,出现以下错误:

org.hibernate.PropertyValueException: not-null property references a null or transient value: sk.jakub.mod.shared.model.Citizen.country 

我不知道问题出在哪里。我也尝试用 persist 方法代替 saveOrUpdate 方法。或者也可以更改保存到数据库的顺序。似乎没有任何效果。

非常感谢您的帮助:)如果需要,我可以从我的应用程序中发布更多代码。

编辑:Citizen.java 代码:

public class Citizen implements java.io.Serializable {

private static final long serialVersionUID = -3102863479088406293L;
private Integer id;
private Country country;
private String name;
private String surname;

public Citizen() {
}

public Citizen(Country country, String name, String surname) {
this.country = country;
this.name = name;
this.surname = surname;
}

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public Stat getCountry() {
return this.country;
}

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

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return this.surname;
}

public void setSurname(String surname) {
this.surname = surname;
}
}

国家.java:

public class Country implements java.io.Serializable {

private static final long serialVersionUID = -4085805854508658303L;
private Integer id;
private String name;
private int population;
private Set<Citizen> citizens = new HashSet<Citizen>();

public Country() {
}

public Country(String name, int population) {
this.name = name;
this.population = population;
}

public Country(String name, int population, Set<Citizen> citizens) {
this.name = name;
this.population = population;
this.citizens = citizens;
}

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public int getPopulation() {
return this.population;
}

public void setPopulation(int population) {
this.population = population;
}

public Set<Citizen> getCitizens() {
return this.citizens;
}

public void setCitizens(Set<Citizen> citizens) {
this.citizens = citizens;
}
}

此外,我手动检查了数据库,国家已保存,但公民未保存。

最佳答案

我看到您在创建国家之前先创建公民。此外,两个服务调用都应该在同一事务中,以使整个操作具有原子性。我相信 COUNTRY_ID 似乎是一个自行生成的 ID。因此,一旦创建了国家/地区,您就可以将其附加到公民,但是调用堆栈显示您正在创建一个公民,该公民具有一个没有 id 的国家/地区对象。这只是我的猜测。您可以尝试将这两个调用放在同一事务下,还可以尝试创建一个国家/地区并将该国家/地区实例附加到公民。

关于java - hibernate/ Spring : Not-null property references a null or transient value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12482169/

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