aspose file tools*
The moose likes Object Relational Mapping and the fly likes save an array to database with hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "save an array to database with hibernate" Watch "save an array to database with hibernate" New topic
Author

save an array to database with hibernate

Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Hi there does anyone know how can i save the values of an array into a database .

Every value that an array has to a row from a table example
array [1] ->to a row
array[2]->to a row

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1671
    
    7

What is in the array and how is it being used?

possibilities are

1. It is an array of Entities that you want to save.
-In this case iterate over the array and save each entity.

2. You have an entity that you wish to save which itself contains an array of entities as one of its fields
- here I would use a collection like List or Set rather than an array.

3. You have an entity that you wish to save which itself contains an array of basic objects (like Strings) as one of its fileds
- Here is would still use a collection, but you could look at @ElementCollection.

Hope that helps.


[How To Ask Questions][Read before you PM me]
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Bill Gorder wrote:What is in the array and how is it being used?

possibilities are

1. It is an array of Entities that you want to save.
-In this case iterate over the array and save each entity.

2. You have an entity that you wish to save which itself contains an array of entities as one of its fields
- here I would use a collection like List or Set rather than an array.

3. You have an entity that you wish to save which itself contains an array of basic objects (like Strings) as one of its fileds
- Here is would still use a collection, but you could look at @ElementCollection.

Hope that helps.

ok i have an array of strings Array [i]on this array i generate random Strings and i want to save them every string in a row in a database.

Array[1]->row 1
Array[2]->row2
.
.
.
.
any ideas?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1671
    
    7

Ok from your description I am envisioning a table with one column and each row is one string in your array. Of course I doubt that is what you mean. Is the array of strings part of an object which is a persistent entity for example a list of phone numbers on a Person object. Hibernate is an ORM so usually you will have domain objects with relationships. If you help me understand these relationships I can probably give you better help mapping it correctly.



Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Bill Gorder wrote:Ok from your description I am envisioning a table with one column and each row is one string in your array. Of course I doubt that is what you mean. Is the array of strings part of an object which is a persistent entity for example a list of phone numbers on a Person object. Hibernate is an ORM so usually you will have domain objects with relationships. If you help me understand these relationships I can probably give you better help mapping it correctly.




There are no realations only data from an array that i want to add to arow.
As I told i generate with


random Strings and i pass them to Voucher[i] array.
So then when i want to save them i want the vouchers[i] to be saved in adatabase.I have implemet that the array will be saved in a file and the the file will be saved in the database.THATS OK.

BUT
if i want to save the array by array line like i said
array [potision1]->to be saved in a row in a table (whatever name table has whatever is the name of colum).

For more easy
table
vouchers with columns id and vouchertype.
so i have the id in auto increment and i want that the value of array [potision1] to be stored in vouchertype column.
this will go on as long the voucher[i] array is filled .
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Bill Gorder wrote:Ok from your description I am envisioning a table with one column and each row is one string in your array. Of course I doubt that is what you mean.


Thats correct 2 columns with id in auto increment and a String (varchar)column for the vouchers.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1671
    
    7

If there are no relationships I would not use an ORM. ORM''s solve the impedance mismatch problem (basically making object models and relational models play nice together). In your case you state that there is no relationship, in the database or the object model as far as I can see. I actually wonder what benefit you will gain storing this value as I am not sure how you will retrieve it or use it in a meaningful way. That said if you absolutely had to use hibernate for this you must create an Entity for it to be persistent.

It might look something like below for an auto incremented id and a voucher:



Now when you new up a MyEntity you would only set the voucher the id is auto generated when its persisted. You could iterate over an array or list of these MyEntity objects and save them to the database using either an EntityManager (JPA) or a Hibernate Session.
Georgios Chatziefstratiou
Ranch Hand

Joined: May 26, 2012
Posts: 83
Bill Gorder wrote:If there are no relationships I would not use an ORM. ORM''s solve the impedance mismatch problem (basically making object models and relational models play nice together). In your case you state that there is no relationship, in the database or the object model as far as I can see. I actually wonder what benefit you will gain storing this value as I am not sure how you will retrieve it or use it in a meaningful way. That said if you absolutely had to use hibernate for this you must create an Entity for it to be persistent.

It might look something like below for an auto incremented id and a voucher:



Now when you new up a MyEntity you would only set the voucher the id is auto generated when its persisted. You could iterate over an array or list of these MyEntity objects and save them to the database using either an EntityManager (JPA) or a Hibernate Session.


Hello thank for your answer have a look at this


This is what i have implement untill know
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: save an array to database with hibernate