Hibernate双向多对一关联错误

时间:2011-11-27 18:37:15

标签: hibernate hibernate-mapping

我有两个类我试图关联,在运行时我得到以下异常:

org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: BidProposal.parents in Parent.bidproposals

任何人都可以在这里找到错误,我已经花了好几个小时......

这些课程如下:

Parent.Java:

@Entity
@Table(name = "parents")
public class Parent extends User implements java.io.Serializable {
  private Integer id;
  private Set<BidProposal> bidproposals = new HashSet<BidProposal>(0);

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
    return this.id;
    }

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

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parents")
    public Set<BidProposal> getBidproposals() {
    return this.bidproposals;
    }

    public void setBidproposals(Set<BidProposal> bidproposals) {
    this.bidproposals = bidproposals;
    }

}

BidProposal.Java:

@Entity
@Table(name = "bidproposal")
public class BidProposal implements java.io.Serializable {

    private Integer id;
    private Parent parent;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
    return this.id;
    }

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id", nullable = false)
    public Parent getParent() {
    return this.parent;
    }

    public void setParent(Parent parent) {
    this.parent = parent;
    }
}

1 个答案:

答案 0 :(得分:2)

  

org.hibernate.AnnotationException:mappedBy引用未知   目标实体属性:Parent.bidproposals中的BidProposal.parents

这意味着在 BidProposal 实体中找不到父母属性。

应该是:

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent")   
public Set<BidProposal> getBidproposals() {    
   return this.bidproposals;  
}