gpt4 book ai didi

java - 为什么我会收到 org.hibernate.PropertyAccessException?

转载 作者:行者123 更新时间:2023-12-01 14:23:58 25 4
gpt4 key购买 nike

尝试运行以下程序时:

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

我不知道这是什么原因。我正在尝试在 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);

最佳答案

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

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

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

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

关于java - 为什么我会收到 org.hibernate.PropertyAccessException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17312054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com