-->
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.  [ 7 posts ] 
Author Message
 Post subject: CGLIB proxy & lazy loading problems in version 3
PostPosted: Mon Aug 08, 2005 9:06 am 
Newbie

Joined: Mon Aug 08, 2005 6:41 am
Posts: 4
Hello,

I have been a long time trying to solve a problem that appeared when upgarding from hibernate 2.1 to version 3. I have an object of type AgencyOperator, inheriting from User, which contains a collection of BranchOffice's which in turn contain User's, as can be seen in the mapping pasted below.

With hibernate 2.1 I could instantiate an AgencyOperator and obtain from it the BranchOffice it is registered with, and from this one, the Agency it belongs to, like:

AgencyOperator.getBranchOffice().getAgency().

With hibernate 3 I experience problems with lazy loading. The BranchOffice property of AgencyOperator is a CGLIB proxy, that after examination with the debugger reveals to contain the correct object in its 'target' property.

However, when I investigate the content of the Agency property inside this BranchOffice object, I see a CGLIB proxy that contains a null reference in the 'target' property. The call to

AgencyOperator.getBranchOffice().getAgency(), then, gives me a null reference.

This is always before closing the HibernateSession.

After browsing documentation and googling, I cannot find the problem. Can anyone provide some help?

Thanks a lot in advance.

Hibernate version: 3.0.5
Name and version of the database you are using: Mysql 4.1
Mapping documents:

<class name="es.parsec.parsys.user.ParsysUser">
<id name="code" type="long" column="code" unsaved-value="null">
<generator class="native" />
</id>
<property name="name" type="string" />
<property name="password" type="string" />

<joined-subclass name="es.parsec.parsys.user.AgencyOperator" table="AgencyOperator">
<key column="code"/>
<many-to-one name="branchOffice" class="es.parsec.parsys.agency.BranchOffice" column="branchOffice"/>
</joined-subclass>
</class>

<class name="es.parsec.parsys.agency.BranchOffice">
<id name="code" type="long" column="code" unsaved-value="null">
<generator class="native"/>
</id>
<property name="name" type="string" length="40"/>
<many-to-one name="agency" column="agency" class="es.parsec.parsys.agency.Agency"/>
<set name="branchOperators" inverse="true" lazy="true" order-by="code" cascade="all">
<key column="branchOffice"/>
<one-to-many class="es.parsec.parsys.user.AgencyOperator"/>
</set>
</class>

<class name="es.parsec.parsys.agency.Agency">
<id name="code" type="long" column="code" unsaved-value="null">
<generator class="native"/>
</id>
<property name="name" type="string"/>
<set name="branchOffices" inverse="true" lazy="true" order-by="code" cascade="all">
<key column="agency"/>
<one-to-many class="es.parsec.parsys.agency.BranchOffice"/>
</set>
</class>

_________________
Javier Ramos Diaz


Top
 Profile  
 
 Post subject: Re: CGLIB proxy & lazy loading problems in version 3
PostPosted: Mon Aug 08, 2005 9:11 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
A couple of things...

You should never receive a lazy loading exception if the original session is still open and your object hasn't been evicted.

I sometimes experience strange behavior with Eclipse debugger where it appears the object is empty through the proxy but when I displayed the value through logging statements, it appeared correctly.

Can you show an example of your code ?

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: CGLIB proxy & lazy loading problems in version 3
PostPosted: Mon Aug 08, 2005 9:43 am 
Newbie

Joined: Mon Aug 08, 2005 6:41 am
Posts: 4
pksiv wrote:
A couple of things...

You should never receive a lazy loading exception if the original session is still open and your object hasn't been evicted.

I sometimes experience strange behavior with Eclipse debugger where it appears the object is empty through the proxy but when I displayed the value through logging statements, it appeared correctly.

Can you show an example of your code ?


Hi Preston,

I do not receive a lazy loading exception, I just get a null pointer reference wheng doing

Code:
AgencyOperator.getBranchOffice().getAgency()


Then I get NullPointerException when trying to read some property of the Agency.

Here is the code of my classes:

Code:

public class Agency
{   

private Long code;
private String name;   
private Set branchOffices = new LinkedHashSet();
   
Agency()
{
   
}

private Set getBranchOffices()
{
  return branchOffices;
}

private void setBranchOffices(Set branchOffices)
{
  this.branchOffices = branchOffices;
}

public Long getCode()
{
  return code;
}

private void setCode(Long code)
{
  this.code = code;
}

public String getName()
{
  return name;
}

public void setName(String name)
{
  this.name = name;
}
   
}


