简体   繁体   English

为什么会出现org.hibernate.PropertyAccessException?

[英]Why am I getting org.hibernate.PropertyAccessException?

While trying to run the following program : 尝试运行以下程序时:

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

I am getting : 我正进入(状态 :

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

I don't know the reason for this. 我不知道原因。 I am trying to make one to many association between Person and Address class. 我正在尝试在PersonAddress类之间one to many关联。

mapping xml : 映射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: POJO:

Person

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

Address 地址

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 that created table 创建表的SQL

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

In your example you add to adress set Strings. 在您的示例中,将添加到地址集字符串。 But in your configuration you specify Address class.So I think your problem in this lines: 但是在您的配置中,您指定了地址类。

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

You need to change set to Set<Address> and add Address objects in set: 您需要将set更改为Set<Address>并在set中添加Address对象:

Set<Address> set = new HashSet<>();
Address address = new Address();
address.setPersonAddress("Address-1");
set.add(address);

You can do couple of things without Mapping xml file. 您可以做几件事而无需映射xml文件。 Place @Embeddable on ur Pojo of 将@Embeddable放在ur Pojo上

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

Then on 然后

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

Better to use Annotations so that we get rid of writing .hbm.xml mapping File 最好使用注释,以便我们摆脱编写.hbm.xml映射文件的麻烦

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.hibernate.PropertyAccessException持续存在 - org.hibernate.PropertyAccessException on persist 休眠:org.hibernate.PropertyAccessException:IllegalArgumentException - Hibernate : org.hibernate.PropertyAccessException: IllegalArgumentException Hibernate Criteria - org.hibernate.PropertyAccessException:IllegalArgumentException - Hibernate Criteria - org.hibernate.PropertyAccessException: IllegalArgumentException jComboBox给出org.hibernate.PropertyAccessException错误 - jComboBox giving org.hibernate.PropertyAccessException error 使用ManyToOne获取org.hibernate.PropertyAccessException的JPA Composite键:无法通过反射设置器设置字段值 - JPA Composite key with ManyToOne getting org.hibernate.PropertyAccessException: could not set a field value by reflection setter of org.hibernate.PropertyAccessException:空值已分配给布尔类型的属性 - org.hibernate.PropertyAccessException: Null value was assigned to a property of boolean type org.hibernate.PropertyAccessException-如何从数据库中获取空值? - org.hibernate.PropertyAccessException - How to get a null value out of database? org.hibernate.PropertyAccessException,同时保持ManyToMany关系 - org.hibernate.PropertyAccessException while persisting ManyToMany relationship org.hibernate.PropertyAccessException:无法通过反射getter获取字段值 - org.hibernate.PropertyAccessException: could not get a field value by reflection getter of org.hibernate.PropertyAccessException:使用CGLIB设置异常的属性值 - org.hibernate.PropertyAccessException: exception setting property value with CGLIB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM