Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Data type problem

 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,I have a problem about data mapping.

However, I want to store a Map object into mySql database.

What type my column inside what I store Map, should be? I tried BLOB, and use and also during object retrieving .
And it want work. Actually it does work, but when I try to cast retrieved object to Map i get an exception.
So, What type my column inside what I store Map, should be - - what method on statement object should set (setObject() or what, and --- what method of result set should use to retrieve Map from database? And also, should I performed the same tactic on Lists?
Thanks in advance...
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to store a complete Java object in a relational database?

This is indeed not going to work. Either serialize it to an inputstream, or just store the map contents.
 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you mean "Map content"? To extract each key/value from Map, and store it as a String or...?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends on the functional and design requirements. Maybe you shouldn´t be using a database at all. But intuitively I would just store each key-value pair as a new row in a table.
 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, as I can see, the best choice is to avoid the Map, and use List. It's much more ease and flexible usage with the same effect accomplishing...
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't say anything about this. It depends on the functional requirements which you didn't say a word about. List and Map have each its own advantages. If you pick the wrong one for your functional requirement, then you'll only get disadvantages. That's nothing more than obvious.
 
Avyaya Avi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Bauke.

As suggested, simple means would be to store the key/value pairs as seperate entities.
 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, it has an influence at performance also, but later during problem resolving I figure out that Bauke was right at first place. Collections I use for mapping some data in Struts framework, but after it I'll extract key-value pair and save it separately as String into database. Anyway thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic