例如,当我们在@OneToMany中使用mapledBy批注时,是否提到了类名或表名?
例子:
@Entity
@Table(name = "customer_tab")
public class Customer {
@Id @GeneratedValue public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
private Integer id;
@OneToMany(mappedBy="customer_tab")
@OrderColumn(name="orders_index")
public List<Order> getOrders() { return orders; }
}
那么,这两个中的哪一个是正确的呢? :
> @OneToMany(mappedBy =“ customer_tab”)
> @OneToMany(mappedBy =“ Customer”)吗?
谢谢!
解决方法:
都不正确.从documentation开始:
mappedBy
public abstract java.lang.String mappedBy
The field that owns the relationship. Required unless the relationship is unidirectional.
mapledBy批注指示它标记的字段由关系的另一端拥有,在您的示例中为一对多关系的另一端.我不完全知道您的架构是什么,但是以下类定义才有意义:
@Entity
@Table(name = "customer_tab")
public class Customer {
@OneToMany(mappedBy="customer")
@OrderColumn(name="orders_index")
public List<Order> getOrders() { return orders; }
}
@Entity
public class Order {
@ManyToOne
@JoinColumn(name = "customerId")
// the name of this field should match the name specified
// in your mappedBy annotation in the Customer class
private Customer customer;
}