为什么我得到org.hibernate.PropertyAccessException?

时间:2013-06-26 05:17:08

标签: java hibernate hibernate-mapping

尝试运行以下程序时:

public class Runner {
    public static void main(String args[]) {
        Configuration config = new Configuration().configure();
        SessionFactory sessFact = config.buildSessionFactory();
        Session sess = sessFact.openSession();
        Transaction trans = sess.beginTransaction();

        Person p = new Person();

        p.setPersonName("Suhail");

        Set<String> set = new HashSet<String>();
        set.add("Address-1");
        set.add("Address-2");
        set.add("Address-3");

        p.setAddressSet(set);

        sess.save(p);
        trans.commit();        
    }
}

我得到了:

SEVERE: IllegalArgumentException in class: pojo.Address, getter method
of property: addressID

Exception in thread "main" org.hibernate.PropertyAccessException: 
IllegalArgumentException occurred calling getter of pojo.Address.addressID

我不知道原因。我想在one to manyPerson类之间建立Address关联。

映射xml

<hibernate-mapping>
  <class name="pojo.Person" table="person">
      <id name="personID" column="p_id">
          <generator class="increment" />
      </id>
      <property name="personName" column="p_name" />
      <set name="addressSet" table="address" cascade="all"> 
          <key column="p_id" />
          <one-to-many class="pojo.Address" />
      </set>
  </class>

  <class name="pojo.Address" table="address">
      <id name="addressID" column="a_id">
          <generator class="increment" />
      </id>
      <property name="personAddress" column="p_address" />
  </class>
</hibernate-mapping>

POJO:

public class Person {
    private int personID;
    private String personName;
    private Set addressSet;

    public int getPersonID() {
        return personID;
    }

    public void setPersonID(int personID) {
        this.personID = personID;
    }

    public String getPersonName() {
        return personName;
    }

    public void setPersonName(String personName) {
        this.personName = personName;
    }

    public Set getAddressSet() {
        return addressSet;
    }

    public void setAddressSet(Set addressSet) {
        this.addressSet = addressSet;
    }
}

地址

public class Address {
    private int addressID;
    private String personAddress;

    public int getAddressID() {
        return addressID;
    }

    public void setAddressID(int addressID) {
        this.addressID = addressID;
    }


    public String getPersonAddress() {
        return personAddress;
    }

    public void setPersonAddress(String personAddress) {
        this.personAddress = personAddress;
    }
}

创建表

的SQL
CREATE TABLE person(p_id INTEGER,p_name TEXT,PRIMARY KEY(p_id));
CREATE TABLE address(a_id INTEGER,p_address TEXT);

2 个答案:

答案 0 :(得分:1)

在您的示例中,您将添加到地址集字符串。但是在你的配置中你指定了Address class.So我认为你的问题在这一行:

Set<String> set = new HashSet<String>();
set.add("Address-1");
set.add("Address-2");
set.add("Address-3");

您需要将set更改为Set<Address>并在set:

中添加Address对象
Set<Address> set = new HashSet<>();
Address address = new Address();
address.setPersonAddress("Address-1");
set.add(address);

答案 1 :(得分:0)

如果没有Mapping xml文件,您可以执行以下操作。将@Embeddable放在你的Pojo of

    @Embeddable
    @Entity
    public class Address {
    @Id
    private int addressID;
    private String personAddress;

    public int getAddressID() {
        return addressID;
    }

    public void setAddressID(int addressID) {
        this.addressID = addressID;
    }


    public String getPersonAddress() {
        return personAddress;
    }

    public void setPersonAddress(String personAddress) {
        this.personAddress = personAddress;
    }
}

然后在

   public class Runner {
    public static void main(String args[]) {
        Configuration config = new Configuration().configure();
        SessionFactory sessFact = config.buildSessionFactory();
        Session sess = sessFact.openSession();
        Transaction trans = sess.beginTransaction();

        Person p = new Person();

        p.setPersonName("Suhail");

        @ElementCollection//To inform hibernate to save this in a seperate table
        Set<String> set = new HashSet<String>();
        set.add("Address-1");
        set.add("Address-2");
        set.add("Address-3");

        p.setAddressSet(set);

        sess.save(p);
        trans.commit();        
    }
}

最好使用Annotations,以便我们摆脱编写.hbm.xml映射文件