简体   繁体   English

合并后,Hibernate不会初始化多对一关系

[英]Hibernate does not initialize many-to-one relation after merge

I have the following class: 我有以下课程:

public class Entry{
    private long cardNumber;
    private long companyId;
    private Company company;
    private Long id;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public long getCardNumber() {
        return mediaSerialNumberId;
    }
    public void setCardNumber(long number) {
        this.cardNumber = number;
    }
    public long getCompanyId() {
        return companyId;
    }
    public void setCompanyId(long id) {
        this.companyId = id;
    }
    public Company getCompany() {
        return company;
    }
    public void setCompany(Company company) {
        this.company = company;
    }
}

With the following uni-directional mapping to a Company: 通过以下单向映射到公司:

<hibernate-mapping>
   <class name="foo.bar.Entry" table="RECONSTRUCTION" polymorphism="implicit" lazy="false">
       <id name="id" column="ID" type="long" unsaved-value="null">
           <generator class="sequence">
               <param name="sequence">SEQ_ENTRY</param>
           </generator>
       </id>
       <property name="cardNumber" column="CARDNUMBER" type="long"/>
       <property name="companyId" column="COMPANYID" type="long"/>
       <many-to-one name="company" column="COMPANYID" class="foo.bar.Company" insert="false" update="false" />
   </class>
</hibernate-mapping>

I create a new Entry,set CardNumber=123, CompanyId=3 and and save it with session.merge(). 我创建一个新条目,设置CardNumber = 123,CompanyId = 3并使用session.merge()保存。 The Company with ID=3 exists, I can load it with session.load(). ID = 3的公司存在,我可以用session.load()加载它。 The Company does not have any Hibernate-relation back to Entry. 公司没有任何与休眠有关的回溯关系。

The returned persisted Entry has getCompanyId()==3 but getCompany()==null. 返回的持久条目具有getCompanyId()== 3,但getCompany()== null。

Why isn't the many-to-one relation initialized by Hibernate in the newly persisted object? 为什么Hibernate不在新持久化的对象中初始化多对一关系? If I switch so that the "companyId" attribute is insert/update=false, I can set an existing Company object instead, but then the getCompanyId()==null instead of the ID of the Company. 如果切换为使“ companyId”属性为insert / update = false,则可以设置现有的Company对象,但是可以设置getCompanyId()== null而不是Company的ID。

In the database the COMPANYID column is correctly saved. 在数据库中,正确保存了COMPANYID列。

I am using Hibernate 3.6.10. 我正在使用Hibernate 3.6.10。

You need to refresh the entity: 您需要refresh实体:

Entry entry = new Entry();
entry.setCardNumber(123);
entry.setCompanyId(3);
entry = session.merge(entry);   

session.flush();
session.refresh(entry );

Because the merge copies the given entity state over the one fetched from the database, the null many-to-one association will also be copied over the one fetched from the database. 由于合并将给定实体状态复制到从数据库获取的状态上,因此空多对一关联也将复制到从数据库获取的状态。 A refresh should solve it. refresh应该可以解决它。

A better approach is to simply use the many-to-one association without the extra companyId column: 更好的方法是简单地使用多对一关联,而无需额外的companyId列:

<many-to-one name="company" column="COMPANYID" class="foo.bar.Company"/>

This way, you can even set the company without having to fetch it from DB: 这样,您甚至可以设置公司,而不必从数据库获取它:

Entry entry = new Entry();
entry.setCardNumber(123);

entry.setCompany(new Company());
entry.getCompany().setId(3);
session.persist(session);

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

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