Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String[] to columns

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been playing around a little bit more with Hibernate but it dawned on me that I'm not really using much object oriented mapping. In fact, I probably could have done what I've done so far much faster just using a JSTL database tag or such.
So, let's say I've got a String array of 25 objects and I want to map each
index of the array to a seperate column in the database.
How do I tell Hibernate that I want this?
Is this what is meant by one to many mapping?
Thanks,
Drew
 
eammon bannon
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is this what is meant by one to many mapping?

No, 1-many mapping is analagous to 1-N relationship in a relational database.
I'm trying to understand why you'd want to map a 25 field DB table as an array of Strings - what is it you want to achieve? You don't map things in Hibernate that way.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by eammon bannon:
I'm trying to understand why you'd want to map a 25 field DB table as an array of Strings - what is it you want to achieve? You don't map things in Hibernate that way.

Sorry, I'm not the best with databases.
What I've done is create this command line utility in Java that parses a incoming plain text email which contains data in CSV format and insert this into a MySQL database.
The parsing class returns the parsed information as a String array.
Each index of the array represents a column in the database.
(name, address, city, state, email, etc.)
So what would be the best way to map this information using Hibernate?
Thanks,
Drew
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Drew Lane:
So what would be the best way to map this information using Hibernate?
Making "(name, address, city, state, email, etc.)" into a class and mapping that class (its fields) into the database schema.
 
eammon bannon
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sorry, I'm not the best with databases.

Quite alright. We're all learning all the time.
I see now why you might want this - given you initial input is an array of command line arguments. How would you map this? I wouldn't let the input dictate how its mapped in Hibernate. Just create your POJO bean classes and your mapping hbm.xml file (or let middlegen do this for you), and handle the input in a DAO or something. The DAO is where you do the work of "mapping" a 25 element String array to an already mapped Hibernate POJO. Something like:

Very, very over simplified and obviously that won't even compile, but you get the idea? My biggest worry is how fixed the 25 element array is - you certainly want some validation on the input methods to stop 26 and 24 (and so on) element arrays being accepted.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lasse Koskela:
Making "(name, address, city, state, email, etc.)" into a class and mapping that class (its fields) into the database schema.

Yes, I already did that but it didn't seem like a very elegant solution.
I spent half an hour or more making the class fixing my typos, then making the mapping file, etc. Seems like time better spent elsewhere.
Since an array is a type of collection I thought there might be a more efficient way.
Drew
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by eammon bannon:

Very, very over simplified and obviously that won't even compile, but you get the idea? My biggest worry is how fixed the 25 element array is - you certainly want some validation on the input methods to stop 26 and 24 (and so on) element arrays being accepted.


Yes. This is pretty much what I did and it worked.
But is there a way to shortcut this:
hibernatePOJO.setField1(contents[0]);
hibernatePOJO.setField2(contents[1]);
hibernatePOJO.setField3(contents[2]);
...
Can I just pass the whole String array to the POJO and still have it map correctly?
Drew
 
eammon bannon
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.
Hibernate, as with all ORM persistance layer solutions, still has the problem that you actually have to map the Object to the relational table. Hibernate removes a whole bunch of "under the hood" work, but there is still no way of getting round the initial tedious task of creating DAOs to use the POJOs - you must explain to Hibernate what data updates what. The real benefit of Hibernate is in theory you will only have to do this once, and with tools like Middlegen, the repetitive work is greatly reduced.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm...
Let me ask this question.
If I want to store a Java collection (array, map, etc.) in a database using Hibernate how does this work? What data type is used in the database? Does each element of the collection have a seperate row in the database?
Regards,
Drew
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Drew,
For example if a Parent class has a collection of child classes; then Hibernate could map this as 1-many relationship such that every item in the child collection is mapped to a row in table "table_child" and each row will have a foreign key back to the parent table.
Regards,
Pho
 
Rudy Dakota
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pho,
A one-to-many relation? I wouldn't think so: you are mapping one child instance to one parent instance. That 's a one-to-one relation.
Let 's keep the terminology clean!
Good riding,
Rudy.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends on what viewpoint you take, it could be a one-to-one or one-to-many.
Given a child object, there is definitely a one-to-one to a Parent. But given a Parent object, there is a collection of child objects. Is the later not a one-to-many to you ?
Pho
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic