19

Lets say Car has a List of Wheel and the same time Wheel have a Car reference to the owner.

public class Car{
List<Wheel> wheels;

}

public class Wheel{
Car owner;
}

I ask this because in JPA is easy to operate like this rather than query again the database for getting the parent entity.

  • 3
    No, it isn't an anti pattern, but you should only do this if you need them, if you don't there is no need to have it, specially the car->wheels relationship as it can easily cause 1:N queries to the database. – Maurício Linhares Jun 13 '12 at 01:57

2 Answers2

11

Bidirectional relationships like this are perfectly valid and expected in JPA. That's the whole point of the mappedBy property. This post has a good explanation:

In a bidirectional JPA OneToMany/ManyToOne association, what is meant by "the inverse side of the association"?

Community
  • 1
  • 1
wrschneider
  • 17,913
  • 16
  • 96
  • 176
4

This certainly isn't an anti pattern.

You might even say this is very convenient that it's so easy to implement this with JPA. Two possible queries become very trivial; getting all wheels from a certain car and getting the car for a certain wheel.

Tom
  • 4,096
  • 2
  • 24
  • 38