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