0

I already discussed the situation with a coworker, but we couldn't explain why this works.

I'm getting a list of Entitys from the DB without detaching them.

List<Announcement> announcements = new JPAQuery(em).from(QANNOUNCEMENT).where(QANNOUNCEMENT.customer.eq(p.getCustomer()).and((QANNOUNCEMENT.person.eq(p)).or(QANNOUNCEMENT.person.isNull()))).list(QANNOUNCEMENT);

After modifying one value I get a Transaction in which I execute the following

em.merge(announcement);
em.flush();

The result is, that the Entity is updated in the Database without any issues.

How can a merge work a non detached Entity?

  • Possible duplicate of [What is the difference between persist() and merge() in Hibernate?](https://stackoverflow.com/questions/4509086/what-is-the-difference-between-persist-and-merge-in-hibernate) – K.Nicholas Dec 03 '18 at 17:41
  • I don't think that it is a duplicate. The case of running a merge() on a not detached entity isn't discussed. The "dirty checking" method mentioned in the last answer seems also not to fit. I'm using eclipselink and when i replace my merge+flush with only a flush, there isnt any update happening. – Christian Schmidt Dec 04 '18 at 10:19
  • You might do better if you post a question about the actual problem you are having. – K.Nicholas Dec 04 '18 at 14:46
  • As per the docs, Merge just takes your instance and merges it into the managed instance, and cascades the merge over required relationships. Since your instance and the managed copy are the same, this is almost a no-op (depending on the changes you've made). So.. what are the changes you've made, and how are you sure that you are working on a managed instance? – Chris Dec 05 '18 at 14:50

0 Answers0