如何连接 JPA 实体 类? (one-to-many/many-to-one)
How to connect JPA entity classes? (one-to-many/many-to-one)
如果有这两个实体:
@Entity
@Table(name = "CUSTOMER")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "FIRST_NAME") //Attributes in the entitiy
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "ADDRESS")
private String address;
和
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "STREET_NAME") //Attributes in the entitiy
private String streetName;
@Column(name = "HOUSE_NUMBER")
private int houseNumber;
我现在如何连接这两个实体并定义 Customer
有很多 Addresses
?
你应该有一个 xml 文件来定义这个实体指的是哪个 table,然后你可以在那里添加它们的字段和关系。
示例:
<entity class="class" access="FIELD">
<table name="class_table" />
<attributes>
<id name="classId">
<column name="class_id" />
</id>
<basic name="className">
<column name="case_name" />
</basic>
<!-- Foreign key many-to-one -->
<many-to-one name="classType" fetch="LAZY">
<join-column name="class_type_id" referenced-column-name="class_type_id" />
</many-to-one>
<!-- Foreign key one-to-many -->
<one-to-many name="class_list" mapped-by="class" fetch="LAZY" />
</attributes>
</entity>
您可以在 Internet 上轻松找到大量此类示例。我强烈建议看看 ObjectDB website.
对于这个特定的场景,您可以如下定义它们:
@Entity
@Table(name = "CUSTOMER")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "FIRST_NAME") //Attributes in the entity
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "ADDRESS")
private String address;
@OneToMany(mappedBy = "customer")
private List<Address> addresses;
}
并在地址 class 中引用您的客户:
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "STREET_NAME") //Attributes in the entitiy
private String streetName;
@Column(name = "HOUSE_NUMBER")
private int houseNumber;
@ManyToOne(fetch = FetchType.LAZY)
private Customer customer;
}
编辑: 根据您对多对多关系示例的请求,您可以通过以下方式为您的客户和地址实体实现它:
首先,您应该有一个具有两个外键的中间 table,一个指向客户 table,另一个指向地址。假设你将这个中间体命名为 table customer_address
,那么它的数据将是这样的:
select * from customer_address;
customer_id | address_id
------------------------
12 | 15
12 | 14
12 | 13
2 | 15
2 | 13
19 | 11
现在您应该更新注释并告诉 JPA 使用此 table 来推断实体之间的多对多关系。
客户class:
@Entity
@Table(name = "CUSTOMER")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "FIRST_NAME") //Attributes in the entity
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "ADDRESS")
private String address;
@ManyToMany(mappedBy = "customer")
@JoinTable(name = "customer_address",
joinColumns = @JoinColumn(name = "customer_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "address_id", referencedColumnName = "id"))
private List<Address> addresses;
}
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "STREET_NAME") //Attributes in the entitiy
private String streetName;
@Column(name = "HOUSE_NUMBER")
private int houseNumber;
@ManyToMany(mappedBy = "customer")
@JoinTable(name = "customer_address",
joinColumns = @JoinColumn(name = "address_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "customer_id", referencedColumnName = "id"))
private List<Customer> customer;
}
在此代码中,注释用于向 JPA 提供以下信息:
@ManyToMany
:表示关系的类型
@JoinTable
:用于指定持有实际关系数据的table,具有以下属性:
joinColumns
:定义中间 table 中的列,该列引用包含 @ManyToMany
注释的实体 class。在 Customer
class 中,它定义了作为 customer
table 的外键的列,在 Address
class 中,它定义了作为外键的列是 address
table. 的外键
inverseJoinColumns
:与joinColumn
属性正好相反。定义中间 table 中的列,该列引用 @ManyToMany
注释另一侧的实体 class。在 Customer
class 中,它定义了作为 address
table 的外键的列,在 Address
class 中,它定义了作为外键的列是 customer
table. 的外键
我知道我的描述不尽如人意readable/understandable,但你知道@JoinTable
注解的描述并不容易。阅读示例代码本身并匹配 column/property 名称以找出每个名称的含义将是一种更简单的方法。您也可以在 ObjectWeb 网站上查看(也包括示例)。
如果有这两个实体:
@Entity
@Table(name = "CUSTOMER")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "FIRST_NAME") //Attributes in the entitiy
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "ADDRESS")
private String address;
和
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "STREET_NAME") //Attributes in the entitiy
private String streetName;
@Column(name = "HOUSE_NUMBER")
private int houseNumber;
我现在如何连接这两个实体并定义 Customer
有很多 Addresses
?
你应该有一个 xml 文件来定义这个实体指的是哪个 table,然后你可以在那里添加它们的字段和关系。
示例:
<entity class="class" access="FIELD">
<table name="class_table" />
<attributes>
<id name="classId">
<column name="class_id" />
</id>
<basic name="className">
<column name="case_name" />
</basic>
<!-- Foreign key many-to-one -->
<many-to-one name="classType" fetch="LAZY">
<join-column name="class_type_id" referenced-column-name="class_type_id" />
</many-to-one>
<!-- Foreign key one-to-many -->
<one-to-many name="class_list" mapped-by="class" fetch="LAZY" />
</attributes>
</entity>
您可以在 Internet 上轻松找到大量此类示例。我强烈建议看看 ObjectDB website.
对于这个特定的场景,您可以如下定义它们:
@Entity
@Table(name = "CUSTOMER")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "FIRST_NAME") //Attributes in the entity
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "ADDRESS")
private String address;
@OneToMany(mappedBy = "customer")
private List<Address> addresses;
}
并在地址 class 中引用您的客户:
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "STREET_NAME") //Attributes in the entitiy
private String streetName;
@Column(name = "HOUSE_NUMBER")
private int houseNumber;
@ManyToOne(fetch = FetchType.LAZY)
private Customer customer;
}
编辑: 根据您对多对多关系示例的请求,您可以通过以下方式为您的客户和地址实体实现它:
首先,您应该有一个具有两个外键的中间 table,一个指向客户 table,另一个指向地址。假设你将这个中间体命名为 table customer_address
,那么它的数据将是这样的:
select * from customer_address;
customer_id | address_id
------------------------
12 | 15
12 | 14
12 | 13
2 | 15
2 | 13
19 | 11
现在您应该更新注释并告诉 JPA 使用此 table 来推断实体之间的多对多关系。
客户class:
@Entity
@Table(name = "CUSTOMER")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "FIRST_NAME") //Attributes in the entity
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "ADDRESS")
private String address;
@ManyToMany(mappedBy = "customer")
@JoinTable(name = "customer_address",
joinColumns = @JoinColumn(name = "customer_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "address_id", referencedColumnName = "id"))
private List<Address> addresses;
}
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "STREET_NAME") //Attributes in the entitiy
private String streetName;
@Column(name = "HOUSE_NUMBER")
private int houseNumber;
@ManyToMany(mappedBy = "customer")
@JoinTable(name = "customer_address",
joinColumns = @JoinColumn(name = "address_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "customer_id", referencedColumnName = "id"))
private List<Customer> customer;
}
在此代码中,注释用于向 JPA 提供以下信息:
@ManyToMany
:表示关系的类型@JoinTable
:用于指定持有实际关系数据的table,具有以下属性:joinColumns
:定义中间 table 中的列,该列引用包含@ManyToMany
注释的实体 class。在Customer
class 中,它定义了作为customer
table 的外键的列,在Address
class 中,它定义了作为外键的列是address
table. 的外键
inverseJoinColumns
:与joinColumn
属性正好相反。定义中间 table 中的列,该列引用@ManyToMany
注释另一侧的实体 class。在Customer
class 中,它定义了作为address
table 的外键的列,在Address
class 中,它定义了作为外键的列是customer
table. 的外键
我知道我的描述不尽如人意readable/understandable,但你知道@JoinTable
注解的描述并不容易。阅读示例代码本身并匹配 column/property 名称以找出每个名称的含义将是一种更简单的方法。您也可以在 ObjectWeb 网站上查看(也包括示例)。