Issue
I have a class Health Check
- as part of the class I record how many parasites are seen (enum NONE, SOME, MANY) and also the location of the parasites (enum HEAD, FEET, BODY).
Two ways this could be done:
METHOD 1
@Entity
public class HealthCheck {
@Id
@GeneratedValue(strategy = AUTO)
private Long id;
private Parasite parasite;
Parasite
public class Parasite {
private BodyLocation bodyLocation;
private Coverage coverage;
}
Or I could have:
METHOD 2
@Entity
public class HealthCheck {
@Id
@GeneratedValue(strategy = AUTO)
private Long id;
private ParasiteNumber parasiteNumber;
private ParasiteLocation parasiteLocation;
Would method 1 require @Entity on parasite class and a entry in the table for each Health Check
and a @OneToOne annotation?
Note Parasite class is not used anywhere else. Its only a part of a Health Check
.
Which way is correct?
Solution
Generally speaking, yes. In ORM (aka JPA or Hibernate), you are building a graph of objects that represent things in your database. Anything that one @Entity touches is also an @Entity because it's a graph.
Whether it's a @OneToOne or a @ManyToOne, etc, depends on the nature of your data model. But, keep in mind, those connections can also be @Lazy, so they are not loaded until they are needed.
Because of @Lazy, method 2 might be preferred, idk. I assume ParasiteLocation and ParasiteNumber is some sort of join-table. If that's the case, you could load a HealthCheck with its PL and PN, but those objects could be Lazy to Parasite.
I don't think there is a one-size-fits-all answer to your question. It very much depends. But good news, ORM is flexible to cover any/all scenario you might have.
Answered By - Jeff Bennett
Answer Checked By - Pedro (JavaFixing Volunteer)