• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: independent key value pair mapping

 
Frans de Gooi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using hibernate and would like to make an independent key value pair mapping. To give you an idea, it is a mapping for config values stored in a database, e.g. key="timeout", value="30", etc.

All examples I find on the internet are of the following type:



That means there is a parent class Foo with an id that refers to an id in the table with the key value pairs. But in my case, there is no such class, I just want to obtain a java Map with key value pairs

Anybody has any idea how to do this?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have a table unrelated to anything else that contains your key - value pairs? Hibernate can't turn this into a Map on its own, but its a fairly simple step to perform manually in your DAO layer isn't it?
 
Frans de Gooi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for answering Paul.

You mean manually reading everything, looping through a list of (mapped) classes which each contain a key value pair, and store these in a hashmap? Of course I could do that but I was just wondering if hibernate could do that for me. But apparently, it can't.

In principle I wouldn't mind doing that, but in that situation how do I make the result persistent? Because I don't want to read all the config values from the database every time when I need a config value. Of course I could store the loaded config values in a static variable like always, but I mean using hibernate. Sorry, I am fairly new to Hibernate

Frans
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You mean manually reading everything, looping through a list of (mapped) classes which each contain a key value pair, and store these in a hashmap? Of course I could do that but I was just wondering if hibernate could do that for me. But apparently, it can't.

Classes? Do you mean objects, or do you have great deal of mapped key/value tables?

Hibernate works by the principal that an object instance == a single row (at least, that's how it behaves roughly, there can be exceptions). If you wanted to get Hibernate to read all this into a Map for you, you'd need to subvert the data model and make your key/value table(s) related to something other. Given this is a messy workaround, I'd just read in the contents of the table and populate the map yourself.


In principle I wouldn't mind doing that, but in that situation how do I make the result persistent? Because I don't want to read all the config values from the database every time when I need a config value. Of course I could store the loaded config values in a static variable like always, but I mean using hibernate. Sorry, I am fairly new to Hibernate

The result is already persistent, since it comes from a persistent store (the database). If you want to cache this Map in memory (which is a differernt thing from persistent) you'll need to do it as you would for any cached data (static variable, singleton, or using a caching service).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic