Title: JPA - What's the best practice way to load static data from a Database lookup table?
Tags: java;database;jpa
Question: I have a lookup table in my relational database schema called UserRole where it has the following table columns:
Role_Id | Role_Name | Role_Desc | (some other foreign key ids linking to other tables) |
-------------------------------------------------------------------------------
1       | Admin     | Admin user| ......                                               |
All rows in above table are static data (they never ever change in my application)
At the moment i have an Entity persistent object:
@Entity
@Table(name = "UserRole")
public class RoleEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Role_Id")
private int id;
@Column(name = "Role_Name")
private String role_name;
@Column(name = "Role_Desc")
private String role_desc;
// other code omitted for brevity
I have an equivalent Repository class which just fetches the data: for e.g.
entityManager.find(...).getResultsList() etc...
................................................................
Every time when i need to use these roles i am making a call to the database directly. I feel there is no need for this because the data never change in my case. I thought of storing the data in an Enum instead and not have that data in the database but the data is already part of a legacy system and i can't change the database design plus that table already has many links to other tables.
I want to load them only once webercynthia@example.org.
What is the best way to do this?
Is it simply just have like a singleton class to maintain a (static?) list of all the roles, and after initialising, use the Repository class to make a call to Database to fetch the data into the list in memory.?
Comment: why do people think there is one magical "correct" *way* and all other *ways* are somehow wrong. Decide what works for you and move forward with it

Comment: ah my apologies, my wording of the question may not be that good. I meant to ask what is the 'best practice' way of doing it, if there is one at all?

Comment: thanks, @OndrejM that seems to work well for me

Comment: There is no pattern in JPA nor in Java EE to do it in the best way. I suggest the simplest solution possible - do as you write and create a singleton bean (e.g. `@ApplicationScoped`), which would store the results of your query.

Here is the accepted answer: What worked for this particular problem is what OndrejM suggested. It as simply as creating a singleton bean and store results of my query upon startup. And of course, there is no pattern or official way to do it. That is the answer.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:10/13/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><database><jpa>', 'Title': "JPA - What's the best practice way to load static data from a Database lookup table?", 'ViewCount': 1435, 'id': 33100361, 'user_AboutMe': '<p>I am a 20 Something Software Developer.</p>\n\n<p>I specialise in Software Engineering focusing on development having technical expertise in a wide variety of areas within it. My skills set consists of:</p>\n\n<ul>\n<li>Java</li>\n<li>JavaEE (JPA/JTA/EJB/CDI/JSP/JSTL/JMS)</li>\n<li>Spring Framework, Spring Boot/Dropwizard</li>\n<li>JDBC, Hibernate </li>\n<li>Html/CSS/JavaScript/Bootstrap</li>\n<li>SQL</li>\n<li>Oracle/MySQL/PostgreSQL/DB2/HSQL/H2</li>\n<li>ActiveMQ, JBoss, Tomcat, Jetty</li>\n<li>XML/JSON/YAML</li>\n<li>Maven/Gradle/Git/Sonar/Jenkins/Vagrant/Nexus</li>\n<li>JUnit &amp; Mockito</li>\n<li>Netty</li>\n<li>LAMP stack development</li>\n</ul>\n\n<p>I am currently in the process of learning:</p>\n\n<ul>\n<li>Microservices</li>\n<li>Docker</li>\n<li>NoSQL (MongoDB etc)</li>\n<li>Reactive Streams</li>\n<li>AngularJS/React/Polymer</li>\n<li>NPM/Bower/Grunt/Gulp/Webpack/Browserify</li>\n<li>MEAN stack</li>\n<li>Vert.x</li>\n</ul>\n', 'user_AccountId': 2193313.0, 'user_DisplayName': 'SoftwareDeveloper', 'user_Reputation': 1064.0, 'user_Views': 150.0, 'user_id': 1939735.0}}