简体   繁体   English

如何在 spring 引导中将实体保存在与数据库的关系中

[英]how to save entities in a relation to database in spring boot

I have a spring boot application with two entities in a relationship.我有一个 spring 启动应用程序,其中有两个实体。 MeetingSetting and MeetingTime meetingSetting can have unlimited meetingTimes. MeetingSetting 和 MeetingTime meetingSetting 可以有无限的会议时间。 So far the databases are generating without problem, but When I try to save my Entity they are saved but different from each other, they are saved independently.到目前为止,数据库生成没有问题,但是当我尝试保存我的实体时,它们被保存但彼此不同,它们是独立保存的。 Meaning MeetingName which is a foreign key inside MeetingTime is not saved but seen as null (I debugged and tried finding out why but could not find anything) THe other values are saved-含义 MeetingName 是 MeetingTime 中的外键,它没有被保存,但被视为 null (我调试并尝试找出原因但找不到任何东西)其他值被保存 -

could someone point me out what my error is?有人能指出我的错误是什么吗?

this is the json I am sending:这是我要发送的 json:

{
   "meetingName":"TEst",
   "meetingPw":"",
   "meetingTime":[
      {
         "date":"2021-05-31",
         "startTime":"15:30",
         "endTime":"16:30"
      },
      {
         "date":"2021-06-21",
         "startTime":"15:30",
         "endTime":"17:30"
      },
      {
         "date":"2021-06-21",
         "startTime":"11:01",
         "endTime":"11:01"
      }
   ]
}

MeetingSettings:会议设置:

@Entity
@Table(name = "meeting_settings")
@Data

public class MeetingsSetting {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "meeting_name", unique = true)
    private String meetingName;

    @Column(name = "meeting_url")
    private String meetingUrl;

    @Column(name = "meeting_pw")
    private String meetingPw;

    @OneToMany(mappedBy = "meeting_Name", cascade = CascadeType.ALL)
    private Set<MeetingTime> meetingTime = new HashSet<>();
}

MeetingTime:会议时间:

@Entity
@Table(name = "meeting_times")
@Data
public class MeetingTime {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "meeting_date")
    private String date;

    @Column(name = "start_time")
    private String startTime;

    @Column(name = "end_time")
    private String endTime;

    @ManyToOne
    @JoinColumn(name = "meeting_name" ,insertable = false, updatable = false , referencedColumnName = "meeting_name")
    private MeetingsSetting meeting_Name;
}

this is how I try to save the entity:这就是我尝试保存实体的方式:

@RestController
@RequestMapping("/api/meetingSetting")
public class MeetingSettingController {

    @Autowired
    MeetingSettingService meetingSettingService;

    @PostMapping("/")
    public void saveMeeting(@RequestBody MeetingsSetting meetingsSetting){
        meetingSettingService.saveMeeting(meetingsSetting);

    }


}

My service calls the save method of an jpaRepository.我的服务调用 jpaRepository 的保存方法。

In a bi-directional One to Many, you have to synchronize both sides of the association.在双向一对多中,您必须同步关联的双方。

You can simply iterate over all MeetingTime objects and set the corresponding MeetingSetting to it.您可以简单地遍历所有MeetingTime对象并将相应的MeetingSetting设置为它。

Your MeetingSettingService 's saveMeeting method could do this:您的MeetingSettingServicesaveMeeting方法可以这样做:

public void saveMeeting(MeetingsSetting meetingsSetting) {
    // ... 
    
    // here you're synchronizing both sides of the association
    meetingsSetting.getMeetingTime()
            .forEach(mt -> mt.setMeetingSetting(meetingSetting));
    // ...
    repository.save(meetingSetting);
}

Solution to my question, I am not sure if this is a good or correct way of solving this maybe someone can advice me a better solution:解决我的问题,我不确定这是否是解决此问题的好方法或正确方法,也许有人可以建议我更好的解决方案:

 @Entity
    @Table(name = "meeting_times")
    @Data
    public class MeetingTime implements Serializable {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        @Column(name = "meeting_date")
        private String date;
    
        @Column(name = "start_time")
        private String startTime;
    
        @Column(name = "meeting_name")
        private String meeting_name;
        THIS IS THE PART WHICH IS CALLED FROM THE METHOD INSIDE MEETINGSCONTROLLER
        @Column(name = "end_time")
        private String endTime;
    
        @ManyToOne
        @JoinColumn(name = "meeting_name" ,insertable = false, updatable = false, referencedColumnName = "meeting_name")
        private MeetingsSetting meetingName;
    }

MeetingsTime Entity:会议时间实体:

@RestController
        @RequestMapping("/api/meetingSetting")
        public class MeetingSettingController {
        
            @Autowired
            MeetingSettingService meetingSettingService;
        
            @PostMapping("/")
            public void saveMeeting(@RequestBody MeetingsSetting meetingsSetting){
                meetingsSetting.getMeetingTime()
                        .forEach(mt -> mt.setMeeting_name(meetingsSetting.getMeetingName()));
                // ...
                meetingSettingService.saveMeeting(meetingsSetting);
        
            }
        
        
        }

I suspect that if you leave it as you originally had it but remove the insertable = false and updatable = false from here it will work我怀疑如果你保持原来的样子,但从这里删除insertable = false and updatable = false它将起作用

    @ManyToOne
    @JoinColumn(name = "meeting_name" , referencedColumnName = "meeting_name")
    private MeetingsSetting meeting_Name;

Give it a try and see.试试看。 Hibernate must first save the nested element (MeetingTime) in DB to retrieve an Id then link that with the MeetingsSetting via a update and then save the MeetingsSetting. Hibernate 必须首先将嵌套元素 (MeetingTime) 保存在 DB 中以检索 Id,然后通过更新将其与 MeetingsSetting 链接,然后保存 MeetingsSetting。

I can't predict exactly the workflow for hibernate but I am pretty confident that if you remove those it will be able to link both of them during save.我无法准确预测 hibernate 的工作流程,但我非常有信心,如果您删除它们,它将能够在保存期间链接它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM