我在被注释的对象中建立一对多关系时遇到问题。
我有以下几点:
@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;
我在这里做错了什么
该mappedBy属性正在引用,customer而该属性是mCustomer,因此出现错误消息。因此,请将您的映射更改为:
mappedBy
customer
mCustomer
/** The collection of stores. */ @OneToMany(mappedBy = "mCustomer", cascade = CascadeType.ALL, fetch = FetchType.LAZY) private Collection<Store> stores;
或者将实体属性更改为customer(这就是我要做的)。
mapledBy参考指示“在我有一个用于查找配置的集合的东西上查找名为’customer’的bean属性。”