enumeration table? how to on pure jpa?

#1

Hello , I've relational db , and there is some table named enumerations with , some key and values.We will migrate to objectdb.
Our problem is , we want to store some keys and values for global application configs.

Like;

tablename : enumeration

table rows:

key            value

servername     127.0.0.1
serverPort     4444
username       test
password       test
pictureCount   588
...

 

Is there any JPA annotation that can handle all those easiest way ?

Like this:

@Entity
@Table(name="enumerations")
public class Enumerations {
    //Field binding possible like below ? 
    @Column(field="servername")
    private String serverName;
    @Column(field="username")
    private String username;
    @Column(field="serverPort")
    private Integer serverPort;

    public final String getServerName() {
        return serverName;
    }
       
    public final void setServerName(String serverName) {
        this.serverName = serverName;
    }
}

So , when i make set , get servername I will directly set or get config from server.

I know there is no field value on Column annotation but i need solution like that on pure JPA if possible.

Fastest clear way...

I've looked some other annotations but i could not find any solution like this.

#2

If you use an Enumerations class as demonstrated above, you will have a single entity object that you can retrieve easily by:

    Enumerations e = em.createQuery(
        "SELECT e FROM Enumerations", Enumerations.class).getSingleResult();

 

Alternatively you can define a special entity class, e.g. GlobalProperty with two String fields key and value, and store your global properties using multiple entity objects of that class.

The key field could also be set as a primary key for fast and simple access by find:

    String serverName = em.find(GlobalProperty.class, "serverName").getValue();

 

ObjectDB Support
#3

Sometimes doing these by hand forces users some mistakes when configuration count increase much.
It would be good to natively add into ObjectDB like me shown on top.That helps us utilizing autocomplete from IDE and minimizing error string mapping.

#4

You may use your code at #1 with the query at #2.

Since you asked for a pure JPA solution - adding an extension to ObjectDB will not help.

ObjectDB Support

Reply