[ACCEPTED]-Hibernate: Removing item from a List does not persist-hibernate

Accepted answer
Score: 21

You have to explicitly specify cascade as 8 CascadeType.DELETE_ORPHAN.

Try to change 7 code to

@Cascade(cascade = {CascadeType.ALL, CascadeType.DELETE_ORPHAN}, mappedBy = "temporal")

Part from hibernate docs:

If the child 6 object's lifespan is bounded by the lifespan 5 of the parent object, make the parent 4 a full lifecycle object by specifying CascadeType.ALL 3 and org.hibernate.annotations.CascadeType.DELETE_ORPHAN (please 2 refer to the Hibernate reference guide 1 for the semantics of orphan delete)

Score: 2

This is the currently recommended way.

@OneToMany(mappedBy = "temporal", orphanRemoval = true, cascade = CascadeType.ALL)


Score: 1

Try removing the calls to Session.refresh(). From 14 the docs:

Re-read the state of the given instance 13 from the underlying database. It is inadvisable 12 to use this to implement long-running 11 sessions that span many business tasks. This 10 method is, however, useful in certain 9 special circumstances. For example

  • where a database trigger alters the object state upon insert or update
  • after executing direct SQL (eg. a mass update) in the same session
  • after inserting a Blob or Clob


If you 8 call flush() before refresh(), that might 7 fix the problem too, since flush() guarantees 6 that any pending SQL will be executed against 5 the DB. In practice I've almost never seen 4 anyone use refresh() and it doesn't look 3 like from your code that you need it.

This 2 chapter from the documentation is worth 1 a read:


Score: 0

You have marked the 'content' field as transient 5 in the super class. I would at least suspect 4 that this is causing problems. With the 3 mapping in the subclass, you basically have 2 now two contradicting mappings for the same 1 attribute.

More Related questions