public class BranchOffice
{

private Long code;
private String name;
private Set branchOperators = new LinkedHashSet();
   
BranchOffice()
{
   
}
   
public Agency getAgency()
{
  return agency;
}

public void setAgency(Agency agency)
{
  this.agency = agency;
}

public Long getCode()
{
  return code;
}

private void setCode(Long code)
{
  this.code = code;
}

public String getName()
{
  return name;
}

public void setName(String name)
{
  this.name = name;
}

private Set getBranchOperators()
{
  return branchOperators;
}

private void setBranchOperators(Set branchOperators)
{
  this.branchOperators = branchOperators;
}

}

public abstract class ParsysUser implements Principal
{
   
private Long code;
private String name;
private String password;

protected ParsysUser()
{
  super();
}

public String getName()
{
  return name;
}

public Long getCode()
{
  return code;
}

private void setCode(Long code)
{
  this.code = code;
}

public String getPassword()
{
  return password;
}

public void setPassword(String password)
{
  this.password = password;
}

public void setName(String name)
{
this.name = name;
}
   
}

public class AgencyOperator extends ParsysUser
{

protected AgencyOperator()
{
  super();
}   
   
private BranchOffice branchOffice;   
   
public BranchOffice getBranchOffice()
{
  return branchOffice;
}

public void setBranchOffice(BranchOffice branchOffice)
{
  this.branchOffice = branchOffice;
}

}



Thanks for your reply,

Javier

_________________
Javier Ramos Diaz


Top
 Profile  
 
 Post subject: Re: CGLIB proxy & lazy loading problems in version 3
PostPosted: Mon Aug 08, 2005 9:56 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
In your code example, the BranchOffice class has a getAgency() and setAgency() method but no actual agency property ? I don't see how this will compile.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: CGLIB proxy & lazy loading problems in version 3
PostPosted: Mon Aug 08, 2005 9:58 am 
Newbie

Joined: Mon Aug 08, 2005 6:41 am
Posts: 4
pksiv wrote:
In your code example, the BranchOffice class has a getAgency() and setAgency() method but no actual agency property ? I don't see how this will compile.


Mmmmmh, sorry, the actual object is more complicated and I simplified it to send the example. I took away too much code.

_________________
Javier Ramos Diaz


Top
 Profile  
 
 Post subject: Re: CGLIB proxy & lazy loading problems in version 3
PostPosted: Mon Aug 08, 2005 10:26 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
jramosd wrote:
Mmmmmh, sorry, the actual object is more complicated and I simplified it to send the example. I took away too much code.


Not sure what to tell you.
I copied your mapping files and your Java class files(adding the agency property) into a test project and after inserting some data with the following code
Code:
      Session session = getSession();
      Transaction tx = session.beginTransaction();

      Agency agency1 = new Agency();
      agency1.setName("Test Agency 1");
      session.save(agency1);
            
      BranchOffice branchOffice1 = new BranchOffice();
      branchOffice1.setName("Branch Office 1");
      branchOffice1.setAgency(agency1);
      session.save(branchOffice1);
      BranchOffice branchOffice2 = new BranchOffice();
      branchOffice2.setName("Branch Office 2");
      branchOffice1.setAgency(agency1);
      session.save(branchOffice2);
      
      AgencyOperator agOp1 = new AgencyOperator();
      agOp1.setBranchOffice(branchOffice1);
      agOp1.setName("Preston");
      agOp1.setPassword("password");
      session.save(agOp1);
      try {
         tx.commit();
      } catch (HibernateException e) {
         e.printStackTrace();
      } finally {
           session.close();
      }
   


I executed a simple query and it seems to work as expected.
Code:
      Session session = getSession();
      try {
         Query q = session.createQuery("from AgencyOperator");
         for (Iterator i = q.list().iterator(); i.hasNext();) {
            AgencyOperator agOp = (AgencyOperator) i.next();
            System.out.print(agOp.getName() + " " + agOp.getBranchOffice().getName());   
            if (agOp.getBranchOffice().getAgency() == null) {
               System.out.println("Why am I null ?");
            } else {
               System.out.println(agOp.getBranchOffice().getAgency().getName());
            }
         }
      } catch (Throwable t) {
         t.printStackTrace();
      } finally {
         session.close();
      }

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: CGLIB proxy & lazy loading problems in version 3
PostPosted: Mon Aug 08, 2005 11:32 am 
Newbie

Joined: Mon Aug 08, 2005 6:41 am
Posts: 4
Thanks for testing, Preston.
I will do the same simple test here to see what I get.
Regards,

Javier

pksiv wrote:
jramosd wrote:
Mmmmmh, sorry, the actual object is more complicated and I simplified it to send the example. I took away too much code.


Not sure what to tell you.
I copied your mapping files and your Java class files(adding the agency property) into a test project and after inserting some data with the following code... [quote]

_________________
Javier Ramos Diaz


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