stenda
stenda

Reputation: 11

JPA/Hibernate how to map a bean that represent "application properties"

for a project I'm working on ( a cms solution), I have a table that store all the properties of the web site of the user (managed by the cms).

The table has this DDL:

CREATE TABLE SITE_OPTIONS(
    OPTION_ID int generated always as identity PRIMARY KEY,
    OPTION_NAME varchar(200) not null,
    OPTION_VALUE text,
    LANGUAGE_ID int not null
)

This is an example of the table content:

+-----------++---------------++------------------++-------------+
| OPTION_ID || OPTION_NAME   || OPTION_VALUE     || LANGUAGE_ID |
+-----------++---------------++------------------++-------------+
|         1 || SITE_TITLE    || My site title    ||           1 |  
+-----------++---------------++------------------++-------------+
|         2 || SITE_SUBTITLE || My site subtitle ||           1 |  
+-----------++---------------++------------------++-------------+
|         3 || TEMPLATE      || interior_designer||           1 |  
+-----------++---------------++------------------++-------------+

What I want, or better I would like, is a bean that represent the "siteoptions":

public class SiteOptions{
    private String siteTitle;

    private String siteSubtitle;

    private String template;

    //getters/setters
}

I have already a Repository based architecture (classic Repository Pattern impl) that I can use to implement the read/save functions.

Do you know if I can accomplish that with pure JPA/Hibernate?

Or have you ever managed this kind of situation?

Thanks in advance!

Upvotes: 0

Views: 79

Answers (1)

Dragan Bozanovic
Dragan Bozanovic

Reputation: 23552

When saving SiteOptions, simply transpose the options matrix to the entity class that corresponds to the database table structure:

@Entity
public class SiteOptionEntity {
   @Id
   @Column(name = "OPTION_ID")
   private Long id;

   @Column(name = "OPTION_NAME")
   private String name;

   @Column(name = "OPTION_VALUE")
   private String value;

   @ManyToOne
   @JoinColumn(name = "LANGUAGE_ID")
   private Language language;
   ...
}

Of course, use the appropriate id generator and decide whether to introduce a separate Language entity (as shown above) or to use the language id only.

Upvotes: 1

Related Questions