This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Simple Hibernate query? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Simple Hibernate query?" Watch "Simple Hibernate query?" New topic

Simple Hibernate query?

Rob Mitchell

Joined: Dec 10, 2003
Posts: 7
Got a little test Java app that inserts records into Oracle table (no problem) and tries to select them back out (not working).

// create table rob (id number, name varchar2(32), created date);

public static void main(String[] args) {
int iArrayLen = 4;
Rob[] robArray = new Rob[iArrayLen];

for (int i=0 ; i<iArrayLen ; i++) {
robArray[i] = new Rob();
robArray[i].setName("Rob [" + i + "] from Hibernate");

try {
Session session = OracleSessionFactory.currentSession();

Transaction tx = session.beginTransaction();
for (int i=0 ; i<iArrayLen ; i++) {[i]);
System.out.println("[" + i + "] " + robArray[i].toString());
System.out.println("Save successful.");

tx = session.beginTransaction();
//Query q = session.createQuery("select * from rob order by id");
//Query q = session.createQuery("select,, r.created from rob as r order by");
//Query q = session.createQuery("from rob r where > 0");

List listRob = session.find("select,, r.created from rob r");
//List listRob = q.list();
int iSize = (listRob == null) ? 0 : listRob.size();
for (int i=0 ; i<iSize ; i++) {
Rob rob = (Rob)listRob.get(i);
System.out.println("Found result Rob [" + i + "] " + rob.toString());

} catch (HibernateException e) {
System.out.println("Save failed: " + e.toString());
} finally {
try { OracleSessionFactory.closeSession(); } catch (HibernateException e1) { }

So, connection to Oracle is fine, inserts are fine, but both Query and Find throw Exceptions:

the session.find(" ... ") throws
net.sf.hibernate.QueryException: in expected: r [select,, r.created from rob r]

Query q = session.createQuery("from rob r where > 0");
query.list() throws
net.sf.hibernate.QueryException: in expected: r [from rob r where > 0]

Any ideas?



_ ____________________________ _ <br />Rob Mitchell<br />Base Class Technologies, Inc.<br />Java, WebSphere, Domino, Oracle, Web development<br />
Rob Mitchell

Joined: Dec 10, 2003
Posts: 7
Argh! Solution is:

List listRob = session.find(" from com.test.Rob as r");

Just me being stupid.

Thanks anyways!

It is sorta covered in the JavaRanch Style Guide.
subject: Simple Hibernate query?
Similar Threads
Controlling Hibernate Session with Spring
Retrieving request parameters (from the url) in a jsp page
Query error
Need help with Hibernate Query language - timestamp field
error in running HQL Query