facebook

org.hibernate.exception.SQLGrammarException: could not exec

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #243889 Reply

    diamonddog
    Member

    In 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 query

    mysql database running simple library.book table with book_id, title, author, borrower .

    tried everything can think of. any help???

    #243921 Reply

    Riyad Kalla
    Member

    Moving to OT > Soft Dev.

    Try: session.find(“from Book”);

    #243924 Reply

    diamonddog
    Member

    The method find(String) undefined for the type Session

    #243929 Reply

    diamonddog
    Member

    Ok 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]

    #243930 Reply

    Riyad Kalla
    Member

    Diamond,
    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

    http://www.hibernate.org/hib_docs/v3/reference/en/html/

    #243935 Reply

    diamonddog
    Member

    i 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 query

    I 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?

    #243951 Reply

    diamonddog
    Member

    Ok 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

    
     
    
Viewing 7 posts - 1 through 7 (of 7 total)
Reply To: org.hibernate.exception.SQLGrammarException: could not exec

You must be logged in to post in the forum log in