-->
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.  [ 3 posts ] 
Author Message
 Post subject: Doubt in creating queries, failing to understand errors.
PostPosted: Tue Jun 03, 2008 1:14 pm 
Newbie

Joined: Mon Sep 10, 2007 3:40 pm
Posts: 2
Location: Leiria, Portugal
Until now I've read the document at http://hibernate.org/42.html, the reference manual related to querying an object retrieval and some topics I found using Google. Neither gave me an answer but have probably pointed out the problem.
I'll put the general information first and them provide more detailed info and the problems I'm facing.

Hibernate version: 3.2.6 ga
Mapping documents: - Only relevant mappings are shown, I remove only the code related to basic properties, like strings, ints, etc.

FileWrapper.hbm.xml
Code:
<many-to-one name="disk" class="de.berlios.jfindmyfiles.catalog.entities.Media" column="MEDIA_ID" not-null="true"/>
           
<one-to-one name="image" class="de.berlios.jfindmyfiles.catalog.entities.ImageData" property-ref="owner"/>

<one-to-one name="video" class="de.berlios.jfindmyfiles.catalog.entities.VideoData" property-ref="owner"/>

<one-to-one name="audio" class="de.berlios.jfindmyfiles.catalog.entities.AudioData" property-ref="owner"/>             
               
<set name="children" inverse="true">
    <key column="PARENT_ID"/>
    <one-to-many class="de.berlios.jfindmyfiles.catalog.entities.FileWrapper"/>
</set>

<many-to-one name="parent" column="PARENT_ID" class="de.berlios.jfindmyfiles.catalog.entities.FileWrapper"/>


ImageData.hbm.xml, VideoData.hbm.xml, AudioData.hbm.xml
Code:
<many-to-one name="owner" column="FILE_ID" unique="true" not-null="true"/>


Media.hbm.xml
Code:
<set name="loans" table="TBL_LOANS" inverse="true">
    <key column="MEDIA_ID"/>
    <one-to-many class="de.berlios.jfindmyfiles.catalog.entities.Loan"/>
</set>

<set name="labels" table="TBL_MEDIA_LABEL">
    <key column="MEDIA_ID"/>
    <many-to-many column="LABEL_ID" class="de.berlios.jfindmyfiles.catalog.entities.Label"/>
</set>

<set name="files" inverse="true">
    <key column="MEDIA_ID"/>
    <one-to-many class="de.berlios.jfindmyfiles.catalog.entities.FileWrapper"/>
</set>

<many-to-one name="type" class="de.berlios.jfindmyfiles.catalog.entities.Type" column="TYPE_ID" not-null="true" lazy="false"/>
       
<join table="TBL_GROUP_MEDIA" optional="true">
    <key column="MEDIA_ID"/>
    <many-to-one name="group" column="GROUP_ID" class="de.berlios.jfindmyfiles.catalog.entities.DiskGroup" not-null="true"/>
</join>


DiskGroup.hbm.xml
Code:
<set name="groups" inverse="true">
    <key column="PARENT_ID"/>
    <one-to-many class="de.berlios.jfindmyfiles.catalog.entities.DiskGroup"/>
</set>

<many-to-one name="parent" column="PARENT_ID" class="de.berlios.jfindmyfiles.catalog.entities.DiskGroup"/>
       
<set name="disks" table="TBL_GROUP_MEDIA">
    <key column="GROUP_ID"/>
    <many-to-many column="MEDIA_ID" unique="true" class="de.berlios.jfindmyfiles.catalog.entities.Media"/>
</set>


Loan.hbm.xml
Code:
<many-to-one name="media" class="de.berlios.jfindmyfiles.catalog.entities.Media" column="MEDIA_ID" not-null="true"/>

<many-to-one name="loanee" column="USER_ID" class="de.berlios.jfindmyfiles.catalog.entities.User" not-null="true"/>


User.hbm.xml
Code:
<set name="loans" table="TBL_LOAN" inverse="true">
    <key column="LOAN_ID"/>
    <one-to-many class="de.berlios.jfindmyfiles.catalog.entities.Loan"/>
</set>


Exception stack trace: The exceptions are thrown only when using a specific code, please see bellow.
Code:
SEVERE [global]
org.hibernate.SessionException: Session is closed!
        at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
        at org.hibernate.impl.SessionImpl.getBatcher(SessionImpl.java:260)
        at org.hibernate.loader.Loader.doQuery(Loader.java:726)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
        at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
        at (...)

SEVERE [global]
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
        at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
        at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
        at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
        at de.berlios.jfindmyfiles.catalog.entities.FileWrapper$$EnhancerByCGLIB$$67a0f96c.toString(<generated>)
        at (...)


This last exception repeats itself twice

Name and version of the database you are using: HSQLDB 1.8

