2010-10-25 106 views
43

我在設置註釋對象中的一對多關係時遇到問題。mappedBy引用未知的目標實體屬性

我有以下幾點:

@MappedSuperclass 
public abstract class MappedModel 
{ 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    @Column(name="id",nullable=false,unique=true) 
    private Long mId; 

那麼這

@Entity 
@Table(name="customer") 
public class Customer extends MappedModel implements Serializable 
{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = -2543425088717298236L; 


    /** The collection of stores. */ 
    @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 
    private Collection<Store> stores; 

@Entity 
@Table(name="store") 
public class Store extends MappedModel implements Serializable 
{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = -9017650847571487336L; 

    /** many stores have a single customer **/ 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn (name="customer_id",referencedColumnName="id",nullable=false,unique=true) 
    private Customer mCustomer; 

我在做什麼這裏不正確

回答

76

mappedBy屬性爲r電話customer,而財產是mCustomer,因此錯誤消息。因此,要麼改變你的映射爲:

/** The collection of stores. */ 
@OneToMany(mappedBy = "mCustomer", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 
private Collection<Store> stores; 

或者實體財產轉爲customer(這是我會做什麼)。

mappedBy引用指示「查看名爲'customer'的bean屬性,以查找具有該集合的事物以查找配置。」

+0

工作,我曾預計它使用反射使用setter或getter方法,而不是直接屬性。 – boyd4715 2010-10-25 03:11:59

+0

@ boyd4715:您可以嘗試在getter上移動註釋,以查看使用屬性訪問(vs字段訪問)時會發生什麼情況。另一方面,'mappedBy'的javadoc表示*擁有關係的字段*,所以我不確定這會改變什麼。 – 2010-10-25 03:24:03

+0

工作,感謝您的快速澄清 – 2016-02-23 06:29:24

相關問題