在哪种情况下您使用 JPA @JoinTable 注释? [英] In which case do you use the JPA @JoinTable annotation?
问题描述
在哪种情况下您使用 JPA @JoinTable
注释?
In which case do you use the JPA @JoinTable
annotation?
推荐答案
EDIT 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屋!