• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

save an array to database with hibernate

 
Ranch Hand
Posts: 106
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Georgios Chatziefstratiou
Ranch Hand
Posts: 106
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 106
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 106
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 106
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic