DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Simplify Java Persistence Using Quarkus and Hibernate Reactive
  • How To Build Web Service Using Spring Boot 2.x
  • Getting Started With JPA/Hibernate
  • Rails 6: Multiple DB Support

Trending

  • Designing a Java Connector for Software Integrations
  • Memory-Optimized Tables: Implementation Strategies for SQL Server
  • When Airflow Tasks Get Stuck in Queued: A Real-World Debugging Story
  • Designing AI Multi-Agent Systems in Java
  1. DZone
  2. Coding
  3. Java
  4. JPA Tutorial: Setting up Persistence Configuration for Java SE Environment

JPA Tutorial: Setting up Persistence Configuration for Java SE Environment

Here's how to create a persistence configuration in the Java SE environment.

By 
MD Sayem Ahmed user avatar
MD Sayem Ahmed
·
Sep. 04, 14 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
102.5K Views

Join the DZone community and get the full member experience.

Join For Free

In my previous article, I have written a sample example showing how to configure JPA for running it in a Java SE environment. I also showed a sample persistence.xml file which looks like below -

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence

http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"

  version="2.1">

  <persistence-unit name="jpa-example" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <properties>
      <!-- Configuring JDBC properties -->
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa_example" />
      <property name="javax.persistence.jdbc.user" value="root" />
      <property name="javax.persistence.jdbc.password" value="my_root_password" />
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />

      <!-- Hibernate properties -->
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.format_sql" value="true" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
      <property name="hibernate.hbm2ddl.auto" value="validate" />

      <!-- Configuring Connection Pool -->
      <property name="hibernate.c3p0.min_size" value="5" />
      <property name="hibernate.c3p0.max_size" value="20" />
      <property name="hibernate.c3p0.timeout" value="500" />
      <property name="hibernate.c3p0.max_statements" value="50" />
      <property name="hibernate.c3p0.idle_test_period" value="2000" />
    </properties>
  </persistence-unit>
</persistence>

Those who are only starting out on JPA, please let me explain some of the components of this configuration.

The first section is used for configuring JDBC connection that will be used by the persistence provider. Usually we specify the JDBC url, database username, password and fully qualified name of the Driver class in this section. The second section configures some property values for hibernate, and is explained below -

  1. The “hibernate.show_sql” property specifies whether or not hibernate will print the queries in the log file (provided that you have configured log4j properly). This is specially helpful if you want to view which queries are being executed for reading/writing/deleting some entities. In the production environment you can set to false if you want so that the queries will not be logged.
  2. The “hibernate.format_sql” property specifies whether or not the queries will be formatted to a more readable form before logging.
  3. The “hibernate.dialet” property specifies which type of dialects we intend to use. If you do not know what they are then please read this excellent answer on StackOverflow.
  4. The “hibernate.hbm2ddl.auto” property is very interesting one. By changing its value you can enable hibernate to create/drop your database tables for you, or validate an existing schema against your mapping. This has also been explained very well on StackOverflow.

The last section configures the connection pool that hibernate will use for the database. Hibernate usually provides a built-in connection pooling mechanism which is good enough for development and testing, but is not suitable for production environment. So to get the optimal connection pooling behavior in production you need to use something more mature. C3P0 is a popular production-grade connection pooling library which is very easy to use with Hibernate. All you need to do is just specify the property values like minimum/maximum number of connections in the pool, timeout values etc., and the rest will be taken care of by Hibernate.

There is also another important point that I have skipped in the last article. In order for the entities to be found by the persistence provider in a Java SE environment, they will have to be listed in the persistence.xml file as follows -

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence

http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"

  version="2.1">

  <class>fully.qualified.path.of.the.entity</class>

  <!-- rest of the contents -->

  </persistence-unit>
</persistence>

Doing this will ensure that your entities will be found by the persistence provider and will be ready for persistence. However, hibernate auto-scans the packages for classes marked with the “Entity” annotation and make them persistent, so we did not have to worry about that. Keep in mind that this is also the case when you use JPA in a Java EE environment (i.e., your application runs in a full-blown application server). In this case the application server scans the application during its deployment and finds the classes marked as entities. If you run your application in a Java SE environment using a provider which does not have this type of auto scanning ability, then you will be required to list the entities in the persistence.xml file like above.

That’s it for setting up JPA in a Java SE environment. Hopefully once you have this setup you will be easily able to persist your entities without much trouble.

Java (programming language) Database Persistence (computer science) Hibernate application Property (programming) Connection (dance) Fully qualified name Java EE

Published at DZone with permission of MD Sayem Ahmed, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Simplify Java Persistence Using Quarkus and Hibernate Reactive
  • How To Build Web Service Using Spring Boot 2.x
  • Getting Started With JPA/Hibernate
  • Rails 6: Multiple DB Support

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!