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