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?
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.
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.
Joined: May 14, 2001
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.
Joined: Sep 19, 2007
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.
subject: JPA returning collection of type PersistentBag instead of ArrayList