-->
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.  [ 5 posts ] 
Author Message
 Post subject: Null value retrieved from lazy loaded proxy object
PostPosted: Wed Jun 04, 2008 10:54 am 
Newbie

Joined: Wed Jun 04, 2008 8:17 am
Posts: 2
Hibernate fails to read value from proxy object (created due to lazy loading) when this object participate in query formation.
I tried to explain the above problem with an example
Let’s consider four entities (sorry about class names I didn’t get any good names just gave random names)
Pt

*/
public class Pt {

private Long ptId;

private String ptName;

private Long ptNumber;

}

public class Tk {

private Long tk_Id;

private Pt pt;
}

Many to one relationship between Tk to Pt


public class Ot {

private Long otId;

private Set<Otm> otms;
}

Ot has set of Otm which has lazy loading option


public class Otm {

private Long otmId;

private Pt pt;

private Ot ot;

}

Otm has many to one relationship betwen Pt and Ot with lazy loading option.

Mapping files

<class name="com.hibernate.test.Pt" table="PT">

<id name="ptId" column="PT_ID" type="java.lang.Long"/>
<property name="ptNumber" column="PT_NBR" type="java.lang.Long"/>
<property name="ptName" column="PT_NAME" type="java.lang.String"/>

</class>



<class name="com.hibernate.test.Tk" table="TK">

<id name="tk_Id" column="TK_ID" type="java.lang.Long"></id>

<many-to-one name="pt" column="PT_NB" class="com.hibernate.test.Pt" property-ref="ptNumber" not-null="true"></many-to-one>
</class>


<class name="com.hibernate.test.Ot" table="OT">
<id name="otId" column="OT_ID" type="java.lang.Long"></id>

<set name="otms" lazy="true" inverse="true" cascade="all">
<key column="OT_ID"></key>
<one-to-many class="com.hibernate.test.Otm" />
</set>


</class>


<class name="com.hibernate.test.Otm" table="OTM">

<id name="otmId" column="OTM_ID" type="java.lang.Long"></id>

<many-to-one name="pt" column="PT_ID"
class="com.hibernate.test.Pt" lazy="proxy"/>

<many-to-one name="ot" column="OT_ID"
class="com.hibernate.test.Ot" lazy="proxy"/>

</class>

All of these have default access as field.

My use case is
I query for Ot > select Otm > get Pt from Otm
Create new instance of Tk set pt to Tk and save Tk
Code

Session session2 = HibernateSessionFactory.getSession();
session2.beginTransaction();

Ot ot2 = (Ot)session2.get(Ot.class, 1002l);
Tk tk = new Tk();
tk.setTk_Id(1l);

tk.setPt(//call the domain method to select one Otm from Ot);
session2.save(tck);
session2.getTransaction().commit();

This gives me a sql exception with message
“Attempt to insert null into a non-nullable column: column: PT_NB table: TK in statement [insert into TEST.TK (PT_NB, TK_ID) values (?, ?)]”

When I debugged the code I could see that Cglib proxy of Pt was set to Tk and while dehydrating this entity, it tried to read ptNumber (it is not primary key) from the proxy. But PojoEntityTuplizer(AbstractEntityTuplizer).getPropertyValue(Object, int) returns null when access is set as field level.

Any suggestions to solve this problem will be helpful. I don’t want to have getter method in my entities as value accesses in entities are done through some domain logic API's.

Thanks in advance
Hari


Last edited by hkg... on Thu Jun 05, 2008 1:32 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 2:40 pm 
Newbie

Joined: Wed Jun 04, 2008 2:29 pm
Posts: 2
I have the same or a similar problem.

Code:
Professor p = student.getClass().getProfessor();

if(p==null)
  //FAIL



It is difficult to diagnose but it happens about 80% of the time when executing the code and it has NEVER happened if I step through the code via the debugger. This leads me to think that hibernate is loading the proxy objects with a separate thread and maybe there is a thread join problem if you are lazy loading through multiple objects.

I have never received a null if I try to lazy load one class
i.e. Class c = student.getClass() is never null.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 1:39 am 
Newbie

Joined: Wed Jun 04, 2008 8:17 am
Posts: 2
jwcyre wrote:
I have the same or a similar problem.

Code:
Professor p = student.getClass().getProfessor();

if(p==null)
  //FAIL



It is difficult to diagnose but it happens about 80% of the time when executing the code and it has NEVER happened if I step through the code via the debugger. This leads me to think that hibernate is loading the proxy objects with a separate thread and maybe there is a thread join problem if you are lazy loading through multiple objects.

I have never received a null if I try to lazy load one class
i.e. Class c = student.getClass() is never null.


I don't think it creates thread but what I found is, while retrieving the value
field.get(proxy) doesn't return any value. Looks like proxy is not created properly.


Top
 Profile  
 
 Post subject: Re: Null value retrieved from lazy loaded proxy object
PostPosted: Thu Jun 05, 2008 8:43 am 
Newbie

Joined: Thu Jun 05, 2008 8:35 am
Posts: 1
hkg... wrote:
Hibernate fails to read value from proxy object (created ............
This gives me a sql exception with message
“Attempt to insert null into a non-nullable column: column: PT_NB table: TK in statement [insert into TEST.TK (PT_NB, TK_ID) values (?, ?)]”

When I debugged the code I could see that Cglib proxy of Pt was set to Tk and while dehydrating this entity, it tried to read ptNumber (it is not primary key) from the proxy. But PojoEntityTuplizer(AbstractEntityTuplizer).getPropertyValue(Object, int) returns null when access is set as field level.

Any suggestions to solve this problem will be helpful. I don’t want to have getter method in my entities as value accesses in entities are done through some domain logic API's.

Thanks in advance
Hari


I have the exact same issue. Hibernate konks off with exception that it cannot insert null value


Top
 Profile  
 
 Post subject: Re: Null value retrieved from lazy loaded proxy object
PostPosted: Thu Mar 21, 2013 5:13 am 
Newbie

Joined: Thu Mar 21, 2013 3:15 am
Posts: 5
We've just encountered the same issue.

UPDATE:
Issue was due to the javassist 3.17.1 library. Updating to Hibernate 4.1.11 with javassist 3.15.0 solved the problem.

See:
https://hibernate.atlassian.net/browse/HHH-7884

Thanks in advance!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.