JPA: Singleton Entity

I got to deal with an entity that has one and only one instance.

The point is to simulate a configuration class that will be created once, updated many times and never deleted.

The main idea is that the object will be similar to a properties file, but stored in the database.

Are there any modeling guidelines for this class, tips or ideas?

+4
source share
2 answers

I will answer a more general question - How to implement the configuration of stored database applications well ?

, , . , CarConfig, CacheConfig, BookingConfig .., AppConfig. / .

, , ( ), / , , :

:

public interface CarConfig {       
    @PropertyName("default.color")
    @DefaultValue("red")
    String getDefaultColor();

    void setDefaultColor(String color);

    @PropertyName("max.size")
    @DefaultValue(100)
    int getMaxSize();

    void setMaxSize(int size);

    ...
}

, . .

getters (: getDefaultColor()) - @PropertyName. . ( ) ( getMaxSize() int) .

. @PropertyName - .

, , , :

CarConfig carConfig = configs.get(CarConfig.class);
String defaultColor = carConfig.getDefaultColor();

? - , , , , , . , .

:

|     key       |value |
...
|"default.color"|"blue"|
|"max.size"     |"85"  |
...

, - getter setter - - .

CUBA - https://www.cuba-platform.com/.

, , :

  • ;
  • / .
+1

JPA, Facade,

Configuration getConfiguration(String key);
Configuration addConfiguration(Configuration configuration);
Configuration updateConfiguration(Configuration configuration);
void deleteConfiguration(Configuration configuration);
Collection<Configuration> getCompleteConfiguration();

, , , . , . , JPA, , , .

ConfigurationEntity ( JPA / )

@Entity
@Table(name = "CONFIGURATION")
public class ConfigurationEntity {

    @Id
    @GeneratedValue
    private Long id;
    private String key;
    private String value;
....
+2

Source: https://habr.com/ru/post/1651878/


All Articles