• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JPA returning collection of type PersistentBag instead of ArrayList

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 84
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic