gpt4 book ai didi

java - mappedBy 指的是类名还是表名?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:56 25 4
gpt4 key购买 nike

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

}

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

  • @OneToMany(mappedBy="customer_tab")
  • @OneToMany(mappedBy="客户") ?

谢谢!

最佳答案

两者都不正确。来自documentation :

mappedBy
public abstract java.lang.String mappedBy
The field that owns the relationship. Required unless the relationship is unidirectional.

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;
}

关于java - mappedBy 指的是类名还是表名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38448019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com