0

I'm looking for the best solution to store Java EE application's global data using Hibernate. It will consist of key value pairs. Example:

projectStarted = "10-11-11"
developerNumber = 3
teamLeader = "John"

As you see, all of this entries have different types.

For now I see two options:

1) Create GlobalData entity. Each field of it will be represented as unique column in the table and will contain unique setting. This way I have no problems with type casting, but I would like to avoid it in case where there will be big amount of settings.

2) Create Setting entity. Each of it will contain two fields: key(Primary key) and value and will be represented as unique record in the table. This is preferable solution, but It's seems to me that I will get a lot of type casting, because settings can be any type.

So basically, I'm looking for the way to implement second solution without getting a lot of troubles from different types. Can anybody help me?

Thanks.

Edit 1.

Yeah, thanks Christian. Just got similar idea.

What if I will have Settings entity, which will be like:

@Entity
@Table(name = "settings")
public class Setting {

    @Column
    private String key;
    @Column
    private String value;
    @Column
    private String converterClassFullName; //example by.lugovsky.MyConverter

    //Getters, setters
}

And GlobalData class.

public class GlobalData {
    private Date projectStarted;

    private int developerNumber;

    private String teamLeader;

    Set<Setting> settings;

    //Getters and setters for all, except settings.

}

So basically my idea is to convert Setting entity before persisting/updating/ etc. I can do this in my DAO, but I was wondering, if I could annotate GlobalData class with @Entity annotation as well without creating new table. This way I can set OneToMany annotation to Setting's set and Perform conversions in the internal @PrePersist etc. methods.

Will Hibernate allow me to do this?

Thanks again

2
  • Looks like something you could store in a properties file quite easily. You can still wrap a bean around it for type safe access. Easy to extend and easy to update.
    – Caps
    Aug 3, 2011 at 12:34
  • Sure, but the problem is that I need to provide GUI to change these values. So I would rather store this in the DB. Aug 4, 2011 at 9:31

1 Answer 1

1

You could store a Converter-Class into the db and the let it run through the given converter for a property before using the value. JSF offers Converter API:

public interface Converter{
    public Object getAsObject(FacesContext fc, UIComponent component, String value) throws ConverterException;

    public String getAsString(FacesContext fc, UIComponent component, Object obj) throws ConverterException;
}

If you have a schema with

name: String

value: String

converter: Class

then you could do something like this:

PropertyEntry pe = // Get from OR-Mapper
Converter c = (Converter) pe.getConverter().newInstance();
Object o = c.getAsObject(null, null, pe.getValue());
// use the object o instead of value

For even more coolness you could also define a field in the class which will not be persisted which you could use to hold the converted value within the object.

2
  • I can not tell you for sure if that will work, but I think that it will not. You really have to try it out, but isn't this flexible enough? Don't try to overengineer such generic approaches, they will get very tricky soon. Let me know when you have tried it! Aug 4, 2011 at 18:40
  • I have tried it in the end. But I didn't managed to let Hibernate handle this without creating extra table. So I'm just performing all the necessary conversions in my Dao, just like I thought. Dao encapsulates conversion logic and on the service level I can work with GlobalData instance without knowing how it's produced. I'm using reflection to access and convert fields of global data class. This way I can add new record in the database by simply adding new field to GlobalData. Removing field also removes the record from the DB. I appreciate your help, thanks. Aug 5, 2011 at 5:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.