简体   繁体   English

休眠一对多映射配置

[英]Hibernate one-to-many mapping configuration

I am new to hibernate and I am trying to implement one-to-many relationship with cascade loading and updating. 我是新来的冬眠者,我正在尝试通过级联加载和更新实现一对多关系。 However, my code always generates org.apache.jasper.JasperException: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister exception. 但是,我的代码总是生成org.apache.jasper.JasperException: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister异常的org.apache.jasper.JasperException: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister Could you take a look on my attempt and suggest what am I doing wrong? 您能看看我的尝试,并建议我做错了什么吗?

The general idea is that there is a Company class which contains set of customers. 总体思路是,存在一个Company类,其中包含一组客户。 When creating new instance of customer, I add him to a company and persist everything. 创建新的客户实例时,我将其添加到公司中并保留所有内容。

Entities (only displaying relevant parts, or at least I hope so) 实体(仅显示相关部分,或者至少希望如此)

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: 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: 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>

Saving customer (this worked before the implementation of one-to-many relationship so I doubt that there is anything wrong with this code): 节省客户(这在实现一对多关系之前有效,因此我怀疑这段代码有什么问题):

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

I hope that I did not forget any relevant part. 我希望我不要忘记任何相关部分。 If so, please tell me and I will post the relevant code. 如果是这样,请告诉我,我将发布相关代码。 Thank you very much for any help! 非常感谢您的帮助!

EDIT: Thanks for the responses. 编辑:感谢您的答复。 It seems that I wrote the names wrongly in capitals. 看来我在大写字母中写错了名字。 I changed that and now I am getting failed to lazily initialize a collection of role: wa2.entities.Company.customers, could not initialize proxy - no Session 我改变了这一点,现在我failed to lazily initialize a collection of role: wa2.entities.Company.customers, could not initialize proxy - no Session

since this is an mapping exception, i guess it comes up during session factory creation, and not when saving? 由于这是一个映射异常,我想它是在会话工厂创建期间出现的,而不是在保存时出现的? also, seeing the whole stacktrace could help. 同样,查看整个stacktrace也会有所帮助。 your exception can be caused by a mismatch of property names between mapping file and beans. 您的异常可能是由于映射文件和Bean之间的属性名称不匹配引起的。 see Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister . 请参阅无法获取org.hibernate.persister.entity.SingleTableEntityPersister的构造方法

in your code you have the name attribute values in 在您的代码中,您具有name属性值

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

and

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

in uppercase, while the properties in your pojos are named company and customer. 大写字母,而pojos中的属性分别命名为company和customer。

One principle in hibernate, that is often stated, but even more often overseen, is, that you need to keep a clean object tree, or the database will not be kept in sync properly. 休眠状态中经常提到的一个原则,但是经常被监督的一个原则是,您需要保持干净的对象树,否则数据库将无法正确同步。

I think this causes your problem: You need to keep the tree clean. 我认为这会引起您的问题:您需要保持树的清洁。 But you don't: You add the customer to the company on the customer side, but you do not add the customer to the company's customer set. 但是您没有:在客户方将客户添加到公司中,但没有将客户添加到公司的客户集中。

In your code, you have added a vice-versa link between customer and company. 在您的代码中,您已经在客户和公司之间添加了反之亦然的链接。 I would add an inverse to the link, for this is what you want. 我会在链接中添加一个逆,因为这就是您想要的。

Second: If you start with hibernate now, really think about the annotation approach. 第二:如果您现在从休眠入手,请真正考虑一下注释方法。 If you inherit legacy apps, stay with the XML, but if you start out fresh ... 如果您继承了旧版应用程序,请继续使用XML,但是如果您重新开始使用...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM