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: How to retrieve several rows
PostPosted: Tue Jan 31, 2012 3:32 pm 
Newbie

Joined: Tue Jan 31, 2012 3:23 pm
Posts: 7
Good evening,

I started with Hibernate today and did tutorials for the most of the day :-).

But now I can't find a solution for my problem, so I hope you don't mind me asking here:

I have two Tables

Document(DocumentID, DocumentName, UserID)
Version(id, VersionID, DocumentID, uploadedAt, DocumentText)

The background behinds this is that a document can have several versions (it can be updated!)

So when I try to get all different Versions of one Document I'm doing this SQL Query: ""SELECT * FROM Version WHERE documentID = " + documentID"

How can I get the Ouput? If I just would like to have one Object from the Version-Table I'd do:

Code:
Version version = session.get(Version.class, documentID);


But it seems that I cant use this function for getting several Versions, that's why I tried it with several SQLQuries (mixed through, this is just one of my attempts):

Code:
List list = session.createSQLQuery("SELECT * FROM Version WHERE documentID = " + documentID).list();


Later I tried to cast it to the type "Version", but exception stopped me ;)

Can anyone of you help me? I'm in a bit of time trouble, so every help is very appreciated!

Thanks a lot!


Top
 Profile  
 
 Post subject: Re: How to retrieve several rows
PostPosted: Tue Jan 31, 2012 4:06 pm 
Newbie

Joined: Tue Jan 31, 2012 3:23 pm
Posts: 7
Okay i've managed to get the text with the following method:

Code:
public List<Version> getVersions(Long documentID){
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction transaction = session.beginTransaction();
      
      Query query = session.createQuery("FROM Version WHERE documentID = " + documentID);
      transaction.commit();

      return query.list();
   }


weird is that I had to use the "openSession()"-method, because I was getting "Session is closed!"-Exceptions. But only in this method, nowhere before ...

Is it a good solution just using "openSession()" here then?


Top
 Profile  
 
 Post subject: Re: How to retrieve several rows
PostPosted: Tue Jan 31, 2012 10:34 pm 
Newbie

Joined: Tue Jan 31, 2012 10:04 pm
Posts: 3
Any operations to the Database has to be performed through an open session. So nothing weird about it.
" But only in this method, nowhere before ..". A session will have been opened surely. You just have to look.


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.