-->
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: loading all the objects from a particular class
PostPosted: Thu Feb 12, 2004 1:04 pm 
Beginner
Beginner

Joined: Thu Jan 15, 2004 11:16 am
Posts: 37
How do I load all the objects of a particular type.
I have a single system object of which there should only be one. I dont know its ID.

If there is more than one then I want to throw an error.

Ta,
Stuart.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2004 1:12 pm 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
Code:
List list = sess.find( "from SystemType" );
if ( list.size() != 1 ) throw new Exception( "..." );


Cheers.

Myk.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2004 1:55 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
session.iterate will be better actually.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2004 2:26 pm 
Beginner
Beginner

Joined: Thu Oct 09, 2003 3:42 pm
Posts: 22
emmanuel wrote:
session.iterate will be better actually.


Wouldn't session.iterate execute a sql statement for each object while session.find would execute one statement to load all of the objects.

E.g. if there were 10 SystemType objects in the database.
Session.find would execute one query to load all of the objects
Session.iterate would execute 11 queries (one to get a list of identifiers and then one for each object)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2004 5:12 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
That's the whole point.
Read the initial post
Quote:
If there is more than one then I want to throw an error.

It will only do 1 request.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2004 5:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Just do session.iterate().hasNext() ... wil do only one request for the id.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 7:23 am 
Beginner
Beginner

Joined: Thu Jan 15, 2004 11:16 am
Posts: 37
You guys are just great. I now have a nice working method. And If I am able to call hasNext() twice then I throw an exception.

Thanks for all the help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 7:36 am 
Beginner
Beginner

Joined: Thu Jan 15, 2004 11:16 am
Posts: 37
Well actually when I try this with HSQLDB I get the following error...

[junit] ------------- Standard Output ---------------
[junit] Hibernate: select from
[junit] ------------- ---------------- ---------------



[junit] Caused by: java.sql.SQLException: Unexpected token: FROM in statement [select
om]
[junit] at org.hsqldb.jdbcResultSet.<init>(Unknown Source)
[junit] at org.hsqldb.jdbcConnection.executeStandalone(Unknown Source)
[junit] at org.hsqldb.jdbcConnection.execute(Unknown Source)
[junit] at org.hsqldb.jdbcStatement.fetchResult(Unknown Source)
[junit] at org.hsqldb.jdbcStatement.executeQuery(Unknown Source)
[junit] at org.hsqldb.jdbcPreparedStatement.executeQuery(Unknown Source)
[junit] at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:83)
[junit] at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:794)
[junit] at net.sf.hibernate.hql.QueryTranslator.iterate(QueryTranslator.java:846)
[junit] at net.sf.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1540)


Looks like the SQL statement is not complete.
Ta,
Stuart.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 9:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
What are your mappings and the query you execute?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 9:56 am 
Beginner
Beginner

Joined: Thu Jan 15, 2004 11:16 am
Posts: 37
apologies for lack of information and late reply. Tonsillitis:-(

I am using xdoclet to generate my mappings but was failing to add all my mappings to the hibernate.cfg.xml file.

I now get the result...
Hibernate: select tireesyste0_.id as x0_0_ from TireeSystem tireesyste0_

Which is great!
Thanks for all the help.
Stuart.


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.