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.  [ 4 posts ] 
Author Message
 Post subject: Lazy initialization issue
PostPosted: Tue Apr 12, 2005 8:02 pm 
Regular
Regular

Joined: Fri Oct 08, 2004 4:11 am
Posts: 78
Location: Nottingham, England
Hi,
I wish to retrieve a user record with all its associated order records by passing username and password parameters to the user class. As I have specified a set attribute with lazy=true, the issue I am having is where to place my Hibernate.Initialize line within the syntax. What I want to do is something like the following:

Code:
          query = session.createQuery("from test.User as user where user.UserName=:username and user.UserPassword=:password")
          .setString("username",username)
          .setString("password",password);
          user = (User) query.uniqueResult();
          Hibernate.initialize(user.getUserOrders());


In the above code I know that the initialize line is incorrect, as when I run this code without it everything is fine minus the fact that I don't get any oredr records back.

How can I do this in this intance? In a non query based format I would do it as follows:

Code:
          userSet = (User)session.load(User.class, password);
          Hibernate.initialize(userSet.getUserOrders());


Hibernate version:
JBoss 4.0

Mapping documents:
Code:
   <class name="User" table="usertbl">
      <cache usage="read-write"/>
       <id name="ID" column="user_id" type="integer" unsaved-value="0">
         <generator class="sequence">
               <param name="sequence">seq_user_mytable</param>
         </generator>
      </id>
      <property name="FirstName" column="first_name" type="string" not-null="true"/>
      <property name="LastName" column="last_name" type="string" not-null="true"/>
      <property name="Address1" column="address1" type="string"/>
      <property name="Address2" column="address2" type="string"/>
      <property name="Address3" column="address3" type="string"/>
      <property name="PostCode" column="postcode" type="string"/>
      <property name="Country" column="country" type="string"/>
      <property name="Email" column="email" type="string" not-null="true"/>
      <property name="HomeTel" column="home_telephone" type="string"/>
      <property name="HomeMob" column="mobile_telephone" type="string"/>
      <property name="DateJoined" column="date_joined" type="timestamp"/>
      
      <property name="UserName" type="string">
         <column name="username" unique-key="UserPassKey"/>
      </property>
      <property name="UserPassword" type="string">
         <column name="userpassword" unique-key="UserPassKey"/>
      </property>
      
      <set name="ArtistChoice" lazy="true" inverse="true" cascade="all-delete-orphan">
         <cache usage="read-write"/>
          <key column="user_id"/>
         <one-to-many class="Artist"/>
       </set>
      
      <set name="UserOrders" lazy="true" inverse="true" cascade="all-delete-orphan">
         <cache usage="read-write"/>
          <key column="user_id"/>
         <one-to-many class="Order"/>
       </set>
   </class>


Code between sessionFactory.openSession() and session.close():
Code:
          transaction = session.beginTransaction();
   
          //Get User details
          query = session.createQuery("from test.User as user where user.UserName=:username and user.UserPassword=:password")
          .setString("username",username)
          .setString("password",password);
          user = (User) query.uniqueResult();
          Hibernate.initialize(user.getUserOrders());
         
          transaction.commit();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 4:12 am 
Regular
Regular

Joined: Fri Oct 08, 2004 4:11 am
Posts: 78
Location: Nottingham, England
Hi,
any pointers on why I can't do the following and how best to resolve it would be most welcome:

Code:
          query = session.createQuery("from test.User as user where user.UserName=:username and user.UserPassword=:password")
          .setString("username",username)
          .setString("password",password);
          user = (User) query.uniqueResult();
          Hibernate.initialize(user.getUserOrders());


Why is hibernate.initialize wrong in this instance? I want to bring back a specific user record and any assoiciated order records for that user......

many thanks

Uzo


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 6:04 am 
Regular
Regular

Joined: Fri Oct 08, 2004 4:11 am
Posts: 78
Location: Nottingham, England
Not to worry guys....
it does work! Blasted application server(jboss 4) was messing me around with cache...

Is it possible to order the lazy resultset (order by x (asc|desc))?

regards

Uzo


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 10:06 am 
Regular
Regular

Joined: Fri Oct 08, 2004 4:11 am
Posts: 78
Location: Nottingham, England
Dam! I love hibernate....

Code:
<set order-by="order_date desc" ... >
  ....
</set>


Maybe someone can answer this for me, is it better to sort in memory or in the database?:

Code:
<set sort="natural"> (memory)

or

<set order-by="order_date desc" ... > (database)


regards


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