Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] To query an entity with filters on a OneToMany attribute gives a weird result

In an Object-Relational world, retrieving a Person and only some of it's addresses does not really make sense. If that kind of thing is allowed, you cannot trust the instance of Person you have found to represent the Person as it is represented in the database. (i.e. If you use that instance of Person later, how do you know what query was used to retrieve it and what parts of the addresses collection are properly populated)

To get a subset of the addresses, you should query for just addresses.

select a From Person p join p.addresses a where......

-Tom

On 06/12/2011 12:36 PM, david.haccoun@xxxxxxx wrote:
Hello,

I use  EclipseLink for 9 months and so far no problem. Since I have the
need to query an entity with a OneToMany attribute, it's all the contrary.
It gives me a strange result.
I have simplified my entities until the maximum but the problem remains.

I will explain my need which is ultra simple :  I have two entities :
Person which has a bidirectional relation with Address.
Person has potentially several Addresses but an Address belongs to one and
only Person.

In Classes, it gives that :

@Entity
public class Person implements Serializable {

     @Id
     private Long id;

     @OneToMany(mappedBy = "person", fetch = FetchType.LAZY)
     private Set<Address>  addresses;

// Getter and setter
...

}


@Entity
public class Address implements Serializable {

      @Id
     private String idAddress;

     @ManyToOne(fetch = FetchType.LAZY)
     @JoinColumn(name = "idPerson", referencedColumnName = "idPerson")
     private Person person;

// Getter and setter
...
}

I want to query personne with their adresses. All that with some conditions
on personne and adresse.
My simplified query  "select pers FROM Person pers join pers.addresses
address
                      where pers.matricule=:matricule  and
address.date=:dateContract"

When i execute it, i retrieve the right person but with all addresses
linked (with foreign key) with this person. Even the addresses which don't
match with the dateContract condition.

It seems that it's a problem related to the use of filtering on a oneToMany
attribute in my query. The problem is solved if i do several requests but
it will give low performances as I have several requests like this.
I have tried with the oneToMany in eager initialization and with a
fetch-join query hint but i have got the same result.

Thank you for having read me :)

PS : I have written the code manually, so a little typo is not impossible

David


_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top