0
@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.

vegan
  • 117
  • 1
  • 11
  • I'd suggest not using `CascadeType.ALL`. This not only includes `CascadeType.REMOVE` and `CascadeType.PERSIST` but also other cascade types. Unless you really want every cascade type always define your cascade types specific. – Casper Sep 27 '18 at 11:36
  • @dirbacke that's not true! https://www.drupal.org/docs/develop/coding-standards/list-of-sql-reserved-words – Simon Martinelli Sep 27 '18 at 11:39
  • can you show the code where you loading and saving the entity? – Simon Martinelli Sep 27 '18 at 11:40
  • @dirbacke As far as I know version is no reserved word in java or SQL. And with hibernate all column names are quoted so that shouldn't be a problem if I'm correct. Or am I missing/mistaking something? – Casper Sep 27 '18 at 11:40
  • @Casper you are right! (see my comment) – Simon Martinelli Sep 27 '18 at 11:41
  • A detached object is a object that is fetched with a hibernate session which is closed. The way to fix this would be opening a new session when you want to persist your object. This is the case if nothing else happens that is weird in your code. Would you mind sharing the code that persists your entity? – Casper Sep 27 '18 at 11:48
  • Please provide more code. I can only help if I have the full DAO and the full service objects :) – Casper Sep 27 '18 at 12:47

1 Answers1

0

Use CascadeType.MERGE

Or Can you try to use saveOrUpdate() or merge() method of hibernate.

Or checkout below link (already asked)

JPA/Hibernate: detached entity passed to persist

Dipak
  • 39
  • 4