-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problems with many-to-one unidirectional association
PostPosted: Sun Mar 01, 2009 7:26 am 
Newbie

Joined: Sun Mar 01, 2009 6:29 am
Posts: 3
I have the following problem. I cannot get id of the associated entity, its always null, but the toString method inserts correct id.
when I run hibernate with debug log level it maps correct parameters to queries when fetching the associated entity.
I get equal results when the bytecode optimization is true and false.
Where I am wrong? Some issues with proxying?
Please, help.

I've prepared the simple showcase.

The persistence object are as follows

Code:
public class FirstObject {

    private SecondObject secondObject;

    private Integer id;

    public SecondObject getSecondObject() {
        return secondObject;
    }

    public void setSecondObject(SecondObject secondObject) {
        this.secondObject = secondObject;
    }   

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String toString() {     
        return HibernateProxyHelper.getClassWithoutInitializingProxy(this)
                .getSimpleName()
                + "-" + getId();
    }
}


Code:
public class SecondObject {

    private Integer id;

    public final Integer getId() {
        return id;
    }
   
    public final void setId(Integer id) {
        this.id = id;
    }

    public String toString() {
        return HibernateProxyHelper.getClassWithoutInitializingProxy(this)
                .getSimpleName()
                + "-" + getId();
    }

}


The mappings

Code:
<hibernate-mapping default-access="field">

   <class name="FirstObject">

      <id name="id" type="integer">
         <column name="id" />
         <generator class="native" />
      </id>

      <many-to-one name="secondObject"
         class="SecondObject" column="so_id" unique="true"/>
   </class>

   <query name="getFirstObjectById.request">
      SELECT fo FROM FirstObject fo WHERE fo.id=:id
   </query>
   <query name="getFirstObjectList.request">
      SELECT fo FROM FirstObject fo
   </query>
   <query name="getFirstObjectCount.request">
      SELECT count(*) FROM FirstObject
   </query>
</hibernate-mapping>


Code:
<hibernate-mapping default-access="property">
   <class name="SecondObject">

      <id name="id" type="integer">
         <column name="id" />
         <generator class="native" />
      </id>
   </class>

   <query name="getSecondObjectById.request">
      SELECT so FROM SecondObject so WHERE so.id=:id
   </query>
   <query name="getSecondObjectList.request">
      SELECT so FROM SecondObject so
   </query>
   <query name="getSecondObjectCount.request">
      SELECT count(*) FROM FirstObject
   </query>
</hibernate-mapping>


The piece of code to obtain data

Code:
....
                    for (FirstObject first : (List<FirstObject>) session
                            .getNamedQuery("getFirstObjectList.request").list()) {
                        System.out.println("fid=" + first.getId());
                        System.out.println("sid="
                                + first.getSecondObject().getId());

                        System.out.println("first=" + first.toString());
                        System.out.println("second="
                                + first.getSecondObject().toString());
                       
                        System.out.println("sid="
                                + first.getSecondObject().getId());
                    }

....


The output

Code:
.....
Hibernate: select firstobjec0_.id as id0_, firstobjec0_.so_id as so2_0_ from FirstObject firstobjec0_
fid=1
sid=null
first=FirstObject-1
Hibernate: select secondobje0_.id as id1_0_ from SecondObject secondobje0_ where secondobje0_.id=?
second=SecondObject-1
sid=null
.....


The example uses hibernate 3.2.6, mysql-connector-5.1.6, mysql-5.1.31 under WinXp

Do you have any thoughts on this?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 01, 2009 8:47 am 
Newbie

Joined: Sun Mar 01, 2009 6:29 am
Posts: 3
I've just removed "final" from the getId() method and all begin to work fine...


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

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.