This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell 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


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Simple Hibernate query?" Watch "Simple Hibernate query?" New topic
Author

Simple Hibernate query?

Rob Mitchell
Greenhorn

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");
robArray[i].setCreated(java.util.Calendar.getInstance().getTime());
}

try {
Session session = OracleSessionFactory.currentSession();

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

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

List listRob = session.find("select r.id, r.name, 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());
}
tx.commit();


} 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.id, r.name, r.created from rob r]


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


Any ideas?

Thanks!

Rob


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

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!

Rob
 
GeeCON Prague 2014
 
subject: Simple Hibernate query?