@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private Book book;
this is in shop class.
In book object, there is version, id, name
I am sending shop object in create request.
If i send
"book": {
"description": "string",
"documentId": 0,
"filterName": "string",
"id": 0,
"isBook": true,
"version": 0
},
this it gives error:
caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: rally.services.center.domain.book.book
if i remove version and send this
"book": {
"description": "string",
"documentId": 0,
"filterName": "string",
"id": 0,
"isBook": true
there is no error.
Why can it be? },
When shop is deleted, book should be deleted and same for update so it is CascadeType.ALL
and orphanRemoval = true
this is where it is saving
Book book = new Book(request.getBook());
Book savedBook = bookRepository.save(book);
it gives error in the last line. i did not override save method.
After i send that request body, it is dto. So to convert it to entity, i was using this, fromDTO:
setDocumentId(bookDTO.getDocumentId());
setDescription(bookDTO.getDescription());
setId(bookDTO.getId());
setVersion(bookDTO.getVersion());
so i deleted the id line and now it works:
setDocumentId(bookDTO.getDocumentId());
setDescription(bookDTO.getDescription());
setVersion(bookDTO.getVersion());
i dont know why but now it works.
it uses fromdto for update and create so for create, no need to set id for entity, because it will create new. For modify, maybe i can need in future, but for now, i dont need id. Because i give shop's id so it updates book in it, without needing bookid.