0

在尝试运行以下程序时:

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 many之间建立联系。PersonAddress

映射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);
4

2 回答 2

1

在您的示例中,您添加到地址集字符串。但是在您的配置中,您指定了地址类。所以我认为您的问题在于:

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

您需要将集合更改为Set<Address>并在集合中添加地址对象:

Set<Address> set = new HashSet<>();
Address address = new Address();
address.setPersonAddress("Address-1");
set.add(address);
于 2013-06-26T05:25:14.427 回答
0

您可以在不映射 xml 文件的情况下做几件事。将 @Embeddable 放在你的 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;
    }
}

然后上

   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 映射文件

于 2013-06-26T05:32:55.287 回答