Home  >  Blog  >   JBoss

Configuring Data Persistence - JBoss

Rating: 4
  
 
3002

Configuring data persistence

After our short excursus on the EJB remote client Api, we will further enhance our application adding a Persistence Context to it so that we are able to store the key-value pairs on a relational database instead of keeping them in memory. Again, weremind you that the purpose of it is not to teach the theory behind Data Persistence, but rather to show how to configure it within your applications.

As you probably know, a major enhancement in Java EE spec 3.x technology is the addition of the new Java Persistence API, which simplifies the Entity Persistence model.

Inclined to build a profession as JBOSS Developer? Then here is the blog post on JBOSS TRAINING ONLINE

In the Java Persistence API, what used to be called Entity Beans in EJB 2.x are now simply called Entities. The persistence subsystem is included by default into all server configurations:

The javax.persistence Api are not loaded by default in the application server.However, as soon as the application server will detect the main configuration file (named persistence.xml) or persistence annotations, the persistence Api will be automatically linked to your application. So, let’s add the JPA persistence.xml configuration file to our project, which will reference the datasource used for mapping our entities to the database:

The key attributes of this file are the persistence unit name, which will identify its unique name, and the jta-data-source, which must match with a valid datasource definition. In the earlier chapter, we have defined this datasource bound to a MySQL database.

                                                        

Project explorer

Note

The persistence.xml can specify either a JTA datasource or a non-JTA datasource.The JTA datasource is expected to be used within the EE environment (even when reading data without an active transaction). Finally, the properties element can contain any configuration property for theunderlying persistence provider.

Frequently asked Jboss Interview Questions

Since JBoss AS usesHibernate as the EJB3 persistence provider, you can pass in any Hibernate options here.Once created, this file needs to be placed in the META-INF folder of your source classes.

MindMajix YouTube Channel

Tip

The real path of persistence.xml file

Please note that the Eclipse src/META-INF virtual path will be translated at runtime into the WEB-INF/classes/META-INF of your web application.

Using a default datasource for the JPA subsystem

In this example, we are referencing the datasource from within the persistence.xml, thus following a “canonical” approach well-known to many developers.

You can, however, choose a default datasource for all your JPA applications, by adding the default-datasource element into the JPA subsystem:          

This way, all JPA applications that haven’t defined the jta-data-source element in persistence.xml will use the default-datasource configured in the main server configuration file.

Explore Jboss Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!
Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
JBoss TrainingMar 23 to Apr 07View Details
JBoss TrainingMar 26 to Apr 10View Details
JBoss TrainingMar 30 to Apr 14View Details
JBoss TrainingApr 02 to Apr 17View Details
Last updated: 03 Apr 2023
About Author

Ravindra Savaram is a Technical Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.

read more