-->
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.  [ 1 post ] 
Author Message
 Post subject: ManyToMany relations and row limit
PostPosted: Tue Mar 16, 2010 9:20 am 
Newbie

Joined: Mon Mar 15, 2010 9:11 am
Posts: 2
Hi,

I'm using Hibernate in a Spring DM / OSGi environment. Works great, however I am running into an issue with ManyToMany relations when using limited result sets. Let me explain using an example.

Code:
public class Person {
   @Id
       @GeneratedValue(strategy = GenerationType.AUTO)
       @Column(name = "id", updatable = false, nullable = false)
       private String      id      = null;

   @ManyToMany(targetEntity = Address.class)
   @JoinTable(name = "PERS_ADDR_MAPPING",
      joinColumns = @JoinColumn(name = "PERS_ID"),
      inverseJoinColumns = @JoinColumn(name = "ADDR_ID")
   )
   List<Address> addresses;
}
Code:
public class Address{
   @Id
       @GeneratedValue(strategy = GenerationType.AUTO)
       @Column(name = "id", updatable = false, nullable = false)
       private String      id      = null;
}


Persisting all of this works well. We use Oracle as backend, end the PERS_ADDR_MAPPING table has the right values.

Let's say I persist the following data:
Person ID 1
Address ID 2
Address ID 2
And the relation is as following: Person 1 has Address 2 and Address 3.

Now, when I do a list of the Persons, I get two Persons, albeit both with the same ID. I get:

Person 1 with Address 2
Person 1 with Address 3

This can be solved by adding a Criteria.DISTINCT_ROOT_ENTITY to my DetachedCriteria. Now I get one Person object, with two Address objects in the Addresses Set. Perfect!


Now I want to use the data, and list the Persons in an UI. To do so, I want to use pages showing one Person per page. So, I modify my list and use for the first page:
Code:
List <Person> persons = getHibernateTemplate().findByCriteria(personCriteria, 0, 1);

This is where the things break. Using this approach I get one Person object, with only one Address. Even though both addresses are connected using the PERS_ADDR_MAPPING table. And it is explainable...the limit that I apply limits the result set to one row, thus limiting also the number of Addresses. This is how (left) joins work in Oracle.

So basically the question is: Is there any way to limit the number of Persons, but not the number of Addresses? Witout having to scan all the rows..

Thank you!
-Xandrios


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

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.