如何在 spring 引导中映射与 JPA 的一对一关系?
How to map one-to-one relationship with JPA in spring boot?
我已经阅读了几个关于一对一关系映射的教程,例如:
https://docs.oracle.com/javaee/6/api/javax/persistence/OneToOne.html,
http://websystique.com/hibernate/hibernate-one-to-one-unidirectional-with-foreign-key-associations-annotation-example/
https://en.wikibooks.org/wiki/Java_Persistence/OneToOne
我相信我遵循了这些教程,但是我的关系映射仍然没有按预期工作。我有以下 类:
@Entity(name = "lesson")
public class Lesson {
@Id
@Type(type = "pg-uuid")
private UUID uid;
private String start_date_time;
private String end_date_time;
private String location;
@OneToOne
@JoinColumn(name="uid") //uid is the name of the Id i want to reference to in the subject class
private Subject subject_uid; // subject_uid is the name of the column in my subject table
public Lesson(UUID uid, String start_date_time, String end_date_time, String location, Subject subject_uid) {
this.uid = uid;
this.start_date_time = start_date_time;
this.end_date_time = end_date_time;
this.location = location;
this.subject_uid = subject_uid;
}
//getters setters
@Entity(name = "subject")
public class Subject {
@Id
@Type(type = "pg-uuid")
private UUID uid;
private String name;
private String start_date;
private String end_date;
private boolean is_lesson_created;
public Subject(UUID uid, String name, String start_date, String end_date, boolean is_lesson_created) {
this.uid = uid;
this.name = name;
this.start_date = start_date;
this.end_date = end_date;
this.is_lesson_created = is_lesson_created;
}
Spring Data Rest 在 /lessons 端点上创建的响应如下所示:
{
"_embedded" : {
"lessons" : [ {
"start_date_time" : "2017-01-08 08:30:00",
"end_date_time" : "2017-01-08 10:15:00",
"location" : "A101 ",
"_links" : {
"self" : {
"href" : "http://localhost:3400/lessons/78038aeb-cdc9-4673-990e-36b8c1105500"
},
"lesson" : {
"href" : "http://localhost:3400/lessons/78038aeb-cdc9-4673-990e-36b8c1105500"
},
"subject_uid" : {
"href" : "http://localhost:3400/lessons/78038aeb-cdc9-4673-990e-36b8c1105500/subject_uid"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:3400/lessons{?page,size,sort}",
"templated" : true
},
"profile" : {
"href" : "http://localhost:3400/profile/lessons"
}
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
当我想要访问 http://localhost:3400/lessons/78038aeb-cdc9-4673-990e-36b8c1105500/subject_uid
link 时,我得到了 404。
UUID 类型是否影响我的映射?我应该更改什么才能访问我的 student_uid?
终于我找到了问题所在,这是我在任何地方都没有读到的东西。当必须完成 one-to-one 连接时,JPA 提供连接的默认名称作为 table 名称下划线 id(subject_id)。就我而言,我在数据库中有一个名为 "subject" 的 table 名称,而 PK 简称为 "uid"。因此,您要做的是将 table 名称附加到要加入的属性名称:
@OneToOne
@JoinColumn(name="subject_uid")//the pattern is: "tablename_joined attribute"
private Subject subject_uid;
我已经阅读了几个关于一对一关系映射的教程,例如: https://docs.oracle.com/javaee/6/api/javax/persistence/OneToOne.html, http://websystique.com/hibernate/hibernate-one-to-one-unidirectional-with-foreign-key-associations-annotation-example/ https://en.wikibooks.org/wiki/Java_Persistence/OneToOne
我相信我遵循了这些教程,但是我的关系映射仍然没有按预期工作。我有以下 类:
@Entity(name = "lesson")
public class Lesson {
@Id
@Type(type = "pg-uuid")
private UUID uid;
private String start_date_time;
private String end_date_time;
private String location;
@OneToOne
@JoinColumn(name="uid") //uid is the name of the Id i want to reference to in the subject class
private Subject subject_uid; // subject_uid is the name of the column in my subject table
public Lesson(UUID uid, String start_date_time, String end_date_time, String location, Subject subject_uid) {
this.uid = uid;
this.start_date_time = start_date_time;
this.end_date_time = end_date_time;
this.location = location;
this.subject_uid = subject_uid;
}
//getters setters
@Entity(name = "subject")
public class Subject {
@Id
@Type(type = "pg-uuid")
private UUID uid;
private String name;
private String start_date;
private String end_date;
private boolean is_lesson_created;
public Subject(UUID uid, String name, String start_date, String end_date, boolean is_lesson_created) {
this.uid = uid;
this.name = name;
this.start_date = start_date;
this.end_date = end_date;
this.is_lesson_created = is_lesson_created;
}
Spring Data Rest 在 /lessons 端点上创建的响应如下所示:
{
"_embedded" : {
"lessons" : [ {
"start_date_time" : "2017-01-08 08:30:00",
"end_date_time" : "2017-01-08 10:15:00",
"location" : "A101 ",
"_links" : {
"self" : {
"href" : "http://localhost:3400/lessons/78038aeb-cdc9-4673-990e-36b8c1105500"
},
"lesson" : {
"href" : "http://localhost:3400/lessons/78038aeb-cdc9-4673-990e-36b8c1105500"
},
"subject_uid" : {
"href" : "http://localhost:3400/lessons/78038aeb-cdc9-4673-990e-36b8c1105500/subject_uid"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:3400/lessons{?page,size,sort}",
"templated" : true
},
"profile" : {
"href" : "http://localhost:3400/profile/lessons"
}
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
当我想要访问 http://localhost:3400/lessons/78038aeb-cdc9-4673-990e-36b8c1105500/subject_uid
link 时,我得到了 404。
UUID 类型是否影响我的映射?我应该更改什么才能访问我的 student_uid?
终于我找到了问题所在,这是我在任何地方都没有读到的东西。当必须完成 one-to-one 连接时,JPA 提供连接的默认名称作为 table 名称下划线 id(subject_id)。就我而言,我在数据库中有一个名为 "subject" 的 table 名称,而 PK 简称为 "uid"。因此,您要做的是将 table 名称附加到要加入的属性名称:
@OneToOne
@JoinColumn(name="subject_uid")//the pattern is: "tablename_joined attribute"
private Subject subject_uid;