0

How to join list of an entity within other entity and i am getting following error " referenced property unknown" ?

Entity one which is including list of 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;
  }

Entity 2 is Ghoda table itself:

@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;
  }  
}

When i try to deploy this then i get following exception:

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

I am not well versed with hibernate and i have created this according to existing code and unable to find where error lies, could anyone suggest how to fix this?

fatherazrael
  • 5,511
  • 16
  • 71
  • 155

1 Answers1

1

You need to use @OneToMany mapping by this way

@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;
  }

}

The simplest way

@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

v.ladynev
  • 19,275
  • 8
  • 46
  • 67