mappedBy 是指 Class 名称还是 Table 名称?
mappedBy refers to the Class Name or to the Table Name?
例如,当我们在@OneToMany 中使用mappedBy 注释时,我们提到了Class 名称还是Table 名称?
一个例子:
@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.
mappedBy
注释表示它标记的字段由关系的另一方拥有,在您的示例中是一对多关系的另一方。我不确切知道你的架构是什么,但以下 class 定义是有意义的:
@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;
}
例如,当我们在@OneToMany 中使用mappedBy 注释时,我们提到了Class 名称还是Table 名称?
一个例子:
@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.
mappedBy
注释表示它标记的字段由关系的另一方拥有,在您的示例中是一对多关系的另一方。我不确切知道你的架构是什么,但以下 class 定义是有意义的:
@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;
}