sql - 使用 Hibernate 插入嵌套实体
问题描述
我试图弄清楚如何正确地将几个实体插入到数据库中,这些实体具有另一个实体作为字段,并且似乎在休眠中它是一个不简单的任务。当我把所有东西都整理整齐时它工作得很好,但是一旦它们交叉链接,它就不能按预期工作。我假设我缺少一些必需的注释以使其正常工作,但是经过几个小时的谷歌搜索,我找不到任何合适的方法来迅速解决它。
我当前的实体以这种方式设置我有一个学生和
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
}
我有一个教室,里面有一个学生实体,通过它的 id 链接
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ClassRoom {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id", foreignKey = @ForeignKey(name = "fk_student"))
private Student student;
private String dateCreated;
}
当我尝试将它存储到数据库中时,我有一个完美的 json:
{
"dateCreated":"17",
"body":[
{
"id":1,
"student":{
"id":1,
"name":"petya",
"hibernateLazyInitializer":{
}
},
"dateCreated":"17"
},
{
"id":2,
"student":{
"id":2,
"name":"petrya",
"hibernateLazyInitializer":{
}
},
"dateCreated":"17"
},
{
"id":3,
"student":{
"id":3,
"name":"slon",
"hibernateLazyInitializer":{
}
},
"dateCreated":"17"
}
]
}
Hibernate: select student0_.id as id1_1_0_, student0_.name as name2_1_0_ from json_schema.student student0_ where student0_.id=?
Hibernate: select student0_.id as id1_1_0_, student0_.name as name2_1_0_ from json_schema.student student0_ where student0_.id=?
Hibernate: select student0_.id as id1_1_0_, student0_.name as name2_1_0_ from json_schema.student student0_ where student0_.id=?
Hibernate: select classroom0_.id as id1_0_0_, classroom0_.date_created as date_cre2_0_0_, classroom0_.student_id as student_3_0_0_ from json_schema.class_room classroom0_ where classroom0_.id=?
Hibernate: select classroom0_.id as id1_0_0_, classroom0_.date_created as date_cre2_0_0_, classroom0_.student_id as student_3_0_0_ from json_schema.class_room classroom0_ where classroom0_.id=?
Hibernate: select classroom0_.id as id1_0_0_, classroom0_.date_created as date_cre2_0_0_, classroom0_.student_id as student_3_0_0_ from json_schema.class_room classroom0_ where classroom0_.id=?
Hibernate: update json_schema.class_room set date_created=?, student_id=? where id=?
Hibernate: update json_schema.class_room set date_created=?, student_id=? where id=?
Hibernate: update json_schema.class_room set date_created=?, student_id=? where id=?
但是一旦我尝试保存链接到第三个学生的第二个教室条目,例如这个:
{
"dateCreated":"10",
"body":[
{
"id":1,
"student":{
"id":1,
"name":"petya",
"hibernateLazyInitializer":{
}
},
"dateCreated":"10"
},
{
"id":2,
"student":{
"id":3,
"name":"slon",
"hibernateLazyInitializer":{
}
},
"dateCreated":"10"
},
{
"id":3,
"student":{
"id":2,
"name":"petrya",
"hibernateLazyInitializer":{
}
},
"dateCreated":"10"
}
]
}
Hibernate: select student0_.id as id1_1_0_, student0_.name as name2_1_0_ from json_schema.student student0_ where student0_.id=?
Hibernate: insert into json_schema.student (name) values (?)
Hibernate: select student0_.id as id1_1_0_, student0_.name as name2_1_0_ from json_schema.student student0_ where student0_.id=?
Hibernate: insert into json_schema.student (name) values (?)
Hibernate: update json_schema.student set name=? where id=?
Hibernate: select classroom0_.id as id1_0_0_, classroom0_.date_created as date_cre2_0_0_, classroom0_.student_id as student_3_0_0_ from json_schema.class_room classroom0_ where classroom0_.id=?
Hibernate: insert into json_schema.class_room (date_created, student_id) values (?, ?)
Hibernate: select classroom0_.id as id1_0_0_, classroom0_.date_created as date_cre2_0_0_, classroom0_.student_id as student_3_0_0_ from json_schema.class_room classroom0_ where classroom0_.id=?
Hibernate: insert into json_schema.class_room (date_created, student_id) values (?, ?)
它打破了:
错误:在表“class_room”上插入或更新违反了外键约束“fk_student”详细信息:表“student”中不存在键 (student_id)=(3)。
这可能是我正在实现的逻辑,我试图以这种方式存储它们:
final ClassRoom[] classRooms = objectMapper.readValue(parser, ClassRoom[].class);
final List<ClassRoom> classRoomList = Arrays.asList(classRooms);
final List<Student> studentsList = new ArrayList<>();
for (final ClassRoom classRoom : classRoomList) {
studentsList.add(classRoom.getStudent());
}
studentRepo.saveAll(studentsList);
classRoomRepo.saveAll(classRoomList);
但我不知道如何首先单独存储学生,只有在存储classRooms之后。感谢任何帮助。希望会随着时间的推移解决它,如果是这样,我会自己发布一个答案。
解决方案
您对 id 的访问是@GeneratedValue(strategy = GenerationType.IDENTITY)
,而您的学生列表和教室都预先填充了 id。如果您想让这些 id 以 json 形式出现,请删除 @GeneratedValue(strategy = GenerationType.IDENTITY)
注释。
推荐阅读
- sql-server - 使用 JDBCConnector 和 StoredProcedures 的 T-SQL SQL 注入
- java - 无法弄清楚为什么它会抛出 ConcurrentModificationException
- asp.net-mvc - 是否可以在 EvoPDF / .Net 中使用 Font Awesome 图标?
- c# - 打开 chrome 并自动填充表单域窗体 C#
- operators - 为什么不同语言的运算符不同?
- html - chrome 中的 Macos 特定错误。在 windows chrome 中运行良好
- extjs - Grid 当列数增加时,列和行线不对齐
- openmdao - 使用 ExternalCodeComp 作为单个 comp 和 OpenMDAO 概念
- vba - Excel VBA:引用另一个工作表中单元格的公式(变量)
- java - SpEL 可以直接传递环境变量还是总是需要某种形式的属性文件?