mappingBy 是指类名还是表名?

tab*_*bim 6 java mapping hibernate jpa one-to-many

例如,当我们在@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; }

}
Run Code Online (Sandbox Code Playgroud)

那么这两个哪个是正确的呢?:

  • @OneToMany(mappedBy="customer_tab")
  • @OneToMany(mappedBy="Customer") ?

谢谢!

Tim*_*sen 7

两者都不正确。从文档

mappingBy
public abstract java.lang.String mappingBy
拥有关系的字段。除非关系是单向的,否则是必需的。

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;
}
Run Code Online (Sandbox Code Playgroud)