-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate does not initialize many-to-one relation
PostPosted: Mon Jan 12, 2015 7:40 am 
Newbie

Joined: Mon Jan 12, 2015 7:36 am
Posts: 1
I have the following class:

Code:
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:
Code:
<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(). The Company with ID=3 exists, I can load it with session.load(). The Company does not have any Hibernate-relation back to Entry.

The returned persisted Entry has getCompanyId()==3 but getCompany()==null.

Why isn't the many-to-one relation initialized by Hibernate in the newly persisted object? 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.

In the database the COMPANYID column is correctly saved.

I am using Hibernate 3.6.10.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.