JPA不急于加载一切 [英] JPA Not Eagerly Loading Everything

查看:172
本文介绍了JPA不急于加载一切的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过OpenJPA实现利用JPA 1.0时遇到问题。我的数据模型包括一个与Leg有OneToMany关系的Trip和一个与Passenger的OneToMany关系。 Leg和Passenger在PassengerLeg中有一个关联。这被映射为双向OneToMany / ManyToOne。基本上我的数据模型中有一颗钻石。如果旅行有2条腿和3名乘客,将有6个乘客腿。对于各种用例,我需要从每个实体的每个方向。现在,当我试图急切地加载所有内容时,PassengerLeg中的leg字段将为null,我无法弄清楚原因。以下是我课程的简洁表示:

I am having a problem leveraging JPA 1.0 via OpenJPA implementation. My data model consists of a Trip which has a OneToMany relationship with Leg and a OneToMany relationship with Passenger. Leg and Passenger have an assocation in PassengerLeg. This is mapped as bidirectional OneToMany/ManyToOne. So essentially I have a diamond in my data model. If a trip has 2 legs and 3 passengers, there will be 6 passengerLegs. For various use cases I have needs to go each direction from each entity. Right now, when I attempt to eagerly load everything, the leg field in PassengerLeg will be null and I cannot figure out why. Here is a skimpy representation 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 documentation and anything I can find on Google to figure out what might cause this, but I have not had any luck. Anyone have any ideas what would cause this? Let me know if you need any more information about classes/annotations.

推荐答案

JPA从不热切地加载许多方面,因为它可以是时间消耗操作。默认提取正如JPA规范中所述

JPA Never loads many side eagerly since it can be time consuming operation. Default fetch As mentioned in JPA Specs


  1. 一对一--->提取渴望

  2. 一许多人 - >获取懒惰

  3. 多对一--->获取渴望

  4. 多对多 - >获取懒惰

  1. One To One --->Fetch Eager
  2. One To Many ---> Fetch Lazy
  3. Many to one---> fetch Eager
  4. Many To Many---> Fetch Lazy

我也不会建议你去Eager fetch,因为它是重型操作

also I will not advice you to go for Eager fetch since it is heavy operation

这篇关于JPA不急于加载一切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