• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is it possible to persist an ArrayList of POJO into database in hibernate?

 
vighnesh udupa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a situation where i have an arraylist containing POJO objects for which i have a DB table. can somebody tell me is it possible to persist this arraylist to DB, if possible how to achieve the same?

Thanks,
Vighnesh
 
Reddy Prashanth
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It is possible to persist a list of collections (like Set, Map or List). Here is a simple example.
Pojo : Pet.java


Pojo : User.java

In mapping file :


If you want to map the Pet pojo as entities instead of value types, you can use "many-to-many" element instead of the composite element.
Hope this is what you are looking for.


 
vighnesh udupa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks prashanth for a good example.

but my requirement is like I have an arraylist containing pet like POJO's and this arraylist is not included in any other POJO. I want to persist this arraylist to DB. Is it possible to directly persist this arraylist without iterating each time to get POJO and then persisting that POJO into DB?

Thanks,
Vighnesh
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vighnesh udupa wrote:Is it possible to directly persist this arraylist without iterating each time to get POJO and then persisting that POJO into DB?



Why don't you want to iterate?
 
vighnesh udupa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you want to iterate?


I can iterate and persist, but if I have 100 POJO's inside arraylist I need to iterate 100 times and persist 100 times, it might cause performance problems. So thought of searching for some other method to do the same and searched in net,but unlucky to get the idea to implement it. Is it possible to persist arraylist of POJO's directly? If possible how can I implement it?
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't directly persist a collection. You still have to iterate. But there are ways to manage those batch inserts in a better way. See this
 
vighnesh udupa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reddy prashanth wrote:Hi,

It is possible to persist a list of collections (like Set, Map or List). Here is a simple example.
Pojo : Pet.java


Pojo : User.java

In mapping file :





Hi,

Can you please tell me what is
<key column="puid" />
<list-index column="listOrder" />


From where we need enter this value, I mean which value should be mapped to these 2 fields?
 
Reddy Prashanth
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The <key column="puid" /> is a foreign key column that is used in the collection table (PetsList table here). It is nothing but the primary key of the Entity "User". the column name can be anything but it is filled with the value of the User class identifier property (uid here).

The list is stored in the database table. It has to maintain the order of the elements in the List. So the list-index column is used.
<list-index column="listOrder" />
 
Radovan Vukovic
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you try to use saveOrUpdateAll(Collection) ?
 
vighnesh udupa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks prashanth and Radovan.
have you try to use saveOrUpdateAll(Collection) ?


is saveOrUpdateAll(Collection) method available in normal Hibernate? as I referred in web it's using some spring concept along with hinernate template. Is it possible to use this method without any spring related stuff, if so can you please tell how to implement this?
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

Have a look at this link http://docs.jboss.org/hibernate/core/3.3/reference/en/html/batch.html.

Sean
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic