JPA“@ JoinTable”注解 [英] JPA "@JoinTable" annotation
问题描述
在这种情况下,您是否使用JPA @JoinTable
注释?
编辑2017-04-29 :正如一些评论者所指出的, JoinTable
示例不需要 mappedBy
注释属性。事实上,最近的Hibernate版本通过输出以下错误拒绝启动:
$ b
org。 hibernate.AnnotationException:
标记为mappedBy的关联不得定义数据库映射
,如@JoinTable或@JoinColumn
$假设你有一个名为项目
的实体和另一个名为任务的实体
并且每个项目都可以有许多任务。
您可以通过两种方式设计此场景的数据库模式。 b
第一个解决方案是创建一个名为 Project
的表和另一个名为 Task
的表和将外键列添加到名为 project_id
的任务表中:
$ b
项目任务
------- ----
id id
名称名称
project_id
通过这种方式,可以确定任务表中每行的项目。如果你使用这种方法,在你的实体类中你将不需要一个连接表:
@Entity
public class Project {
@OneToMany(mappedBy =project)
private Collection< Task>任务;
}
@实体
公共类任务{
@ManyToOne
私人项目项目;
}
另一种解决方法是使用第三个表格,例如 Project_Tasks
,并将项目和任务之间的关系存储在该表中:
Project Task Project_Tasks
------- ---- -------------
id id project_id
name name task_id
调用 Project_Tasks
表一个加入表。要在JPA中实现第二个解决方案,您需要使用 @JoinTable
注释。例如,为了实现单向一对多关联,我们可以像这样定义我们的实体:
Project
entity:
@Entity
public class Project {
@Id
@GeneratedValue
private Long pid;
私人字符串名称;
@JoinTable
@OneToMany
private List< Task>任务;
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;
}
公共列表< Task> getTasks(){
返回任务;
}
public void setTasks(List< Task> tasks){
this.tasks = tasks;
$ b 任务 $> / code>实体:
@Entity
公共类任务{
@Id
@GeneratedValue
private Long tid;
私人字符串名称;
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;
}
}
这将创建以下数据库结构:
@JoinTable
注释还允许您自定义连接表的各个方面。例如,如果我们注释了这样的任务
属性: @ JoinTable(
name =MY_JT,
joinColumns = @JoinColumn(
name =PROJ_ID,
referencedColumnName =PID
),
inverseJoinColumns = @JoinColumn(
name =TASK_ID,
referencedColumnName =TID
)
)
@OneToMany
private List< Task>任务;
结果数据库会变成:
最后,如果要为多对多关联创建模式,则使用连接表是只有可用的解决方案。
In which case do you use JPA @JoinTable
annotation?
解决方案 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
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.
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;
}
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
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
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
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:
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;
The resulting database would have become:
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屋!