user2268507
user2268507

Reputation:

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!

Upvotes: 0

Views: 1944

Answers (1)

Breno Inojosa
Breno Inojosa

Reputation: 602

You can use hibernate, yes, but... when modelling a database, you don't need to think of the relations as HashMaps.

Instead, think of each relation. HashMap is key => value. In your case, the 'value' is an array list, which in a database is represented by a 1:N relationship.

So all you want is a class called 'Key' with an array of values inside it. You can use hibernate to create 1:N mappings in a very easy way, yes. So when you get the element with a certain key, you will also get your entire array list automatically.

1:N / 1->n in hibernate is called one to many. Check this link : http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example/

Upvotes: 1

Related Questions