-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: EntityManager.persist() creates an unimplemented bag
PostPosted: Mon Sep 01, 2008 10:30 am 
Beginner
Beginner

Joined: Mon Sep 01, 2008 9:35 am
Posts: 24
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.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 01, 2008 1:57 pm 
Beginner
Beginner

Joined: Mon Sep 01, 2008 9:35 am
Posts: 24
Nevermind.
The problem is that Arrays.asList() returns a list of fixed length, and thus cannot add or remove elements.
If I create a list using new ArrayList() and then add the parts to the list, the problem doesn't appear anymore.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.