Hi,
I am working on a problem that has a one-to-many association, where the whole has a list of parts. If I create an instance of Whole and associate it with some instances of Part, and call EntityManager.persist(), Hibernate will replace the existing list with it's own list implementation, PersistentBag. The problem is that the new list assigned to the whole throws an UnsupportedException for the most common methods of the List interface.
Here are the entities used:
Code:
@Entity
public class Whole {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@OneToMany(cascade={CascadeType.ALL})
@JoinColumn(name="whole_id", nullable=false, updatable=false)
private List<Part> parts;
...
}
Code:
@Entity
public class Part {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
...
}
Here is the code I am using to recreate the problem:
Code:
@Test
public void testUpdateWhole() throws Exception {
Part part1 = new Part();
Part part2 = new Part();
Whole whole = new Whole();
whole.setParts(Arrays.asList(part1, part2));
em.persist(whole);
em.flush();
Part part3 = new Part();
whole.getParts().add(part3);
}
It throws an exception in the last line of the previous method:
Code:
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:131)
at java.util.AbstractList.add(AbstractList.java:91)
at org.hibernate.collection.PersistentBag.add(PersistentBag.java:298)
at example.test.HibernateTests.testUpdateWhole(HibernateTests.java:45)
The same applies if I try to remove an object from the list.
I am using the latest versions fo hibernate:
Hibernate Core 3.3.0 SP1
Hibernate Annotations 3.4.0 GA
Hibernate EntityManager 3.4.0 GA
Thanks in advance.