*
The moose likes Object Relational Mapping and the fly likes JPA returning collection of type PersistentBag instead of ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA returning collection of type PersistentBag instead of ArrayList" Watch "JPA returning collection of type PersistentBag instead of ArrayList" New topic
Author

JPA returning collection of type PersistentBag instead of ArrayList

Nilesh Soni
Greenhorn

Joined: May 14, 2001
Posts: 21
Hi All,
I have 2 entities (Category and Products) having one to many relationship between them. My architecture is having Spring (in business Layer) and am using Hibernate under the hood of JPA. I am using annotations for all my configurations. Please see the attached code.

Inside the finder method of a DAO, I am finding (EntityManager.find()) the parent entity. The problem is that the child property of this parent (which is a collection of type ArrayList) is actually returned as PersistentBag.

What should I do so that the getter method of my parent will return me collection of type ArrayList?



Do anyone has any idea?

Regards.
Benjamin Winterberg
Ranch Hand

Joined: Sep 19, 2007
Posts: 36
Hibernate uses own List implementations dependening on your mapping. While mapping java.util.List without specifying @IndexColumn, hibernate uses bag semantic for this relation. This results in PersistentBags. You should define the index column to get list semantic (PersistentList).

Check out the reference documentation for further details.


Hibernate DAOs with Spring | Separation of Concerns | Eclipse Code Templates | more...
Angel Taveras
Ranch Hand

Joined: Nov 13, 2008
Posts: 84

Hello, basically the hibernate need to know what objects are loaded, if they are lazy loaded, the connection to the database and the session associated with the loaded objects among a lot of other stuff that the ArrayList can't hold and don't need to have. That why it uses a proprietary implementation of java.util.List.

Regards,
Nilesh Soni
Greenhorn

Joined: May 14, 2001
Posts: 21
Ben and Angel,
First of all thanks for quick reply.

After reading post from both of you, I am having some more questions for which I am not able to google proper answers.

1) For getting PersistentList instead of PersistenBag, we have to implement IndexColumn annotation and hence we have to create an extra column into the table. Why on earth, such kind of weird requirement? What if I don't want to have a dedicated column in DB for such solution?

2) Is it that hibernate will always going to return collection of PersistentXXX type only whenever we will find an Entity (having a member variable as a Collection) using EntityManager.find() method?
If I will implement the solution as specified by Ben, still I will get PersistentList, while my requirement is an ArrayList. So onus is on me to do the conversion as follows:



I hope I am not asking for too much.

Regards.
Benjamin Winterberg
Ranch Hand

Joined: Sep 19, 2007
Posts: 36
Nilesh Soni wrote:1) For getting PersistentList instead of PersistenBag, we have to implement IndexColumn annotation and hence we have to create an extra column into the table. Why on earth, such kind of weird requirement? What if I don't want to have a dedicated column in DB for such solution?

List semantic means that hibernate persists the lists index of each element. Because of that you have to define the column where the index will be persisted.

Personally, I would avoid bag semantic because it has drawbacks. You have to take a look at the generated SQL while using bag semantic. E.g. when you remove 1 element from a collection mapped with bag semantic, the generated SQL first deletes ALL elements from the collection and than inserts ALL elements except the removed one.

I for one would avoid bag semantic and use list or set semantic instead.

Relating your question with ArrayList: Maybe Im wrong, but AFAIK hibernate never uses ArrayList automatically. You have to change the List implementation by yourself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA returning collection of type PersistentBag instead of ArrayList
 
Similar Threads
Is the statement about EJB 3.x right?
hashCode() & equals() method test
Inner join problem
Problem at insert with relationship JPA 2.0
how to use a composite primary key as foreign key