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.xmlCode:
<many-to-one name="owner" column="FILE_ID" unique="true" not-null="true"/>
Media.hbm.xmlCode:
<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.xmlCode:
<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.xmlCode:
<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.xmlCode:
<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.