我正在開發一個應用程序,使用Java中的Hibernate,Spring和GWT。我在Hibernate(使用JBoss Developer Studio)下使用反向工程從現有的MySQL數據庫獲取POJO和配置文件。這是非常簡單的數據庫,只有兩個實體:國家和公民。他們之間有OneToMany關係。Hibernate/Spring:非空屬性引用空值或瞬態值
下面是代碼:
應用程序入口點:
- 服務提供了簡單的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;
}
}
現在SpringDAOs:
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
我想不通問題出在哪裏。我正在嘗試而不是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;
}
}
Country.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;
}
}
此外,我手動檢查數據庫和國家被保存,但公民不是。
好像您正在嘗試保存具有空國家對象的Citizen POJO。你能否包括國家和公民的代碼? –
源碼參考資料:) – Reshi