简体   繁体   English

Spring data JPA 使用 Beanutils.copyProperties 更新一对多关系

[英]Spring data JPA updates one/many-to-many relationship using Beanutils.copyProperties

I have two classes with one-to-many relationship.我有两个具有一对多关系的类。

class Hotel {
    private String name;
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Meeting> meetings = new ArrayList<>();
}
class Meeting {
    @Column(name = "meeting_time")
    @NotNull
    private LocalDateTime meetingTime;

    @Column(name = "number_people")
    @NotNull
    @Min(1)
    private int numberPeople;
}

class HotelDto{
    private long id;
    private List<MeetingDto> meetingDtos;
}
class MeetingDto {
    private long id;
    private LocalDateTime meetingTime;
    private int numberPeople;
}

To update the existing hotel in the database, do I have to retrive one by one the existing meeting from meeting database table and assign new time and number, then assign the meeting list to the hotel?要更新数据库中的现有酒店,我是否必须从会议数据库表中一一检索现有会议并分配新的时间和号码,然后将会议列表分配给酒店? If not, how do I resolve the reference exception after I copy a list through BeanUtils.copyProperties eg如果没有,我如何通过BeanUtils.copyProperties复制列表后解决引用异常,例如

Hotel newHotel = new Hotel();
Hotel hotel = hotelDao.findById(hotelDto.getId());
List<Meeting> newMeetingList = new ArrayList<>();
for (MeetingDto mDto : hotelDto.getMeetingDtos()) {
    Meeting meeting = meetingDao.findById(mDto.getId());
    meeting.setMeetingTime(mDto.getMeetingTime());
    meeting.setNumberPeople(mDto.getNumberPeople());
    // meetingDao.saveAndFlush(meeting);     should I add this?
    newMeetingList.add(meeting);
}
newHotel.setMeetings(newMeetingList);
BeanUtils.copyProperties(newHotel, hotel, "id");
hotelDao.saveAndFlush(hotel);

Is there a better way to update the object with the relationship?有没有更好的方法来更新具有关系的对象?

In order to update the hotel you should just load it from database, update what you want and save it 为了更新酒店,您应该只从数据库中加载它,更新您想要的内容并保存

Hotel hotel = hotelDao.findById(hotelDto.getId());
hotel.getMeetings().add(newMeeting); 
hotelDao.saveAndFlush(hotel);

BeanUtils is a good API to use, but it does kind of shallow object mapping. BeanUtils 是一个很好用的 API,但它做的是浅对象映射。 It doesn't do a good job when working with the objects that contain in other objects and figuring out their data types.Use ModelMapper instead of BeanUtils.Also ModelMapper uses TypeTokens to allow mapping of generic parameterized types.在处理包含在其他对象中的对象并弄清楚它们的数据类型时,它做得不好。使用 ModelMapper 而不是 BeanUtils。此外,ModelMapper 使用 TypeTokens 来允许映射泛型参数化类型。 if you wonder how it works you can read the documentation from this link http://modelmapper.org/如果您想知道它是如何工作的,您可以从此链接http://modelmapper.org/阅读文档

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

相关问题 使用BeanUtils.copyProperties实现参数类型不匹配 - argument type mismatch by Using BeanUtils.copyProperties 使用BeanUtils.copyProperties复制特定字段? - Copy specific fields by using BeanUtils.copyProperties? Spring JPA 多对多关系无法获取数据 - Spring JPA Many-to-Many relationship fail to fetch data Using BeanUtils.copyProperties from a Mybatis created object to a Spring Bean, the properties are null in the Spring bean. 为什么? - Using BeanUtils.copyProperties from a Mybatis created object to a Spring Bean, the properties are null in the Spring bean. Why? 如何在 Spring Mvc 中使用 BeanUtils.copyProperties 忽略对象中的某些字段 - How to ignore some fields in an object using BeanUtils.copyProperties in Spring Mvc 使用Spring提供的BeanUtils.copyProperties将属性复制到构建器 - Copy properties to a Builder with BeanUtils.copyProperties provided by Spring 如何使用BeanUtils.copyProperties? - How to use BeanUtils.copyProperties? BeanUtils.copyProperties和嵌套列表 - BeanUtils.copyProperties and nested List 使用BeanUtils.copyProperties时获取错误(dest,src) - Getting error when using BeanUtils.copyProperties(dest, src) 使用 Spring 数据 JPA 在多对一中的错误关系持久性 - Error relationship persistence using Spring Data JPA in a many to one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM