Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Hibernate be used to store HashMaps of data without classes to represent their structure?

I have pretty much zero experience with Hibernate, though I've used similar persistence libraries in other languages before. I'm working on a Java project that will require a way to define "models" (in the MVC sense) in text configuration files, generate the database tables automatically, and (ideally) be database-backend-agnostic. As far as I can tell from some quick Googling, Hibernate is the only widely-used backend-agnostic Java database library; while I could write my own compatibility layer between my model system and multiple DB backends, that's a debugging endeavor that I'd like to avoid if possible.

My question is: Can Hibernate be used to store data whose structure is represented in some other way than an annotated Java class file, such as a HashMap with some configuration object that describes its structure? And, if not, are there any other relatively-stable Java database libraries that could provide this functionality?

EDIT: Here's a clearer description of what I'm trying to accomplish:

I am writing a data-modeling library. When a certain method of the library is called, and passed a configuration object (loaded from a text file), it should create a new "model," and create a database table for that model if necessary. The library can be queried for items of this model, which should return HashMaps containing the models' fields. It should also allow HashMaps to be saved to the database, provided their structure matches the configuration files. None of these models should be represented by actual compiled Java classes.

like image 286
Adam R. Nelson Avatar asked Dec 19 '12 06:12

Adam R. Nelson


2 Answers

I think you could try use @MapKey annotation provided by JPA (not the Hibernate @MapKey annotation, it's pretty different!).

@javax.persistence.OneToMany(cascade = CascadeType.ALL)
@javax.persistence.MapKey(name = "name")
private Map<String, Configuration> configurationMap = new HashMap<String, Configuration>();
like image 193
Kamil Avatar answered Sep 25 '22 11:09

Kamil


I don't believe Hibernate will let you have a Map as an @Entity but it will let you have a custom class that contains a map field:

@Entity
public class Customer {
    @Id @GeneratedValue public Integer getId() { return id; }
    public void setId(Integer id) { this.id = id; }
    private Integer id;

    @OneToMany @JoinTable(name="Cust_Order")
    @MapKeyColumn(name"orders_number")
    public Map<String,Order> getOrders() { return orders; }
    public void setOrders(Map<String,Order> orders) { this.orders = orders; }
    private Map<String,Order> orders;
}

(example from Hibernate docs)

Additionally, you don't have to use annotations (if that is what you're trying to avoid): Hibernate relationships can be described via xml files and there are utilities (maven plugins for example) which can automatically generate the necessary java pojo's from the xml.

Does your model require a relational database? You might consider a database like Mongo that stores any object you can represent with JSON.

like image 34
Mark Avatar answered Sep 23 '22 11:09

Mark