0

我正在使用 Java 中的 Hibernate、Spring 和 GWT 开发应用程序。我在 Hibernate(使用 JBoss Developer Studio)下使用逆向工程从现有的 MySQL 数据库中获取 POJO 和配置文件。这是一个非常简单的数据库,只有两个实体:Country 和 Citizen。它们之间有 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:

公民道:

@Repository
public class CitizenDAO {

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

国家DAO:

@Repository
public class CountryDAO {

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

最后

公民.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>

国家.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 方法,persist 方法。或者也改变保存到数据库的顺序。似乎没有任何效果。

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

编辑: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;
    }
}

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

4

2 回答 2

2

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

于 2012-09-18T19:40:20.493 回答
0

请检查您是否正确实现了 equals、hashcode 和 compareTo(如果适用)方法。我最近遇到了这个问题,并通过正确实施这些问题来解决它。

于 2015-07-13T22:52:29.690 回答