-->
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.  [ 10 posts ] 
Author Message
 Post subject: Problem with Criteria & FetchMode
PostPosted: Thu Mar 19, 2009 6:44 am 
Newbie

Joined: Fri Mar 02, 2007 6:31 am
Posts: 19
I keep gettting LazyInitializationExceptions although I use setFetchMode() to initialize fields.

Any idea what I'm doing wrong?

Extract of my code:
Code:
Criteria criteria = session.createCriteria(EntryLinkDelta.class)
                            .createCriteria("providerEntryLink", "pel")
                                .createCriteria("pel.entry")
                                    .add(Restrictions.idEq(id));
String[] required = { "providerEntryLink.providerData.dataKey", "oldValue", "newValue"};
for (int i = 0; i < required.length; i++) {
   criteria.setFetchMode(required[i], FetchMode.JOIN);
}
List list = criteria.list();
session.close();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
   EntryLinkDelta eld = (EntryLinkDelta) iterator.next();
   log.debug("\t" + eld.getOldValue());
   log.debug("\t" + eld.getNewValue());
   log.debug("\t" + eld.getProviderEntryLink().getProviderData().getDataKey()); // LazyInitializationException
}

Extract of my POJOs:
Code:
class EntryLinkDelta {
   private String oldValue;
   private String newValue;
   private ProviderEntryLink providerEntryLink;
   // other properties and getters & setters
}

class ProviderEntryLink {
   private Entry entry;
   private ProviderData providerData;
   // other properties and getters & setters
}

class Entry {
   private Long id;
   // other properties and getters & setters
}

class ProviderData {
   private String dataKey;
   // other properties and getters & setters
}

Stacktrace:
Code:
ERROR hibernate.LazyInitializationException - could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
        at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)
        at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
        at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
        at com.jnj.gtsc.agent.commons.bankdir.pl.ProviderData_$$_javassist_6.getDataKey(ProviderData_$$_javassist_6.java)
        at com.jnj.gtsc.agent.business.bankdir.business.BankDirDAOBL.getDetails(BankDirDAOBL.java:39)
        at com.jnj.gtsc.agent.business.bankdir.session.BankDirDAOBLMgrBean.getDetails(BankDirDAOBLMgrBean.java:54)
        at com.jnj.gtsc.agent.business.bankdir.session.BankDirDAOBLMgrBean_7cajfk_EOImpl.getDetails(BankDirDAOBLMgrBean_7cajfk_EOImpl.java:46)
        at com.jnj.gtsc.agent.business.bankdir.session.BankDirDAOBLMgrBean_7cajfk_EOImpl_WLSkel.invoke(Unknown Source)
        at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:491)
        at weblogic.rmi.cluster.ReplicaAwareServerRef.invoke(ReplicaAwareServerRef.java:120)
        at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:434)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:147)
        at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:429)
        at weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteRequest.java:35)
        at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:224)
        at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:183)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 7:56 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 4:10 am
Posts: 46
you have to do


String[] required = { "providerEntryLink.providerData", "providerEntryLink.providerData.dataKey"};


if dataKey is value type (String, Long ..) then you can

String[] required = { "providerEntryLink.providerData"};


Good luck!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 8:48 am 
Newbie

Joined: Fri Mar 02, 2007 6:31 am
Posts: 19
It doesn't work. I still get the same LazyInitializationException

Neither

String[] required = { "providerEntryLink.providerData"};

nor

String[] required = { "providerEntryLink.providerData", "providerEntryLink.providerData.dataKey"};

works, even though 'dataKey' is a string.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 9:04 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 4:10 am
Posts: 46
try

String[] required = { "providerEntryLink", "providerEntryLink.providerData"};


it has to work :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 9:19 am 
Newbie

Joined: Fri Mar 02, 2007 6:31 am
Posts: 19
alexandrubarbat wrote:
it has to work :)

It doesn't. :-(

I would expect that I only need to provide the association path for the fields I need. So specifying {"providerEntryLink", "providerEntryLink.providerData"} seems unnecessary to me. Hibernate should be able to figure out by itself that "providerEntryLink" should be fetched.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 9:27 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 4:10 am
Posts: 46
ok, so let's dig it. Please post the mappings for classes

EntryLinkDelta, ProviderEntryLink and ProviderData.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 9:32 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 4:10 am
Posts: 46
Put in log4j configuration


<category name="org.hibernate.type">
<priority value="trace"/>
</category-->

<category name="org.hibernate.SQL">
<priority value="debug"/>
</category>


and post also the sql generated by hibernate for your query


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 9:37 am 
Newbie

Joined: Fri Mar 02, 2007 6:31 am
Posts: 19
EntryLinkDelta.hbm.xml:
Code:
<hibernate-mapping package="com.jnj.gtsc.agent.commons.bankdir.pl" schema="AGENT">
  <class name="EntryLinkDelta" table="TBD3_ENTRY_LINK_DELTA" mutable="false">
    <id name="id" column="DELTA_ID" type="long" unsaved-value="null">
      <generator class="increment" />
    </id>
    <version name="version" column="VERSION" type="long" />
    <many-to-one name="providerEntryLink" not-null="true" embed-xml="false">
      <column name="ENTRY_LINK_ID" />
    </many-to-one>
    <property name="field" column="FIELD" type="string" length="256" not-null="true" />
    <property name="oldValue" column="OLD_VALUE" type="string" length="1000" not-null="false" />
    <property name="newValue" column="NEW_VALUE" type="string" length="1000" not-null="false" />
    <property name="createUserId" column="CREATE_USID" type="string" length="20" not-null="false" />
    <property name="createContext" column="CREATE_CONTEXT" type="string" length="20" not-null="false" />
    <property name="createStamp" column="CREATE_STAMP" type="timestamp" not-null="false" />
    <property name="updateUserId" column="UPDATE_USID" type="string" length="20" not-null="false" />
    <property name="updateContext" column="UPDATE_CONTEXT" type="string" length="20" not-null="false" />
    <property name="updateStamp" column="UPDATE_STAMP" type="timestamp" not-null="false" />
  </class>