Relevant SQL created by Hibernate:
Code:
Hibernate: select filewrappe0_.FILE_ID as col_0_0_ from TBL_FILE filewrappe0_ where filewrappe0_.name like ?
Hibernate: select filewrappe0_.FILE_ID as FILE1_3_3_, filewrappe0_.name as name3_3_, filewrappe0_.absolutePath as absolute3_3_3_, filewrappe0_.lastModified as lastModi4_3_3_, filewrappe0_.description as descript5_3_3_, filewrappe0_.hidden as hidden3_3_, filewrappe0_.size as size3_3_, filewrappe0_.extension as extension3_3_, filewrappe0_.file as file3_3_, filewrappe0_.folder as folder3_3_, filewrappe0_.MEDIA_ID as MEDIA11_3_3_, filewrappe0_.PARENT_ID as PARENT12_3_3_, imagedata1_.IMAGE_ID as IMAGE1_4_0_, imagedata1_.data as data4_0_, imagedata1_.FILE_ID as FILE3_4_0_, videodata2_.VIDEO_ID as VIDEO1_11_1_, videodata2_.data as data11_1_, videodata2_.FILE_ID as FILE3_11_1_, audiodata3_.AUDIO_ID as AUDIO1_0_2_, audiodata3_.data as data0_2_, audiodata3_.FILE_ID as FILE3_0_2_ from TBL_FILE filewrappe0_ left outer join TBL_IMAGE imagedata1_ on filewrappe0_.FILE_ID=imagedata1_.FILE_ID left outer join TBL_VIDEO videodata2_ on filewrappe0_.FILE_ID=videodata2_.FILE_ID left outer join TBL_AUDIO audiodata3_ on filewrappe0_.FILE_ID=audiodata3_.FILE_ID where filewrappe0_.FILE_ID=?


What I want to do and the problems I'm getting:
I'm trying to get some data from the database and display it in a JList. To achieve that I though about using a parameterized query, providing the parameter I needed, grabbing the results and, using a foreach construct, putting them inside the JList's model.
The problem I faced is that, using the code below, no item is displayed in the JList.

Code:
Session s = eng.sessionFactory.getCurrentSession();
s.beginTransaction();
Criteria c = s.createCriteria(FileWrapper.class).add(Restrictions.ilike("name", jtfSearchText.getText().trim(), MatchMode.ANYWHERE));

for(Object o : c.list()) {
    //System.err.println("FOUND: " + o);
    listModel.addElement(o);
}

s.getTransaction().commit();


If I uncomment the line where I'm printing the retrieved objects name, all items are shown in the JList.

I changed the code, based on some examples I found to the following:
Code:
Session s = eng.sessionFactory.getCurrentSession();
s.beginTransaction();

Iterator it = s.createQuery("from FileWrapper file where file.name like ?").setString(0, "%"
        + jtfSearchText.getText().trim() + "%").iterate();

while(it.hasNext()) {
    listModel.addElement(it.next());
}
s.getTransaction().commit();


This resulted in the above exceptions being thrown, which I think shows a bug in my code, probably the mapping files and I'm inclined to believe that the problem lies in the lazy=true/false property that I don't yet understand properly.
And I'm sorry, but I couldn't understand it from the official documentation, sometime those docs complicate things that are simple.[/code]

So, basically I don't understand what is going on, why, when I run through the results and print their name before I put them on the JList's model do they appear in the interface, whereas if I don't print their name, the list simply doesn't show anything.
And why does the second code doesn't work when the first does, what is the difference here?

I would like to add that an RTFM answer is something I don't have a problem with, but if you would be kind enough, please provide the manual name, chapter title, page number, paragraph count and line offset starting from the top. :D

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 10:13 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Basically, you're running into objects that are loaded lazily. When you call a method on them, they are loaded, that's why they then appear. When nothing was loaded, they don't.

If you extended the life of your session, you probably wouldn't have this problem. Look at the open session in view design pattern for a quick fix to this problem.

Also, take a look at this tutorial about How Hibernate Works. I think it might really help clear up some of your confusion:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=07howhibernateworks

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 11:32 am 
Newbie

Joined: Mon Sep 10, 2007 3:40 pm
Posts: 2
Location: Leiria, Portugal
Cameron McKenzie wrote:
Basically, you're running into objects that are loaded lazily. When you call a method on them, they are loaded, that's why they then appear. When nothing was loaded, they don't.

I thought about that, but what made it more confusing it that a JList calls the toString method of the objects it contains in order to display them, so why would the objects stay hidden when a method, in this case the toString, was being called by a component, and appear if I call the same method before adding the objects to the JList's model?

Cameron McKenzie wrote:
Also, take a look at this tutorial about How Hibernate Works. I think it might really help clear up some of your confusion:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=07howhibernateworks


Thanks for the help, I'll see what I can learn from the tutorial.


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