-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: SQLException
PostPosted: Thu Jan 05, 2006 6:54 am 
Beginner
Beginner

Joined: Mon Sep 27, 2004 11:51 pm
Posts: 22
Hi ,

l have a java.sql.SQLException,

Code:
org.springframework.jdbc.BadSqlGrammarException:

Hibernate operation: could not execute query;

bad SQL grammar [

select distinct

this_.BOOK_ID as y0_,
this_.TITLE as y1_,
this_.ISBN as y2_ from BOOKS this_

inner join LINK_BOOK_AUTHOR authors5_ on this_.BOOK_ID=authors5_.BOOK_ID
inner join AUTHORS author3_ on authors5_.AUTHOR_ID=author3_.AUTHOR_ID
inner join NOTES note1_ on this_.BOOK_ID=note1_.NOTE_ID
inner join PUBLISHERS publisher2_ on this_.PUBLISHER_ID=publisher2_.PUBLISHER_ID

where (lower(y2_) like ?) and (1=1) and (1=1) and (1=1) order by y1_ asc];

nested exception is java.sql.SQLException: Unknown column 'y2_' in 'where clause'

java.sql.SQLException: Unknown column 'y2_' in 'where clause'
...
...


it throw when l executed the method below ,
Code:
   public Collection findBooksByCriteriaWithLimit(BookCommand bookCommand)
         throws DataAccessException {
      
      final Book book = bookCommand.getBook();
      final Author iAuthor = bookCommand.getAuthor();
      final Publisher iPublisher = book.getPublisher();
      final Note iNote = book.getNote();
      final String sortColumn = bookCommand.getS_sortColumn();
      final int page = bookCommand.getS_page();
      final int pageSize = bookCommand.getS_pageSize();
      final int ascending = bookCommand.getS_ascending();

      return getHibernateTemplate().executeFind(new HibernateCallback() {
         public Object doInHibernate(Session session)
               throws HibernateException, SQLException {

            Example exampleBook = Example.create(book).ignoreCase()
                  .enableLike(MatchMode.ANYWHERE);
            Example examplePublisher = Example.create(iPublisher).ignoreCase()
                  .enableLike(MatchMode.ANYWHERE);
            Example exampleNote = Example.create(iNote).ignoreCase()
                  .enableLike(MatchMode.ANYWHERE);
            Example exampleAuthor = Example.create(iAuthor).ignoreCase()
                  .enableLike(MatchMode.ANYWHERE);

            Criteria bookCriteria = session.createCriteria(Book.class)
                              .setProjection(Projections.distinct(Projections.projectionList()
                                    .add(Projections.property("id"),"id")
                                    .add(Projections.property("title"),"title")
                                    .add(Projections.property("isbn"),"isbn")
                                    ));

            if (ascending > 0) {
               bookCriteria.addOrder(Order.asc(sortColumn));
            } else {
               bookCriteria.addOrder(Order.desc(sortColumn));
            }
            bookCriteria.add(exampleBook);

            bookCriteria.createCriteria("note").add(exampleNote);
            bookCriteria.createCriteria("publisher").add(examplePublisher);
            bookCriteria.createCriteria("authors").add(exampleAuthor);
            bookCriteria.setFirstResult(page * pageSize)
                     .setMaxResults(pageSize);

            return bookCriteria.setResultTransformer(new AliasToBeanResultTransformer(Book.class)).list();
            
         }
      });
      
   }


l found the error was came from the generated sql by hibernate , or make it simple , the problem can be restated more simple as below ,

when l execute the findBooksByCriteriaWithLimit(..) , BadSqlGrammarException throw , the generated sql would be ,

Code:
select

this_.BOOK_ID as y0_, this_.TITLE as y1_, this_.ISBN as y2_

from BOOKS this_

where y0_ = 1


it suppose to be ,
Code:
select

this_.BOOK_ID as y0_, this_.TITLE as y1_, this_.ISBN as y2_

from BOOKS this_

