使用Hibernate插入嵌套实体。

问题描述 投票:0回答:1

我正试图弄清楚如何正确地将多个实体插入到数据库中,而这些实体又有另一个实体作为字段,似乎在hibernate中这是一个有点非同小可的任务。当我把所有的东西都排列整齐时,它工作得很好,但是当它们被交叉链接时,它就不能如愿了。我认为我缺少了一些所需的注释来使它工作,但经过几个小时的google,我无法找到任何合适的方法来及时解决它。

我目前的实体是这样设置的,我有一个学生和

@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 (?, ?)

它的错误是:

ERROR: insert or update on table "class_room" violates foreign key constraint "fk_student" Detail: 表 "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。感谢大家的帮助。希望会随着时间的推移解决,如果有,自己会发帖解答。

sql hibernate annotations entity
1个回答
1
投票

你对id的处理方法是 @GeneratedValue(strategy = GenerationType.IDENTITY)而你的学生列表和教室列表都已经提前填充了id。如果你想保持这些id在json中的存在,请去掉 @GeneratedValue(strategy = GenerationType.IDENTITY) 注释。

© www.soinside.com 2019 - 2024. All rights reserved.