【问题标题】:Hibernate: How to join list of an entity within other entity? (referenced property unknown)Hibernate:如何在其他实体中加入实体列表? (引用属性未知)
【发布时间】:2019-04-18 07:44:48
【问题描述】:

如何在其他实体中加入实体列表,我收到以下错误“引用的属性未知”?

包含 Ghoda 列表的实体一:

@Entity
@Table(name = "Case")
public class Case 
  @Enumerated(EnumType.STRING)
  @OneToOne(mappedBy = "case", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  public List<Ghoda> getGhodaHistory() {
    return ghodaHistory;
  }

  public void setGhodaHistory(List<Ghoda> ghodaHistory) {
    this.ghodaHistory= ghodaHistory;
  }

实体 2 是 Ghoda 表本身:

@Entity
@Table(name = "Ghoda")
public class Ghoda{
  @OneToOne
  @JoinColumn(name = "case_ID")
  public Case getCase() {
      return case;
  }

  public void setCase(Case case) {
      this.case= case;
  }  
}

当我尝试部署它时,我得到以下异常:

Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: de.bokla.model.Case.ghodaHistory, referenced property unknown: java.util.List.case

我不熟悉hibernate,我根据现有代码创建了这个,无法找到错误所在,有人可以建议如何解决这个问题吗?

【问题讨论】:

标签: java hibernate orm hibernate-mapping


【解决方案1】:

你需要通过这种方式使用@OneToMany映射

@Entity
@Table(name = "Case")
public class Case 

  @OneToMany(mappedBy = "case", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  public List<Ghoda> getGhodaHistory() {
    return ghodaHistory;
  }

  public void setGhodaHistory(List<Ghoda> ghodaHistory) {
    this.ghodaHistory= ghodaHistory;
  }

}

@Entity
@Table(name = "Ghoda")
public class Ghoda{

  @ManyToOne
  @JoinColumn(name = "case_ID")
  public Case getCase() {
      return case;
  }

  public void setCase(Case case) {
      this.case= case;
  }

}

最简单的方法

@Entity
@Table(name = "Case")
public class Case 

  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  @JoinColumn("case_ID")
  public List<Ghoda> getGhodaHistory() {
    return ghodaHistory;
  }

  public void setGhodaHistory(List<Ghoda> ghodaHistory) {
    this.ghodaHistory= ghodaHistory;
  }

}

@Entity
@Table(name = "Ghoda")
public class Ghoda{

}

https://stackoverflow.com/a/37542849/3405171

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多