- This topic has 6 replies, 2 voices, and was last updated 18 years, 11 months ago by diamonddog.
-
AuthorPosts
-
diamonddogMemberIn main function this code:
Session session = null;
try {
// Step 3 – Get a Hibernate Session
// This step will read hibernate.cfg.xml and prepare hibernate for use
session = HibernateSessionFactory.currentSession();
System.out.println(“Session is open?”+session.isOpen());
System.out.println(“Session is connected?”+session.isConnected());
// Step 4 – Persist entity to database//Using from Clause
String SQL_QUERY =”from Book book”;booklist = session.createQuery(SQL_QUERY).list();
for(Book book: booklist){
System.out.println(“Title: ” + book.getTitle());
System.out.println(“Author: ” + book.getAuthor());
}
session.close();
System.out.println(“Select successful.”);
} catch (HibernateException e) {
System.out.println(“Select failed. BECAUSE::: “+e+”\n”+session.isConnected());when run it get:
org.hibernate.exception.SQLGrammarException: could not execute querymysql database running simple library.book table with book_id, title, author, borrower .
tried everything can think of. any help???
Riyad KallaMemberMoving to OT > Soft Dev.
Try: session.find(“from Book”);
diamonddogMemberThe method find(String) undefined for the type Session
diamonddogMemberOk so i cdoe to:
String SQL_QUERYsql = “select title, author, borrowedby from library.book”;
booklist = session.createSQLQuery(SQL_QUERYsql).list();new exception is:
org.hibernate.QueryException: addEntity() or addScalar() must be called on a sql query before executing the query. [select title, author, borrowedby from library.book]
Riyad KallaMemberDiamond,
Please read the Hibernate tutorial, I think it will save you a lot of time. I’m rusty with it right now otherwise I would help, but HIbernate supports HQL (hibernate query language) as well as SQL. I believe what you are doing is generating a query (SQL) to execute, and you should be using HQL like “from Book”, but you just need to find the right method to call, which is why I suggest the tutorial, I think it changed in Hibernate 3.1
diamonddogMemberi have read the tutorial. reinstalled the latest mysql(5.0) database server….reworked the small app
and still getting the error. even my HQL is: from Book.added to hibernate.cfg.xml
<property name=”show_sql”>true</property>error is:
Hibernate: select book0_.id_book as id1_, book0_.title as title0_, book0_.author as author0_, book0_.borrower as borrower0_ from library__book book0_
Select failed. BECAUSE::: org.hibernate.exception.SQLGrammarException: could not execute queryI am wondering. the tutorial in MyEclipse only showed how to do a query.save(). The problem is when
I call query.list(); that is where it falls apart. When I test for query.getNamedParameters(); I get Null
somewhere here it is breaking down. and I believe it is in the MyEclipse code somewhere. A bug?
diamonddogMemberOk found some fixes and got it all working. Just in case someone else has the same prob. in hibernate.cfg.xml
deleted the column property because HQL was looking for a table called library_book instead of library.book
for some strange reason, MySQL had a prob. with this. Now HQL “from Book” became “…..book as book0_”
and as well, the generator class in book.cfb.xml I changed to <generator class=”identity”> from hilo. I wish there was a table to tell which generator classes worked well with which Dialects. Last I called session.beginTransaction() before i uses session.save(Object)Serializable, and session.createQuery(sqlStr).list(); Finally everything works. WheW -
AuthorPosts