Using JoinColumn, JPA using oneToMany mappedBy
2011-05-30 22:11
369 查看
有两个实体Order和Customer,一个用户可以有多个订单,一个订单属于一个用户,所以Customer与Order是一对多的关系,那么在Order实体里可以有一个到Customer的引用customer,同时在Customer里有一个Order实体的集合orders。如果在只是如下这样写:
public class Order {
@id
private long id;
@ManyToOne
private Customer customer;
public void setId(long id) {
this.id = id;
}
public long getId() {
return this.id;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Customer getCustomer() {
return this.customer;
}
}
public class Customer{
@id
private long id;
@OneToMany
private Set<Order> orders;
public void setId(long id) {
this.id = id;
}
public long getId() {
return this.id;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
public Set<Order> getOrders() {
return this.orders;
}
}
那么系统可能会自动生成一张Customer_Order关联表,可是事实上我们不需要这样的表,因为通过 Select order from Order order where order.customer.id=:id, 就可以找到某个Customer的Orders,而不需要另建一张表。要想去掉这个自动生成的表,要这样写:
public class Order {
@id
private long id;
@ManyToOne
@JoinColumn(name="customer", nullable=false)
private Customer customer;
public void setId(long id) {
this.id = id;
}
public long getId() {
return this.id;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Customer getCustomer() {
return this.customer;
}
}
public class Customer{
@id
private long id;
@OneToMany(mappedBy="customer")
private Set<Order> orders;
public void setId(long id) {
this.id = id;
}
public long getId() {
return this.id;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
public Set<Order> getOrders() {
return this.orders;
}
}
多出一个mappedBy和JoinColumn, 就是告诉JPA实现者,orders是通过order里的customer映射来的,每次查找orders通过Order里的customer联接。这样就不会生成Customer_Order关联表了
public class Order {
@id
private long id;
@ManyToOne
private Customer customer;
public void setId(long id) {
this.id = id;
}
public long getId() {
return this.id;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Customer getCustomer() {
return this.customer;
}
}
public class Customer{
@id
private long id;
@OneToMany
private Set<Order> orders;
public void setId(long id) {
this.id = id;
}
public long getId() {
return this.id;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
public Set<Order> getOrders() {
return this.orders;
}
}
那么系统可能会自动生成一张Customer_Order关联表,可是事实上我们不需要这样的表,因为通过 Select order from Order order where order.customer.id=:id, 就可以找到某个Customer的Orders,而不需要另建一张表。要想去掉这个自动生成的表,要这样写:
public class Order {
@id
private long id;
@ManyToOne
@JoinColumn(name="customer", nullable=false)
private Customer customer;
public void setId(long id) {
this.id = id;
}
public long getId() {
return this.id;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Customer getCustomer() {
return this.customer;
}
}
public class Customer{
@id
private long id;
@OneToMany(mappedBy="customer")
private Set<Order> orders;
public void setId(long id) {
this.id = id;
}
public long getId() {
return this.id;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
public Set<Order> getOrders() {
return this.orders;
}
}
多出一个mappedBy和JoinColumn, 就是告诉JPA实现者,orders是通过order里的customer映射来的,每次查找orders通过Order里的customer联接。这样就不会生成Customer_Order关联表了
相关文章推荐
- Using JoinColumn, JPA using oneToMany mappedBy
- 用JoinColumn使用JPA的OneToMany不自动生成数据库表
- 码农小汪-Hibernate学习8-hibernate关联关系注解表示@OneToMany mappedBy @ManyToMany @JoinTable
- 码农小汪-Hibernate学习9-hibernate双向关联关系注解表示@OneToMany mappedBy @ManyToMany @JoinTable
- 异常:Caused by: java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z/Caused by: java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreign
- 3 OneToMany ManyToMany MappedBy Cascade
- hibernate基于注解的维护权反转:@OneToMany(mappedBy=)
- annotation @OneToMany(mappedBy="")
- In R, how to split/subset a data frame by factors in one column?
- How to join on two columns using hibernate JPA annotations
- JPA 注解 OneToMany和ManyToOne
- JPA,在@OneToMany里加入mappedBy属性
- JPA关系映射之one-to-one、one-to-many、many-to-one和many-to-many
- Specify the column on one-to-many
- 关于在JPA中使用@OneToOne @OneToMany @ManyToOne @ManyToMany
- JPA在@OneToMany里加入mappedBy属性避免生成中间表
- 基于JPA的CRUD(OneToMany)
- hibernate one-to-many/many-to-one 使用中间表/使用join 标签
- JPA关系映射之one-to-many和many-to-one
- JPA OneToMany双向关联