在Spring Data中如何通过外键值而不是join来读取实体?

时间:2017-03-27 19:29:37

标签: mysql spring jpa spring-data spring-data-jpa

我正在使用Spring Data而且我很怀疑为什么即使将外部实体声明为Lazy加载后,他们也会急切地加载此方法:

@Entity
@Table(name = "USER_HIERARCHY")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class UserHierarchy {

  @Id
  @GeneratedValue
  private Long id;

  @ManyToOne(fetch = FetchType.LAZY) // LAZY LOADING
  @JoinColumn(name = "USER_ID",referencedColumnName = "ID")
  private User user;

  @ManyToOne(fetch = FetchType.LAZY) //LAZY LOADING
  @JoinColumn(name = "REPORTING_MANAGER_ID", referencedColumnName = "ID")
  private User reportingManager;

  @Column(name = "LEVEL")
  private Integer level;


  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public User getUser() {
    return user;
  }

  public void setUser(User user) {
    this.user = user;
  }

  public User getReportingManager() {
    return reportingManager;
  }

  public void setReportingManager(User reportingManager) {
    this.reportingManager = reportingManager;
  }

  public Integer getLevel() {
    return level;
  }

  public void setLevel(Integer level) {
    this.level = level;
  }

  @Override
  public String toString() {
    return ReflectionToStringBuilder.toStringExclude(this, "user", "reportingManager");
  }

在日志中,我可以将查询看作:

@Entity
@Table(name = "USER")
public class User {

  @Id
  @GeneratedValue
  private Long id;

  @Column(name = "EMAIL")
  private String email;

  @Column(name = "CUSTOMER_ID")
  private Long customerId;

  @Column(name = "STATUS")
  private String status;

  // Getter and Setter

为什么即使我通过报告经理ID,也需要额外加入?

UserHierarchy Class:

pair<int, int> a;
pair<int, pair<int, int> > b;
a=make_pair(1,2);
b=make_pair(3,a);
cout<<b.first<<b.second.first<<b.second.second;

用户实体类

func animateExplosion(sender: UIButton){

    let star = SKSpriteNode(imageNamed: "ExplodingStar")
    let starHeight = sender.frame.height / 3
    star.size = CGSize(width: starHeight, height: starHeight)
    var position = sender.frame.origin
   position = self.sceneScene.convertPoint(fromView: position)
    star.position = position
    let minimumDuration = 1
    let maximumDuration = 2
    let randomDuration = TimeInterval(RandomInt(min: minimumDuration * 100, max: maximumDuration * 100) / 100)
    let fireAtWill = SKAction.move(to: getRandomPosition(view: sender), duration: randomDuration)
    let rotation = SKAction.rotate(byAngle: CGFloat(randomAngle()), duration: Double(randomDuration))
    let fadeOut = SKAction.fadeOut(withDuration: randomDuration)
    let scaleTo = SKAction.scale(to: starHeight * 2, duration: randomDuration)
    let group = SKAction.group([fireAtWill, rotation]) //for testing no fade out or remove parent
    let sequence = SKAction.sequence([group])

    if randomAmountOfExplodingStars > 0{
        randomAmountOfExplodingStars -= 1
        sceneScene.addChild(star)
        star.run(sequence)
        animateExplosion(sender: sender)
    }
}

根据proposal

  

在创建查询时,您已确保已解析的属性   是托管域类的属性。

那么这是否意味着为了使User对象处于“托管状态”它使用join或我在实现中出错?

1 个答案:

答案 0 :(得分:1)

我最近遇到了同样的问题,而且目前在Spring Data中似乎没有解决方案。

但是我为它创建了ticket

如果您使用Criteria API或JPQL进行此特定查询,那么它将正常工作。