Reputation: 2055
I have a List of Book objects that contain bookId
.
I have to pass that List of Book objects and save each object in its row in database.
List looks like this:
[
{
"bookId": 2
},
{
"bookId": 3
},
{
"bookId": 2
},
{
"bookId": 3
}
]
Could someone guide me how the code should look like?
I started something like this:
public void addMultipleRents(List<RentDto> rentDtoList, long userId){
List<Rent> rentList = Arrays.asList(modelMapper.map(rentDtoList, Rent.class));
RentDto rentDto = new RentDto();
User user = userRepository.findById(userId)
.orElseThrow(()-> new UserNotFoundException("User with id: " +
rentDto.getUserId() + " is not found"));
List<Rent> rent = new ArrayList<>();
for(Rent r : rentList){
r.setRentStart(LocalDateTime.now());
r.setUser(user);
r.setBook(book);
rent.add(r);
}
rentRepository.saveAll(rentList);
}
Rent.java
public class Rent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private LocalDateTime rentStart;
private LocalDateTime rentEnd;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "book_id", referencedColumnName = "id")
private Book book;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", referencedColumnName = "id")
private User user;
}
RentDto.java
public class RentDto {
private Long rentId;
private Long userId;
private Long bookId;
private String userFirstName;
private String userLastName;
private String bookTitle;
private LocalDateTime rentStart;
private LocalDateTime RentEnd;
}
But I am getting error for r.setBook(book);
:
The given id must not be null!
Upvotes: 0
Views: 1768
Reputation: 88707
To summarize the trail of comments:
Instead of relying on modelMapper
(whatever the class is) you could iterate over the list of DTOs yourself and create a Rent
entity for each of those. You then put them into a list and save that list as you're doing already.
The pseudo code for this could be something like this (simplified):
User user = userRepo.getById(userId); //get the user for all rentals
List<Rent> rentList = new ArrayList<>(rentDtos.size()); //you already know the list size you're expecting
for( RentDTO dto : rentDtos) {
Book book = bookRepo.getById( dto.getBookId() ); //the book for the one rental
Rent rent = new Rent(); //create a new rent
... //set user, book and rent time here
rentList.add(rent);
}
rentRepository.saveAll(rentList);
Note that this (pseudo) code isn't meant to be compilable, fast or to handle all possible errors but to get you started.
One obvious improvement would be to first collect a set of book ids, load all books for those ids (e.g. as a Map<Long, Book>
) and then get the books to assignto the Rent
entities from that map. I'll leave that as an exercise for you though.
One final advice: since you're a beginner you should first get a good grasp of the basics before getting too deep into complex frameworks such as Spring etc. Those frameworks make use of concepts like dependency injection, object-relational mapping etc. which would be too hard to understand correctly if you're still missing the basics.
Upvotes: 2