使用 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,当我尝试将它存储到 db 时,它运行良好:

{
   "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)注释。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法使用Hibernate JPA更新嵌套实体

使用SQL Server以自动增量插入嵌套实体

使用 Entity Framework Core 将一对多的嵌套实体插入数据库

如何为嵌套实体构建 Hibernate 标准

如何从Spring / JPA / Hibernate中的嵌套实体获取对象?

Spring Boot 使用 JSON RequestBody 保存嵌套实体

在SOLR中使用DIH配置嵌套实体

如何使用AutoMapper使用嵌套实体更新实体,并使用Entity Framework保存更新的实体?

使用实体框架检索许多嵌套实体的性能降低

如何使用具有特定条件的嵌套实体返回nHibernate实体

更新嵌套实体 (ManyToOne),当使用 JPA CascadeType 更新根实体时

使用现有的嵌套实体保存新的断开连接的实体,而无需复制现有的实体

在主键上的JPA @AutoGenerated对嵌套实体使用父序列/自动缩进

如何使用spring-data-rest发布新的嵌套实体

如何使用Spring Boot为嵌套实体配置Jackson解串器

使用Spark结构化流处理包含嵌套实体的JSON

序列化-在嵌套实体上使用条件搜索查询

如何在C#中使用JavascriptSerializer序列化嵌套实体(模型)?

如何使用生成的C#OData客户端代码请求具有1个以上嵌套实体的实体?

嵌套实体集合

Spring Data JPA:批量插入嵌套实体

在Hibernate实体中使用AspectJ

使用Javassist创建Hibernate实体

批量插入或使用Hibernate更新?

嵌套实体的设计注册

是否可以使用EF通过单个查询查询我的案件?(其中实体的条件和嵌套实体的另一个条件)

在Hibernate中使用外键插入实体的正确格式是什么?

使用Hibernate和Oracle进行批量插入不会记录哪个实体导致ConstraintViolationException

使用临时嵌套子项更新现有实体时,Spring数据JPA-Hibernate-TransientObjectException