The right MySQL persistence.xml example file for JPA 3 and Hibernate 6
JPA 3 and Hibernate persistence.xml file
As developers migrate their old Java Persistence API applications to JPA 3, their progress is often halted when an incorrectly configured JPA 3 persistence.xml file blocks their Java applications from connecting to the underlying MySQL database.
The the two most common JPA 3 persistence.xml file misconfigurations are:
- The use of javax.persistence names instead of jakartaee.persistence
- An incorrectly configured MySQL dialect
- An incorrectly configured MySQL JDBC driver
JakartaEE package name change
This is a simple error to correct. Unfortunately, it’s equally easy to overlook.
All of the old javax package name prefixes were converted to jakartaee when the enterprise Java platform was handed over to the Eclipse foundation.
Part of the transfer was a chance in the name of every package in the API. As such, all the old javax prefixed properties in the persistence.xml file must be converted to jakartaee.
<!--property name="javax.persistence.jdbc.user" value="root" /--> <property name="jakarta.persistence.jdbc.user" value="root" />
This change must be made to every reference to javax in the JPA 3 persistence.xml file.
JPA 3 Hibernate dialect for MySQL
Prior to the Hibernate 6.x release, MySQL required a version specific dialect that addressed whether the underlying database was configured with InnoDB or MyISAM.
Hibernate dialect options in prior releases included:
- MySQL55Dialect
- MySQL57InnoDBDialect
- MySQL57Dialect
- MySQL5InnoDBDialect
- MySQL8Dialect
However, these classes are now all deprecated and the generic MySQLDialect class is recommended instead.
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
MySQL JDBC driver for Hibernate and JPA 3
Developers should also be careful that they are no longer referencing the old MySQL JDBC driver.
The old com.mysql.jdbc.Driver should not be used anymore.
Newer Hiberante and JPA 3 implementations should use the com.mysql.cj.jdbc.Driver class for MySQL connections.
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
Correct persistence.xml file for JPA 3, Hibernate and MySQL
With the above misconfigurations addressed, a modern persistence.xml file for JPA 3, Hibernate and MySQL configurations looks as follows:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence version="3.0" xmlns="https://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"> <persistence-unit name="jpa-hibernate-mysql"> <properties> <property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" /> <property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hibernate_examples" /> <property name="jakarta.persistence.jdbc.user" value="root" /> <property name="jakarta.persistence.jdbc.password" value="password" /> <property name="jakarta.persistence.schema-generation.database.action" value="create" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> </properties> </persistence-unit> </persistence>
A discussion of the configuration of the persistence.xml file for version 2.2 of the Java Persistence API can be found here.