例如,当我们在@OneToMany中使用mappedBy注释时,我们是否提到了类名或表名?
一个例子:
@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; }
}
那么这两个中哪一个是正确的? :
谢谢!
答案 0 :(得分:3)
两者都不正确。来自documentation:
的mappedBy
public abstract java.lang.String mappedBy
拥有这种关系的领域。除非关系是单向的,否则是必需的。
mappedBy
注释表示它标记的字段由关系的另一侧拥有,在您的示例中是一对多关系的另一侧。我不确切知道你的架构是什么,但是下面的类定义是有道理的:
@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;
}