-->
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.  [ 6 posts ] 
Author Message
 Post subject: Dinamically choosing between lazy and non-lazy collections
PostPosted: Thu Nov 18, 2004 9:50 pm 
Newbie

Joined: Thu Nov 18, 2004 9:28 pm
Posts: 11
Location: Costa Rica
Hibernate version: 2.1.6, 9.8.2004

Mapping documents:
Code:
<hibernate-mapping>
  <!--
      Created by the Middlegen Hibernate plugin 2.1

      http://boss.bekk.no/boss/middlegen/
      http://www.hibernate.org/
  -->

  <class name="com.isthmus.electronic.payments.hibernate.ProviderPerson" table="ProviderPerson">

    <id name="idproviderPerson" type="java.lang.String" column="IdProviderPerson" unsaved-value="null">
      <generator class="uuid.hex"/>
    </id>

    <property name="name" type="java.lang.String" column="name" not-null="true" length="18"/>
    <property name="idnumber" type="java.lang.String" column="IdNumber" not-null="true" length="18"/>

    <!-- Associations -->

    <!-- bi-directional one-to-many association to ProviderBill -->
    <set name="providerBills" lazy="true" inverse="true" cascade="none">
      <key>
        <column name="IdProviderPerson"/>
      </key>
      <one-to-many class="com.isthmus.electronic.payments.hibernate.ProviderBill"/>
    </set>

  </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
ProviderPerson providerPerson = (ProviderPerson) session.load(ProviderPerson.class, "2c9280e50043c3c8010043c3cb0d0001");


Full stack trace of any exception that occurs:
This is the exception I'm trying to avoid on the client when an object with no collection loaded is retrieved:
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:209)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)
at net.sf.hibernate.collection.Set.toString(Set.java:217)
at java.lang.String.valueOf(String.java:2466)
at java.lang.StringBuffer.append(StringBuffer.java:387)
at EjbClient.main(EjbClient.java:41)
at java.lang.reflect.Method.invoke(Native Method)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
Exception in thread "P=516386:O=0:CT"

Name and version of the database you are using:
Microsoft SQL Server 2000

The generated SQL (show_sql=true):
N/A
Debug level Hibernate log excerpt:
N/A

Hello, I'm using a stateless session bean to retrieve the PersonProvider entity (that it's related one-to-many with the ProviderBill entity), my idea was to avoid using the DTO pattern by simply returning the POJO retrieved by Hibernate on a simple load() operation.
I was pretty excited with the idea of having lazy collections because I wouldn't want to load a huge graph every time I want to read an entity (specially if there are cases when I'm not going to need the FK values!). My question is if there is any way to indicate dinamically (on runtime) to the session that I'm going to do a lazy load or not (not by specifying it in the mapping file), because sometimes I'd like to use a full collection load and sometimes I won't need the collection at all. I don't want an open session on my client application (swing).
Any help will be very appreciated!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 3:54 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
This is not possible. Sessionfactory is an immutable object. You can overcome this by supplying two methods in your DAO objects. For instance:
Code:
public MyObject getMyLazyObject(){...}

and
Code:
public MyObject getMyFullLoadedObject{...}


You can also write two configuration files which have different settings. Use the different configuration files in the application where it's needed.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 11:53 am 
Newbie

Joined: Thu Nov 18, 2004 9:28 pm
Posts: 11
Location: Costa Rica
Thanks for your reply. I think that's the way it should go.
What could be the best practice to handle this collections issue in the interaction between the presentation and business layer using the hibernate POJOs?, I mean, is there a documented case or set of patterns for a web-app (JSP/JSF) or desktop-app (Swing) interacting with EJBs?.
I'm trying to avoid DTOs.
Is there a thing such as a Remote Session?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 11:54 am 
Newbie

Joined: Fri Nov 19, 2004 10:59 am
Posts: 1
Hi folks,

I got the same need in my program.

I find in the class net.sf.hibernate.impl.CriteriaImpl the function setFetchMode(String associationPath, FetchMode mode).

I tried two different way to use it. One not work and the second one not work. I don

_________________
Vipaire


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 12:03 pm 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
dmunguia wrote:
I mean, is there a documented case or set of patterns for a web-app (JSP/JSF) or desktop-app (Swing) interacting with EJBs?.
Is there a thing such as a Remote Session?


Look at the design patterns in the hibernate documentation:
http://www.hibernate.org/40.html

Basicly it is: a session per business transaction. This is problematic in a MVC design in a Swing application (Because of java object references). There is not much you can do about it.
Hope this helps.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 12:15 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
@dmunguia

A remote Session is actualy a very nice idea. There was a mail about this in the dev list some day ago. But I don't think this will be done for Hibernate.

An other way of avaoiding DTO's is to cut the GUI apart. For SWING I only know about comercial solutions like Canoo or even TerminalSever or Citrix. For SWT you can have a look [url]rswt.sf.net[/url].

HTH
Ernst


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