JPA doesn't load everything heavily

I am having a problem using JPA 1.0 through the OpenJPA implementation. My data model consists of a trip that has a OneToMany relationship with a Leg and a OneToMany relationship with a passenger. Leg and Passenger has an association in PassengerLeg. This shows up as bidirectional OneToMany / ManyToOne. So essentially I have a diamond in my data model. If the trip has 2 legs and 3 passengers, there will be 6 passengers. For different use cases, I need to go in every direction from every object. Right now, when I try to load everything completely, the leg field in PassengerLeg will be null and I can't figure out why. Here is a lean view of my classes:

@Entity
public class Trip {

  @OneToMany(mappedBy = "trip", fetch = FetchType.EAGER)
  private List<Leg> legs;

  @OneToMany(mappedBy = "trip", fetch = FetchType.EAGER)
  private List<Passenger> passengers;

}

@Entity
public class Leg {

  @ManyToOne
  @JoinColumn(name = "TRIP_ID")
  private Trip trip;

  @OneToMany(mappedBy = "leg", fetch = FetchType.EAGER)
  private List<PassengerLeg> passengers;

}

@Entity
public class Passenger {

  @ManyToOne
  @JoinColumn(name = "TRIP_ID")
  private Trip trip;

  @OneToMany(mappedBy = "passenger", fetch = FetchType.EAGER)
  private List<PassengerLeg> legs;

}

@Entity
public class PassengerLeg {

  @ManyToOne
  @JoinColumn(name = "LEG_ID")
  private Leg leg; //this will be null

  @ManyToOne
  @JoinColumn(name = "PASSENGER_ID")
  private Passenger passenger;

}

      

I've spent countless hours reading the documentation and everything I can google to figure out what might be causing this, but I had no luck. Anyone have any ideas what might be causing this? Let me know if you need more information on classes / annotations.

+3


source to share


1 answer


JPA Never loads many sides as it can take a long time. Default fetch As mentioned in the JPA specs

  • One by One ---> Fetch Eager
  • One for Many ---> Fetch Lazy
  • Many to one ---> fetch Eager
  • Many for many ---> Fetch Lazy


Also, I will not advise you to go for Eager fetch as it is a heavy operation.

+6


source







All Articles