where this_.BOOK_ID = 1


Is findBooksByCriteriaWithLimit(..) a wrong implementation or l miss something ?

RelationShip of Tables :

Books <-- 1:1 --> Notes
Books <-- M:M --> Authors
Books <-- M:1 --> Publishers

Env :

Hibernate 3.1
Mysql 4.1.8-nt

thanks.

moon


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 07, 2006 2:58 pm 
Beginner
Beginner

Joined: Mon Sep 27, 2004 11:51 pm
Posts: 22
After few days seaching , l still can't find an answer .

Below is my *hbm.xml files , hope that someone can point me to the right direction.

Book.hbm.xml,
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping >

<class name="org.yourschool.library.domain.Book"
   table="BOOKS" lazy="false">

   <id name="id"
      type="long"
      column="BOOK_ID"
      unsaved-value="null">
      <generator class="native"/>
   </id>
   
    <version name="version" type="long" column="VERSION"/>
   
   <property name="title"
      type="string"
      column="TITLE"
      length="255"
      not-null="true"/>

   <property name="publishingYear"
      type="integer"
      column="PUBLISHING_YEAR"/>

   <property name="isbn"
      type="string"
      column="ISBN"
      length="15"
      unique="true"/>

   <property name="edition"
      type="string"
      column="EDITION"
      length="30"/>

   <property name="description"
      type="string"
      column="DESCRIPTION"
      length="30"/>

   <property name="publicationPlace"
      type="string"
      column="PUBLICATION_PLACE"
      length="40"/>

   <property name="classMark"
      type="string"
      column="CLASSMARK"
        length="20"/>

   <property name="attachment"
      type="string"
      column="ATTACHMENT"
      length="32"/>

<!-- describe one-to-many association between Book and Item -->
   <set name="items"
        table="ITEMS"
        lazy="true"
        inverse="true"
        cascade="none"> <!-- reason for inverse="true" , see hibernate in action pg.110 -->

        <key column="BOOK_ID"/>

        <one-to-many
             class="org.yourschool.library.domain.Item"/>
   </set>

<!-- describe many-to-many association between Book and Author -->

   <set name="authors"
      table="LINK_BOOK_AUTHOR"
      lazy="true"
      cascade="none"> <!--link table-->

      <key column="BOOK_ID"/> <!-- MINE ! -->

      <many-to-many
         class="org.yourschool.library.domain.Author"
         column="AUTHOR_ID"/>
   </set>

<!-- describe many-to-many association between Book and Subject -->

   <set name="subjects"
      table="LINK_BOOK_SUBJECT"
      lazy="true"
      cascade="none"> <!--link table-->

      <key column="BOOK_ID"/> <!-- MINE ! -->

      <many-to-many
         class="org.yourschool.library.domain.Subject"
         column="SUBJECT_ID"/>
   </set>

<!-- describe many-to-many association between Book and Series -->

   <set name="seriez"
      table="LINK_BOOK_SERIES"
      lazy="true"
      cascade="none"> <!--link table-->

      <key column="BOOK_ID"/> <!-- MINE ! -->

      <many-to-many
         class="org.yourschool.library.domain.Series"
         column="SERIES_ID"/>
   </set>

<!-- describe many-to-many association between Book and OtherTitle -->

   <set name="otherTitles"
      table="LINK_BOOK_OTHERTITLE"
      lazy="true"
      cascade="none"> <!--link table-->

      <key column="BOOK_ID"/> <!-- MINE ! -->

      <many-to-many
         class="org.yourschool.library.domain.OtherTitle"
         column="OTHERTITLE_ID"/>
   </set>

<!-- describe one-to-many relationship between Book and Reservation -->

   <set name="reservations"
         table="RESERVATIONS"
         lazy="true"
         inverse="true"
         cascade="all">

      <key column="BOOK_ID"/>

      <one-to-many
         class="org.yourschool.library.domain.Reservation"/>
   </set>

