gpt4 book ai didi

java - Hibernate一对多映射配置

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

我是 hibernate 新手,我正在尝试通过级联加载和更新来实现一对多关系。但是,我的代码总是生成 org.apache.jasper.JasperException: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister 异常。你能看看我的尝试并建议我做错了什么吗?

总体思路是有一个 Company 类,其中包含一组客户。创建新客户实例时,我将他添加到公司并保留所有内容。

实体(仅显示相关部分,或者至少我希望如此)

public class Company implements Serializable {

private static final long serialVersionUID = 146243652;

private String id;
private String name;
private String website;
private Set<Customer> customers;
... getters, setters etc

public class Customer implements Serializable {
private static final long serialVersionUID = 864235654;

private String name;
private String surname;
private String adress;
private String id;
private Company company;

customer.hbm.xml:

<hibernate-mapping>
<class name="wa2.entities.Customer" table="CUSTOMER">
<id column="ID" name="id" type="java.lang.String" />
<property column="NAME" name="name" type="java.lang.String" />
<property column="SURNAME" name="surname" type="java.lang.String" />
<property column="ADRESS" name="adress" type="java.lang.String" />

<many-to-one name="COMPANY" class="wa2.entities.Company">
<column name="COMPANY_ID" not-null="true"></column>
</many-to-one>
</class>
</hibernate-mapping>

company.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="wa2.entities.Company" table="COMPANY">
<id column="ID" name="id" type="java.lang.String" />
<property column="NAME" name="name" type="java.lang.String" />
<property column="WEBSITE" name="website" type="java.lang.String" />

<set name="CUSTOMERS" table="CUSTOMER" fetch="select">
<key>
<column name="CUSTOMER_ID" not-null="true"></column>
</key>
<one-to-many class="wa2.entities.Customer"/>
</set>
</class>
</hibernate-mapping>

保存客户(这在一对多关系实现之前有效,所以我怀疑这段代码有什么问题):

        Customer customer = new Customer();

String name = req.getParameter("name");
customer.setName(name);
String surname = req.getParameter("surname");
customer.setSurname(surname);
String adress = req.getParameter("adress");
customer.setAdress(adress);

String companyID = req.getParameter("companyId");
Company company = repository.loadCompany(companyID);
customer.setCompany(company);

String id = UUID.randomUUID().toString();
customer.setId(id);

repository.saveCustomer(customer);

我希望我没有忘记任何相关部分。如果是这样,请告诉我,我将发布相关代码。非常感谢您的帮助!

编辑:感谢您的回复。看来我把名字的大写写错了。我改变了这一点,现在我无法延迟初始化角色集合:wa2.entities.Company.customers,无法初始化代理 - 无 session

最佳答案

由于这是一个映射异常,我猜它是在 session 工厂创建期间出现的,而不是在保存时出现的?另外,查看整个堆栈跟踪也会有所帮助。您的异常可能是由于映射文件和 bean 之间的属性名称不匹配引起的。请参阅Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister .

在您的代码中,您有 name 属性值

<many-to-one name="COMPANY" class="wa2.entities.Company">
<column name="COMPANY_ID" not-null="true"></column>
</many-to-one>

<set name="CUSTOMERS" table="CUSTOMER" fetch="select">
<key>
<column name="CUSTOMER_ID" not-null="true"></column>
</key>
<one-to-many class="wa2.entities.Customer"/>
</set>

大写,而您的 pojo 中的属性被命名为公司和客户。

关于java - Hibernate一对多映射配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29316485/

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