-->
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.  [ 5 posts ] 
Author Message
 Post subject: Criteria API + restriction on association = inner join
PostPosted: Sat May 07, 2005 4:21 pm 
Newbie

Joined: Thu May 05, 2005 6:35 pm
Posts: 3
Hi!

In my object model Product has one name and zero or more secondary names (synonyms) mapped as unidirectional one-to-many. Using Criteria
API I try to put resriction on synonyms, and I expect products without synonyms at all to be included in results also, but Hibernate makes this impossible by generating "inner join" instead of "left outer join".

Is this behavior by design? Any workarounds?

Hibernate version:
3.02

Mapping documents:
Code:
<class name="mydomain.om.Product" table="products" >
    <id name="id" column="id" access="field">
      <generator class="assigned"/>
    </id>
    <property name="name" access="field"/>
    <bag name="synonyms" access="field" order-by="id asc" outer-join="true" >
      <key column="productId" />
      <one-to-many class="ua.spectruminfo.om.Synonym"/>
    </bag>
  </class>
  <class name="mydomain.om.Synonym" table="syns" >
    <id name="id" access="field">
      <generator class="native"/>
    </id>
    <property name="name"  access="field"/>
  </class>


Code between sessionFactory.openSession() and session.close():
Code:
Criteria crit = session.createCriteria(Product.class);
crit.createCriteria("synonyms").add(
  Restrictions.or(
    Restrictions.like("name","%"+query.getSynonym()+"%"),
    Restrictions.isNull("name") // <-----this should give me products w/o synonyms too
  )
);
List products = crit.list()


Name and version of the database you are using:
MySQL 4.0

The generated SQL (show_sql=true):
Code:
select this_.id as id2_, this_.name as name1_2_, synonym2_.id as id0_, synonym2_.name as name5_0_ from products this_ inner join syns synonym2_ on this_.id=synonym2_.productId where synonym2_.name like ? or synonym2_.name is null limit ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 9:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
the nested createCriteria() call results in the inner join. This makes perfect sense if you stop and think about the nature of this statement.

You really want createAlias().

http://www.hibernate.org/hib_docs/reference/en/html/querycriteria.html#querycriteria-associations


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 12:42 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Steve, the createCriteria will also result in an inner join.

To the original poster, based on the query you build up, the generated sql is exactly what i'd expect.

Are you trying to find records that don't have any synonym records? If so you can try the isEmpty() Restriction although the join will still most likely cause you grief.

AFAIK, outer join querying isn't supported in Criteria API.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 5:51 pm 
Newbie

Joined: Thu May 05, 2005 6:35 pm
Posts: 3
Yes. createAlias() and createCriteria() both give inner join.

What I want actually are products with synonyms matching user's query AND products without synonyms at all in ONE result set (this is reuirement because in real system restriction on synonyms will be ORed with restrictions on other product fields). This can be accomplished only with outer join.

If VampBoy is right, it seems that I have to move to HQL. Pity because I really like OO approach of Criteria API


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 6:04 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Here is a bit of history:

http://forum.hibernate.org/viewtopic.ph ... highlight=


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