<!-- describe one-to-one association between Book and Note -->

   <one-to-one name="note"
      class="org.yourschool.library.domain.Note"
      cascade="all"/>

<!-- describe many-to-one relationship between Book and Publisher -->

   <many-to-one name="publisher"
      class="org.yourschool.library.domain.Publisher"
      cascade="none"
      column="PUBLISHER_ID"
        not-null="false"/>

</class>

</hibernate-mapping>


Note.hbm.xml,
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>

   <class name="org.yourschool.library.domain.Note" table="NOTES"
      lazy="true">

      <id name="id"
         type="long"
         column="NOTE_ID">
         
         <generator class="foreign">
            <param name="property">book</param>
         </generator>
         
      </id>

      <version name="version" type="long" column="VERSION" />

      <property name="noteDetail"
         type="string"
         length="255"
         column="NOTE_DETAIL" />

      <!-- describe one-to-one relationship between Note and Book -->
      <one-to-one
         name="book"
         class="org.yourschool.library.domain.Book"
         cascade="none"
         constrained="true" />

   </class>

</hibernate-mapping>


Publisher.hbm.xml ,
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping >

<class name="org.yourschool.library.domain.Publisher"
   table="PUBLISHERS" lazy="true">

   <id name="id"
      type="long"
      column="PUBLISHER_ID"
      unsaved-value="null">
      <generator class="native"/>
   </id>

    <version name="version" type="long" column="VERSION"/>

   <property name="publisherName"
      type="string"
      column="PUBLISHER_NAME"
                length="64"
      not-null="true"
                unique="true"/>

<!-- describe one-to-many association between Publisher and Book -->
   <set name="books"
            table="BOOKS"
            lazy="true"
            inverse="true"
            cascade="none"> <!-- reason for inverse="true" , see hibernate in action pg.110 -->

              <key column="PUBLISHER_ID"/>

              <one-to-many
                  class="org.yourschool.library.domain.Book"/>
        </set>

</class>

</hibernate-mapping>


Author.hbm.xml ,
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping >

<class name="org.yourschool.library.domain.Author"
   table="AUTHORS" lazy="false">

   <id name="id"
      type="long"
      column="AUTHOR_ID"
      unsaved-value="null">
      <generator class="native"/>
   </id>

    <version name="version" type="long" column="VERSION"/>

   <property name="authorName"
      type="string"
      column="AUTHOR_NAME"
                length="64"
      not-null="true"
                unique="true"/>

<!-- describe many-to-many association between Author and Book -->
   <set name="books"
            table="LINK_BOOK_AUTHOR"
            lazy="true"
            inverse="true"
            cascade="save-update"> <!-- reason for inverse="true" , see hibernate in action pg.110 -->

              <key column="AUTHOR_ID"/>

              <many-to-many
                  class="org.yourschool.library.domain.Book"
                  column="BOOK_ID"/>
        </set>

</class>

</hibernate-mapping>


moon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 2:27 pm 
Beginner
Beginner

Joined: Mon Sep 27, 2004 11:51 pm
Posts: 22
This problem has been discussed before.

Related threads are ,

1. using projections is causing SQL query error on oracle (ORA-00904 error) http://opensource2.atlassian.com/projec ... se/HHH-817

2. select distinct entities using Criteria. http://forum.hibernate.org/viewforum.php?t=941669

3. Projections not working with property that is primary key. http://forum.hibernate.org/viewforum.php?t=940906

4. ProjectList and Criteria produces incorrect sql.
http://forum.hibernate.org/viewforum.php?t=941684

....etc.

Gavin commented that the generated sql is perfectly reasonable and a valid SQL and should be accepted and support by those vendors , including Oracle , MySQL , PostgreSQL , ... etc , but this decision cause Criteria API a pitfall when using Projection , it is so natural to write the code above and cause an error , then spend a few days to find an answer (if he/she is a beginner like me)....huh ~ .

OR

document it.

:)

moon


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.