0

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!

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

1 Comment

just adding some information that might be obvious... the Key class will have an ID and it will have its own table in the DB. The Value class will have an ID of its own, of course, but also a pointer to the Key (like a 'fk_key_id' foreign key) so that hibernate will be able to tell "how to get all Value objects for a determined key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.