在哪种情况下,您使用JPA @JoinTable批注? [英] In which case do you use the JPA @JoinTable annotation?
问题描述
在哪种情况下,您使用JPA @JoinTable
批注?
In which case do you use the JPA @JoinTable
annotation?
推荐答案
编辑2017-04-29 :正如一些评论者所指出的,JoinTable
示例不需要mappedBy
注释属性.实际上,最新版本的Hibernate拒绝通过打印以下错误来启动:
EDIT 2017-04-29: As pointed to by some of the commenters, the JoinTable
example does not need the mappedBy
annotation attribute. In fact, recent versions of Hibernate refuse to start up by printing the following error:
org.hibernate.AnnotationException:
Associations marked as mappedBy must not define database mappings
like @JoinTable or @JoinColumn
假设您有一个名为Project
的实体和另一个名为Task
的实体,并且每个项目可以有许多任务.
Let's pretend that you have an entity named Project
and another entity named Task
and each project can have many tasks.
您可以通过两种方式为此方案设计数据库架构.
You can design the database schema for this scenario in two ways.
第一个解决方案是创建一个名为Project
的表和另一个名为Task
的表,并将外键列添加到名为project_id
的任务表中:
The first solution is to create a table named Project
and another table named Task
and add a foreign key column to the task table named project_id
:
Project Task
------- ----
id id
name name
project_id
这样,就可以为任务表中的每一行确定项目.如果使用这种方法,则在您的实体类中不需要联接表:
This way, it will be possible to determine the project for each row in the task table. If you use this approach, in your entity classes you won't need a join table:
@Entity
public class Project {
@OneToMany(mappedBy = "project")
private Collection<Task> tasks;
}
@Entity
public class Task {
@ManyToOne
private Project project;
}
另一种解决方案是使用第三个表,例如Project_Tasks
,并将项目和任务之间的关系存储在该表中:
The other solution is to use a third table, e.g. Project_Tasks
, and store the relationship between projects and tasks in that table:
Project Task Project_Tasks
------- ---- -------------
id id project_id
name name task_id
Project_Tasks
表称为联接表".要在JPA中实现第二种解决方案,您需要使用@JoinTable
批注.例如,为了实现单向一对多关联,我们可以这样定义实体:
The Project_Tasks
table is called a "Join Table". To implement this second solution in JPA you need to use the @JoinTable
annotation. For example, in order to implement a uni-directional one-to-many association, we can define our entities as such:
Project
实体:
Project
entity:
@Entity
public class Project {
@Id
@GeneratedValue
private Long pid;
private String name;
@JoinTable
@OneToMany
private List<Task> tasks;
public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Task> getTasks() {
return tasks;
}
public void setTasks(List<Task> tasks) {
this.tasks = tasks;
}
}
Task
实体:
Task
entity:
@Entity
public class Task {
@Id
@GeneratedValue
private Long tid;
private String name;
public Long getTid() {
return tid;
}
public void setTid(Long tid) {
this.tid = tid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
这将创建以下数据库结构:
This will create the following database structure:
@JoinTable
注释还允许您自定义联接表的各个方面.例如,是否我们对tasks
属性进行了如下注释:
The @JoinTable
annotation also lets you customize various aspects of the join table. For example, had we annotated the tasks
property like this:
@JoinTable(
name = "MY_JT",
joinColumns = @JoinColumn(
name = "PROJ_ID",
referencedColumnName = "PID"
),
inverseJoinColumns = @JoinColumn(
name = "TASK_ID",
referencedColumnName = "TID"
)
)
@OneToMany
private List<Task> tasks;
结果数据库将变为:
最后,如果要为多对多关联创建架构,则使用联接表是唯一可用的解决方案.
Finally, if you want to create a schema for a many-to-many association, using a join table is the only available solution.
这篇关于在哪种情况下,您使用JPA @JoinTable批注?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!