0

So I'm new to JPA and just trying to figure out the best practices for what I need. I have code the looks like the following Entity and when I use findAll() in the controller its automatically pulling the data I want from Equipment and Validation. Whats the proper way to just retrieve Equipment though if I dont Validation. The full database is much larger than this with many more joins so there's an ask to be able to just retrieve the data within a single table also.

@Entity
@Table()
public class Equipment{
   @Id
   @Column(name="eqpmnt_id")
   Integer equipmentId;
   
   @Column()
   String equipmentName;
   
   @OneToOne()
   @JoinColumn(name="eqpmnt_id")
   Validation validation;
Travis Frazier
  • 441
  • 3
  • 13
  • 1
    Set the [FetchType](https://docs.oracle.com/javaee/6/api/javax/persistence/FetchType.html) of the relationship to Lazy: `@OneToOne(fetch = FetchType.LAZY)` – OH GOD SPIDERS Jul 13 '22 at 13:05
  • @OHGODSPIDERS, does LAZY work with OneToOne? – Asgar Jul 13 '22 at 13:08
  • @Asgar It works with any relationship, doesn't matter if OneToOne, OneToMany or ManyToOne. Everyone of those 3 JPA annotations has an attribute fetch that by default is set to EAGER if not specified by the user itself. – OH GOD SPIDERS Jul 13 '22 at 13:11
  • @OH GOD SPIDERS How does that work? I would like to be able to retrieve everything and just the table in separate calls – Travis Frazier Jul 13 '22 at 13:12
  • 1
    See [this](https://stackoverflow.com/questions/1444227/how-can-i-make-a-jpa-onetoone-relation-lazy) – Asgar Jul 13 '22 at 13:12
  • If you use Spring Data JPA, try [Projections](https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections) to select only what you need in controller. – Andrey Belyaev Jul 20 '22 at 07:16

0 Answers0