We're updating the issue view to help you get more done. 

ClassCastException when changing a Collection association to a Set if @PreUpdate listener exists

Description

If a @PreUpdate listener is present, a ClassCastException is raised during flush after an update of an association defined to use collection (bag) semantics when the assigned value is a set.

 

1 2 3 4 5 6 7 8 9 10 11 java.lang.ClassCastException: java.util.HashSet cannot be cast to org.hibernate.collection.spi.PersistentCollection at org.hibernate.event.internal.FlushVisitor.processCollection(FlushVisitor.java:43) at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:104) at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:65) at org.hibernate.event.internal.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:59) at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:166) at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:235) at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:94) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:38) at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1483) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1469)

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 // Assignment code (for an existing instance obtained via query) HashSet<Task> tasks = new HashSet<Task>(); tasks.add(new Task("Some work")); employee.setTasks(tasks); entityManager.merge(employee); entityManager.flush(); // Using the classes below public class EmployeeListener { @PreUpdate void onPreUpdate(Object o) { } } @Entity @EntityListeners( EmployeeListener.class ) public class Employee { @Id private String name; ... @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) protected Collection<Task> tasks; } @Entity public class Task { ... }

 

Environment

None

Status

Assignee

Gail Badner

Reporter

Stephen Fikes

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.4.3
5.3.10

Priority

Major