{code:java} package org.hibernate.jpa.test;
@Entity public class Product { @Id @GeneratedValue private Integer id; @OneToMany(fetch = LAZY, cascade = ALL , orphanRemoval = true ) private List<Widget> widgets;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public void setWidgets(List<Widget> widgets) { this.widgets = widgets; } }
{code} {code:java} package org.hibernate.jpa.test;
@Entity public class Widget { private String name; private int id;
public Widget() {
}
public String getName() { return name; }
public void setName(String name) { this.name = name; } @Id @GeneratedValue public int getId() { return id; }
public void setId(int id) { this.id = id; }
} {code} {code:java} //.... import com.google.common.collect.ImmutableList; //.... public class EntityManagerTest extends BaseEntityManagerFunctionalTestCase { //.... @Test public void testSavingTwiceEntityWithImmutableCollection() { EntityManager em = getOrCreateEntityManager(); em.getTransaction().begin();
Widget widget1 = new Widget(); widget1.setName("hammer");
Widget widget2 = new Widget(); widget2.setName("axel");
Product p = new Product(); p.setWidgets(ImmutableList.of(widget1, widget2)); em.persist(p);
p = em.getReference(Product.class,p.getId()); em.merge(p);
em.getTransaction().commit(); em.clear(); em.close(); } } {code} Fails with: {code:none} java.lang.UnsupportedOperationException at com.google.common.collect.ImmutableCollection.clear(ImmutableCollection.java:320) at org.hibernate.collection.internal.PersistentBag.clear(PersistentBag.java:411) at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:545) at org.hibernate.type.CollectionType.replace(CollectionType.java:721) at org.hibernate.type.TypeHelper.replace(TypeHelper.java:167) at org.hibernate.event.internal.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:374) at org.hibernate.event.internal.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:184) at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:157) at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:65) at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:905) at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:891) at org.hibernate.jpa.test.EntityManagerTest.testSavingTwiceEntityWithImmutableCollection(EntityManagerTest.java:400) {code}
Full test case available at \[1\].
Problem is visible also when using spring-data \[2\].
\[1\] https://github.com/hibernate/hibernate-orm/compare/master...bostko:test-merge-immutable \[2\] https://github.com/bostko/test-spring-data |
|