</hibernate-mapping>

ProviderEntryLink.hbm.xml:
Code:
<hibernate-mapping package="com.jnj.gtsc.agent.commons.bankdir.pl"
   schema="AGENT">
   <class name="ProviderEntryLink" table="TBD3_PROVIDER_ENTRY_LINK">
      <id name="id" column="ENTRY_LINK_ID" type="long" unsaved-value="null">
         <generator class="increment" />
      </id>
      <version name="version" column="VERSION" type="long" />
      <many-to-one name="providerData" column="PROVIDER_DATA_ID"
         not-null="true" />
      <many-to-one name="entry" column="ENTRY_ID" not-null="true" />
      <set name="deltas" inverse="true" embed-xml="false">
         <key column="ENTRY_LINK_ID" />
         <one-to-many class="EntryLinkDelta" />
      </set>
      <property name="context" column="CONTEXT" type="string"
         length="50" not-null="false" />
      <property name="createUserId" column="CREATE_USID" type="string"
         length="20" not-null="false" />
      <property name="createContext" column="CREATE_CONTEXT" type="string"
         length="20" not-null="false" />
      <property name="createStamp" column="CREATE_STAMP" type="timestamp"
         not-null="false" />
      <property name="updateUserId" column="UPDATE_USID" type="string"
         length="20" not-null="false" />
      <property name="updateContext" column="UPDATE_CONTEXT" type="string"
         length="20" not-null="false" />
      <property name="updateStamp" column="UPDATE_STAMP" type="timestamp"
         not-null="false" />
   </class>
</hibernate-mapping>

ProviderData.hbm.xml:
Code:
<hibernate-mapping package="com.jnj.gtsc.agent.commons.bankdir.pl"
   schema="AGENT">
   <class name="ProviderData" table="TBD3_PROVIDER_DATA">
      <id name="id" column="PROVIDER_ID" type="long" unsaved-value="null">
         <generator class="increment" />
      </id>
      <version name="version" column="VERSION" type="long" />
      <property name="provider" column="PROVIDER" type="string"
         length="50" not-null="true" />
      <property name="dataKey" column="DATA_KEY" type="string"
         length="50" not-null="true" />
      <property name="sequence" column="SEQ" type="long" not-null="true" />
      <set name="linkEntries" inverse="true" embed-xml="false">
         <key column="PROVIDER_DATA_ID" />
         <one-to-many class="ProviderEntryLink" />
      </set>
      <property name="createUserId" column="CREATE_USID" type="string"
         length="20" not-null="false" />
      <property name="createContext" column="CREATE_CONTEXT" type="string"
         length="20" not-null="false" />
      <property name="createStamp" column="CREATE_STAMP" type="timestamp"
         not-null="false" />
      <property name="updateUserId" column="UPDATE_USID" type="string"
         length="20" not-null="false" />
      <property name="updateContext" column="UPDATE_CONTEXT" type="string"
         length="20" not-null="false" />
      <property name="updateStamp" column="UPDATE_STAMP" type="timestamp"
         not-null="false" />
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 12:15 pm 
Beginner
Beginner

Joined: Wed Oct 03, 2007 4:10 am
Posts: 46
Case solved:

you have to put only:

String[] required = {"providerData"};

if the query is something like that (and it is):

Criteria criteria = getSession().createCriteria(EntryLinkDelta.class)
.createCriteria("providerEntryLink", "pel")
.add(Restrictions.idEq(new Long(1)));


because you are at providerEntryLink level.

if you do :

Criteria criteria = getSession().createCriteria(EntryLinkDelta.class);
criteria.createCriteria("providerEntryLink", "pel");
criteria.add(Restrictions.idEq(new Long(1)));


you have to:

Criteria c = getSession().createCriteria(Terminal.class);
c.createCriteria("partener", "part");
c.add(Restrictions.idEq(new Long(18025017)));

String[] required = {"providerEntryLink.providerData"};
....


because you are at root level(EntruLinkDelta)


Good luck


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 5:21 am 
Newbie

Joined: Fri Mar 02, 2007 6:31 am
Posts: 19
It finally works. Thanks a lot.

I now put the 'setFetchMode()' on the root level so I can pass the full association path to it.
I need the full path because then I can use it in my HibernateCleaner too. (The cleaner sets all other references to POJOs and collections to null so I can pass the object graph over RMI to my Swing client)

Final code:
Code:
String[] required = { "providerEntryLink.providerData" };
Criteria criteria = session.createCriteria(EntryLinkDelta.class);
criteria.setFetchMode(required[0], FetchMode.JOIN);
criteria.createCriteria("providerEntryLink", "pel")
      .createCriteria("pel.entry")
         .add(Restrictions.idEq(id));
List/*<EntryLinkDelta>*/ list = criteria.list();
session.close();
HibernateCleaner.clean(list, required);


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