-->
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 query using subselect fetch method
PostPosted: Fri Sep 21, 2007 12:36 pm 
Newbie

Joined: Fri Sep 21, 2007 11:54 am
Posts: 9
Location: Switzerland
Dear users,

I would like to use subselect fetch mode to avoid a cartesian product during a Criteria query. I read informations about this problem in the "Java Persistence with Hibernate" book and I first thought i found the solution on page 588 (The cartesian product problem) but it finally seems it does not work as expected.

I'm trying to fetch informations as follow:
Code:
List<alert> alerts = session.createCriteria(Alert.class)
.createCriteria("sources")
.createCriteria("addresses")
.add(Expression.like("address", "10.192.%"))
.list();


In a DB structure where an alert contain a set of sources which could contain a set of addresses. Addresses are linked using an association table (N..N) as shown in the mapping below:


Code:
<class name="Alert" table="alert">
  <set name="sources" table="source_instance" fetch="subselect">
    <key column="_alert_oid" />
    <one-to-many class="Source"/>
  </set>
</class>

<class name="Source" table="source_instance">
  <id name="oid" column="_oid" type="int" />
  <property .. some properties .. />
  <set name="addresses" table="address_association"
   fetch="subselect">
    <key column="_node_oid" />
    <many-to-many column="_address_oid" class="Address"/>
  </set>
</class>

<class name="Address" table="address">
  <id name="oid" column="_oid" type="int" />
  <property name="ident" column="ident" type="string" />
  <property name="address" column="address" type="string" />
</class>


As said before, even with the fetch="subselect" mode activated I always retrieve the cartesian product of matched addresses. I mean that if i have 2 alerts into the DB, which both of them contains 2 addresses (1 alert contain 2 addresses), i always retrieve 4 alerts and not the 2 expected (alerts are built from the address without checking for distinct values)...

Any advise or tip is welcome !

Thanks in advance.

Joël


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 6:48 pm 
Newbie

Joined: Fri Sep 21, 2007 11:54 am
Posts: 9
Location: Switzerland
After an advice from Steve, who told me to look for "distinct" statement to solve my problem, I did a bit of googeling on this topic. Because I don't want to use distinct at software layer (and to get 1000 of non-distinct object which need to be filtered using a setResultTransformer) I tryed to find some information about distinct statement at DB layer (using criteria). Here are the most interesting information i found:
- http://hibernate.bluemars.net/117.html?cmd=prntdoc#A12
- http://opensource.atlassian.com/projects/hibernate/browse/HHH-1411

Reading the second post, It seems that a low level (DB layer) distinct exist, but i'm not able to find/use it... Maybe someone have an idea about this ?

On the other hand i learned (from the first link above) that getting the same object multiple times at software layer was not so bad because on duplicated object, references were returned and only the "distinct number of results" were instanced. I'm not sure if i understood it well so i will be happy to have a feedback about this conclusion !?

Thanks in advance.

Joël


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 22, 2007 7:40 am 
Newbie

Joined: Fri Sep 21, 2007 11:54 am
Posts: 9
Location: Switzerland
Still some thoughts about my problem...

Using a distinct statement at software layer does not really solve the problem of the first Hibernate query which does a cartesian product (problem explained on page 588, in "Java Persistence with Hibernate" book) with many redundant data !! The main problem is that this cartesian product is always done (even if the fetch method is set on "subselect" or "select").

So any tip is still welcome !

Thanks a lot !

Joël


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 23, 2007 5:48 pm 
Newbie

Joined: Fri Sep 21, 2007 11:54 am
Posts: 9
Location: Switzerland
It's me again...

After some testing, I'm not sure (regarding my last post) if the problem is really related to a cartesian product. I think it is more related to data structure building. Because for each extracted address, it seems that Hibernate build its related root object (Alert.class) even if some address are related to the same source. That means that if the following data are in the DB:

Code:
Alert(Source(127.0.0.1, 10.192.72.2))

Two same alerts will be created (and both of them will have 127.0.0.1 and 10.192.72.2 source address).

So it is not really related to a cartesian product problem ? am i right ?
It is more related on how Hibernate build Object using Set mapping ?

Thanks in advance !


Joël


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.