• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple Hibernate query?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rob Mitchell
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Argh! Solution is:

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

Just me being stupid.

Thanks anyways!

Rob
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic