-->
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.  [ 4 posts ] 
Author Message
 Post subject: criteria generate bad sql on multiple N:M relationships
PostPosted: Mon Jan 11, 2010 9:55 am 
Newbie

Joined: Mon Jul 28, 2008 4:14 am
Posts: 3
Hi all

I found a problem in java with Hibernate 3 and Oracle 10g related to queries built on multiple N:M relationships, when the corresponding join tables have columns with the same name. It seems that the Criteria class generates SQL code that Oracle rejects with an exception of type "ORA-00918: column ambiguously defined". This is because Hibernate adds on the select clause duplicated and not required columns aliases.

This issue was already signaled on this forum with no replies at all (https://forum.hibernate.org/viewtopic.php?f=25&t=998515&start=0).

If there are no bugs in the Criteria I'd like to know the correct way to define/use it in a way that does not offend Oracle.
Otherwise this must be considered a bug: has anyone any useful information about it?

Instead of posting a long message, trying to describe the problem in a detailed way, I may send a very very simple Eclipse project that demonstrates the issue to anyone interested in (I don't know why but I cannot attach any file from my user in this forum ...) .
In few words:
  • a class A has 2 N:M relationships with a class B and a class C
  • A, B and C are mapped on Oracle tables with the same names
  • the relationships A->B and A->C are mapped on join tables AB and AC having the expected foreign keys: AB[a_id,b_id], AC[a_id,c_id]
  • if you use Hibernate Criteria for querying the DB about A objects having restrictions on both B and C sub-objects, Oracle complains that the generated query is ambiguous

The exception and the SQL generated are the following, with the duplicated columns highlighted in bold:
Quote:
Hibernate: select * from ( select row_.*, rownum rownum_ from ( select this_.id as id0_2_, this_.a as a0_2_, bset4_.a_id as a1_, b1_.id as b2_, b1_.id as id3_0_, b1_.b as b3_0_, cset6_.a_id as a1_, c2_.id as c2_, c2_.id as id4_1_, c2_.c as c4_1_ from a this_, ab bset4_, b b1_, ac cset6_, c c2_ where this_.id=bset4_.a_id and bset4_.b_id=b1_.id and this_.id=cset6_.a_id and cset6_.c_id=c2_.id and lower(this_.a) like ? and b1_.b>? and c2_.c between ? and ? ) row_ ) where rownum_ <= ? and rownum_ > ?
11-gen-2010 12.36.56 org.hibernate.util.JDBCExceptionReporter logExceptions AVVERTENZA: SQL Error: 918, SQLState: 42000
11-gen-2010 12.36.56 org.hibernate.util.JDBCExceptionReporter logExceptions GRAVE: ORA-00918: column ambiguously defined

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query


The Hibernate mapping file used is the following:
Code:
<?xml version='1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

   <class name="test.A" table="a">

      <id name="id" column="id">
         <generator class="native" />
      </id>

      <property name="a" column="a" type="java.lang.String"/>
      
        <set name="bSet" cascade="all" table="ab">
             <key column="a_id"/>
             <many-to-many unique="true" column="b_id" class="test.B" />
        </set>

        <set name="cSet" cascade="all" table="ac">
             <key column="a_id"/>
             <many-to-many unique="true" column="c_id" class="test.C" />
        </set>

   </class>

   <class name="test.B" table="b">
      <id name="id" column="id" />
      <property name="b" column="b" type="java.lang.Integer" />
   </class>

   <class name="test.C" table="c">
      <id name="id" column="id" />
      <property name="c" column="c" type="java.lang.Double" />
   </class>

</hibernate-mapping>


Any help will be greatly appreciated.

Thanks all

Fabrizio Casali


Top
 Profile  
 
 Post subject: Re: criteria generate bad sql on multiple N:M relationships
PostPosted: Tue Jan 12, 2010 1:55 pm 
Newbie

Joined: Wed Dec 23, 2009 12:38 pm
Posts: 14
Hi

can you please post your Criteria ? I tried following and I am not getting any error.

Criteria crit = session.createCriteria(Category.class).createAlias("types","type").createAlias("areas", "area")
.add(Restrictions.like("type.name", "first")).add(Restrictions.like("area.name", "first"));


Generated SQL

select this_.id as id0_2_, this_.name as name0_2_, types4_.categoryId as categoryId, type1_.id as typeId, type1_.id as id3_0_, type1_.name as name3_0_, areas6_.categoryId as categoryId, area2_.id as areaId, area2_.id as id4_1_, area2_.name as name4_1_ from MyCateogry this_, categoryType types4_, MyType type1_, categoryAreas areas6_, MyArea area2_ where this_.id=types4_.categoryId and types4_.typeId=type1_.id and this_.id=areas6_.categoryId and areas6_.areaId=area2_.id and type1_.name like ? and area2_.name like ?


Here is the hbm file.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.example.association.manytomany.Category"
table="MyCateogry">

<id name="id"><generator class="native" /></id>
<property name="name" />

<set name="types" cascade="all" table="categoryType">
<key column="categoryId" />
<many-to-many unique="true" column="typeId"
class="com.example.association.manytomany.Type" />
</set>

<set name="areas" cascade="all" table="categoryAreas">
<key column="categoryId" />
<many-to-many unique="true" column="areaId"
class="com.example.association.manytomany.Area" />
</set>

</class>

<!-- Type and Area are simple classes hence ommited -->

</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: criteria generate bad sql on multiple N:M relationships
PostPosted: Wed Jan 13, 2010 5:50 am 
Newbie

Joined: Mon Jul 28, 2008 4:14 am
Posts: 3
Thank you so much for your reply

Yes, you're right: a simple query does not produce the error. I forgot to specify that the result has to be paginated onto the database.

If you review the query I signaled in the previous post you can see the use of rownum. So, if you add to your criteria the firstResult and maxResult properties you will see the exception.
This is due to the particular method used by Oracle for paging through the results: it encloses the original query into 2 nested queries "select * from ..." where the repeated column aliases go bad:
Quote:
select * from ( select row_.*, rownum rownum_ from (<YOUR QUERY>) row_ ) where rownum_ <= <MAX> and rownum_ > <MIN>


You also have the duplicated column in your query:
Quote:
... types4_.categoryId as categoryId, ... areas6_.categoryId as categoryId, ...


With other DB the exception does not raise: I tried with MySQL and it's OK, because for paging it uses another method
Quote:
<YOUR QUERY> limit <MIN>, <MAX>


However, for completeness, the following is the incredibly complex code of the main method I used for produce the error :))

Code:
   public static void main(String[] args) {
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
      //
      Criteria criteria = session.createCriteria(A.class);
      criteria.add(Restrictions.ilike("a", "%test%"));
      criteria.createCriteria("bSet").add(Restrictions.gt("b", Integer.valueOf(10)));
      criteria.createCriteria("cSet").add(Restrictions.between("c", Double.valueOf(0.1), Double.valueOf(0.9)));
      criteria.setFirstResult(1);
      criteria.setFetchSize(2);
      criteria.setMaxResults(2);
      criteria.list();
      session.getTransaction().commit();
      //
      HibernateUtil.getSessionFactory().close();
   }


Bye

Fabrizio


Top
 Profile  
 
 Post subject: Re: criteria generate bad sql on multiple N:M relationships
PostPosted: Tue Oct 01, 2013 2:41 pm 
Newbie

Joined: Tue Oct 01, 2013 2:35 pm
Posts: 1
Hello Fabrix,

Did you find any solution for this problem ? I am having the same error but don't know how to resolve it without changing hibernate version

Regards,
Mitesh Pandey


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.