Reputation: 41
I have two legacy tables(Person, Car) in my application. One of them is Person.
@Entity Person
@OneToMany Collection cars.
Now the car table has three columns(id, carid, personid). Ideally speaking carid should be primary key (assuming a person has only one car). But in my legacy table id is primary key.So multiple records got inserted into the table for the same personid and carid(a bug which needs to be fixed)
But my questions is there a way I can retrieve distinct(personid+carid) for a person when I call person.getCars()? Basically I don't want duplicate entries in my person.getCars() collection
I am using jpa2 named queries. select p from Person p; p.getCars().size() - to retrieve car list(this returns duplicates as well)
JPA provider - hibernate
Upvotes: 0
Views: 2187
Reputation: 7048
You could make the cars collection a Set
, and make sure Car has good equals()
and hashCode()
methods so the set won't contain duplicate cars.
Upvotes: 1