How do I store a HashMap in a database when the values will be arrays?
-
http://stackoverflow.com/questions/24836649/how-to-store-a-hashmap-in-a-database-whose-values-are-arrays I'm trying to store a HashMap that will contain Arrays as values. When I query the database, I would like to retrieve the hashmap as a java object so that I can perform operations on it as if it were simply a hashmap within the java program. Is this possible? Would something like Hibernate work here? Thanks for your help!
-
Answer:
Yes, you can use any orm framework to store Java objects. The most popular frameworks for this are Hibernate and Openjpa. You can connect it to any database - Mysql, MS SQL, Oracle, and even Nosql ones like Mongo. There are some annotations you have to make to show how you would like the relationships between objects to be - one to many, many to many, etc In your case, if you store the HashMap, it will be stored as a blob so it won't be readable unless you deserialize it with your application. Storing Arrays however are readable directly in the database. They are converted to strings of "[...]" , which store the ids of objects, or the values themselves if they are primitives.
Abhilash Murthy at Quora Visit the source
Other answers
Since HashMap implements Serializable and the constituent objects which are probably a Sting for key and an array for value are also Serializable, I think it is possible to store this Java object as a blob into DB. Or as a simpler solution, since array size is fixed, assume it is of size 5. You need to create a 7 column table. Column 1 will store a the unique identifier for every HashMap record. Column 2 will store the key in HashMap. The rest four columns will be occupied by the array contents(value in HashMap). Like you have already mentioned, JPA with hibernate can be used to persist Java objects as such in DB. Here's a sample : http://www.objectdb.com/api/java/jpa/MapKeyClass . So your code should look like : @Entity public class Example { @Id long id; @ElementCollection @MapKeyColumn(name="myMapKey") @Column(name="myMapValue") Map<String, String[]> myMap = new HashMap<String, String[]>(); }
Radhikaa Bhaskaran
Related Q & A:
- How can I sort a column in a DataGrid?Best solution by Stack Overflow
- How can I inject a constant into a directive's controller?Best solution by stackoverflow.com
- How do i find a profile of a person with a yahoo mail address?Best solution by Yahoo! Answers
- How do I save a video from a CD to a computer?Best solution by Yahoo! Answers
- How can I get a job at a resort for a Summer?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.