-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: How to query all entries of a table
PostPosted: Thu Aug 19, 2004 3:25 am 
Newbie

Joined: Wed Aug 18, 2004 6:30 am
Posts: 14
Hi !

I have a Table called countries. It contains two fields (ID_COUNTRY, NAME). I also have a Bean with the name CountryImpl with getter and setter methods for the id and name.

can anybody tell me how i can make a query (using hibernate of course) that returns me a list containing all entries of the table COUNTRIES.

It should be a java.util.List containing CountryImpl objects.
I had a look at the documentation, but i couldn't find a solution.

Thanks for your help.
Br,
kook80


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 3:28 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
I had a look at the documentation, but i couldn't find a solution.

are you serious?
what about trying "from Country" ???

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 3:37 am 
Newbie

Joined: Wed Aug 18, 2004 6:30 am
Posts: 14
hey

i've tried "SELECT * FROM COUNTRIES"

but i got the following message:
"* only allowed inside aggregate function in SELECT [SELECT * FROM COUNTRIES]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 3:54 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
kook80 wrote:
hey

i've tried "SELECT * FROM COUNTRIES"

but i got the following message:
"* only allowed inside aggregate function in SELECT [SELECT * FROM COUNTRIES]


why not try what Anthony suggested.

countryList = session.find("from Country");


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 4:03 am 
Newbie

Joined: Wed Aug 18, 2004 6:30 am
Posts: 14
hi !

i have tried
session.find("FROM country")

but i get the following exception:
DEBUG net.sf.hibernate.util.JDBCExceptionReporter - SQL Exception
java.sql.SQLException: ORA-00936: missing expression

i have also tried:
Query q = session.createQuery("SELECT * FROM COUNTRY");
List results = q.list();


br,
kook80


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 4:11 am 
Newbie

Joined: Wed Aug 18, 2004 6:30 am
Posts: 14
This are the log entries i get with session.find("FROM country") - maybe you can help me !


2004-08-19 10:09:44,404 [Thread-6] DEBUG net.sf.hibernate.hql.QueryTranslator - HQL: FROM country
2004-08-19 10:09:44,404 [Thread-6] DEBUG net.sf.hibernate.hql.QueryTranslator - SQL: select from
2004-08-19 10:09:44,405 [Thread-6] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
2004-08-19 10:09:44,405 [Thread-6] DEBUG net.sf.hibernate.SQL - select from


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 4:14 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
kook80 wrote:
hi !

i have tried
session.find("FROM country")

but i get the following exception:
DEBUG net.sf.hibernate.util.JDBCExceptionReporter - SQL Exception
java.sql.SQLException: ORA-00936: missing expression

i have also tried:
Query q = session.createQuery("SELECT * FROM COUNTRY");
List results = q.list();


br,
kook80


now we'll need everything in the red box to help


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 4:18 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Is country the same as Country? please make effort...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 4:24 am 
Newbie

Joined: Thu Aug 19, 2004 4:21 am
Posts: 1
It is very easy. Just the Hibernate doesn't bear the asterics.;-)
Select from table as t where....

success!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 5:03 am 
Newbie

Joined: Wed Aug 18, 2004 6:30 am
Posts: 14
Hibernate version: 2.1.6

Mapping documents:
Country.hbm.xml

<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.nokia.yellowPages.model.impl.CityImpl" table="CITY">
<id name="cityId">
<column name="ID_CITY" />
<generator class="sequence">
<param name="sequence">city_sequence</param>
</generator>
</id>

<property name="cityName">
<column name="NAME" />
</property>

<many-to-one name="country"
column="ID_COUNTRY"
class="com.nokia.yellowPages.model.impl.CountryImpl"
not-null="true" />

</class>
</hibernate-mapping>



City.hbm.xml

<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.nokia.yellowPages.model.impl.CityImpl" table="CITY">
<id name="cityId">
<column name="ID_CITY" />
<generator class="sequence">
<param name="sequence">city_sequence</param>
</generator>
</id>

<property name="cityName">
<column name="NAME" />
</property>

<many-to-one name="country"
column="ID_COUNTRY"
class="com.nokia.yellowPages.model.impl.CountryImpl"
not-null="true" />

</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Session session = mSessionFactory.openSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
results = session.find("FROM country");
//Query q =session.createQuery("from country");
//results = q.list();
mLogger.debug("# of countries in list: "+results.size());
tx.commit();

} catch (HibernateException e) {
mLogger.debug("HibernateException was thrown -> msg: " + e.getMessage());
} finally {
session.close();
}


Full stack trace of any exception that occurs:
2004-08-19 10:09:44,404 [Thread-6] DEBUG net.sf.hibernate.hql.QueryTranslator - HQL: FROM country
2004-08-19 10:09:44,404 [Thread-6] DEBUG net.sf.hibernate.hql.QueryTranslator - SQL: select from
2004-08-19 10:09:44,405 [Thread-6] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
2004-08-19 10:09:44,405 [Thread-6] DEBUG net.sf.hibernate.SQL - select from
2004-08-19 10:09:44,406 [Thread-6] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
2004-08-19 10:09:44,459 [Thread-6] DEBUG net.sf.hibernate.util.JDBCExceptionReporter - SQL Exception
java.sql.SQLException: ORA-00936: missing expression

at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:169)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:208)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:543)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1405)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteDescribe(TTC7Protocol.java:643)
at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:1819)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2015)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:395)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:339)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:179)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:87)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:875)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1544)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1521)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1513)
at com.nokia.yellowPages.model.impl.DatabaseManagerImpl.listTest(DatabaseManagerImpl.java:90)
at com.nokia.yellowPages.TestHibernateAction.execute(TestHibernateAction.java:60)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2416)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:601)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
at java.lang.Thread.run(Thread.java:534)
2004-08-19 10:09:44,474 [Thread-6] WARN net.sf.hibernate.util.JDBCExceptionReporter - SQL Error: 936, SQLState: 42000
2004-08-19 10:09:44,475 [Thread-6] ERROR net.sf.hibernate.util.JDBCExceptionReporter - ORA-00936: missing expression


Name and version of the database you are using:
Oracle 9

Debug level Hibernate log excerpt:
?? i don't know


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 5:04 am 
Regular
Regular

Joined: Wed Aug 18, 2004 5:16 am
Posts: 69
Location: Modena, Italy
You have to think about objects and classes, not tables when using Hibernate.

When Anthony use "from Country" Country is the exact (case sensitive) name of the java class, not the table name.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 5:12 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
you didn't post the mapping for CountryImpl, just CityImpl twice.

but please try "from CountryImpl"


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 5:17 am 
Newbie

Joined: Wed Aug 18, 2004 6:30 am
Posts: 14
Sorry

Country Impl mapping:

<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.nokia.yellowPages.model.impl.CountryImpl" table="COUNTRY">
<id name="countryId">
<column name="ID_COUNTRY" />
<generator class="sequence">
<param name="sequence">country_sequence</param>
</generator>
</id>

<property name="countryName">
<column name="NAME" />
</property>

<set name="cities"
inverse="true"
cascade="save-update" >

<key column="ID_COUNTRY" />
<one-to-many class="com.nokia.yellowPages.model.impl.CityImpl" />

</set>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 5:21 am 
Newbie

Joined: Wed Aug 18, 2004 6:30 am
Posts: 14
i have to think in objects ?!?

so how does it work if my object is CountryImpl and my Table is Country

Select from CountryIml as c where c.countryID >0
??

or Select from CountryImpl as c where c.ID_COUNTRY > 0

?

regards
martin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 5:28 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
kook80 wrote:
i have to think in objects ?!?


yes

Quote:
so how does it work if my object is CountryImpl and my Table is Country


forget your table, you already defined the table name in the mapping doc, now just use the names you gave your properties.

did you try the query I posted last?

Quote:
Select from CountryIml as c where c.countryID >0

correct


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.