Modern persistence.xml for Java 25, Hibernate 7.4 and MySQL
Older JPA examples often contain a mixture of obsolete javax.persistence property names, old MySQL driver classes and Hibernate 5 or 6 configuration. For a modern Java 25 application, use Jakarta Persistence 3.2 and a current Hibernate ORM release.
As of this update, Hibernate ORM 7.4 is the current stable Hibernate series and supports Java 25. It implements Jakarta Persistence 3.2.
Use jakarta.persistence, not javax.persistence
The namespace is jakarta.persistence. It is not jakartaee.persistence. For example, the JDBC user property is:
<property name="jakarta.persistence.jdbc.user" value="root"/>The same namespace applies to the standard JDBC URL, driver and password properties.
Modern MySQL JDBC driver
Do not use the legacy com.mysql.jdbc.Driver class. MySQL Connector/J uses com.mysql.cj.jdbc.Driver.
<property name="jakarta.persistence.jdbc.driver"
value="com.mysql.cj.jdbc.Driver"/>Hibernate 7 MySQL dialect
Old Hibernate examples frequently specify version-specific dialects such as MySQL5Dialect, MySQL57Dialect or MySQL8Dialect. Those old version-specific choices should not be copied into a Hibernate 7 application.
Hibernate can normally determine the appropriate dialect from the JDBC connection metadata, so an explicit dialect is usually unnecessary. If you deliberately configure one, use the generic MySQL dialect:
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQLDialect"/>
Modern Hibernate applications can normally determine the MySQL dialect from JDBC metadata.
Jakarta Persistence 3.2 persistence.xml example
Jakarta Persistence 3.2 uses the 3.2 persistence schema. A clean resource for a Java 25 application looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<persistence
xmlns="https://jakarta.ee/xml/ns/persistence"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://jakarta.ee/xml/ns/persistence
https://jakarta.ee/xml/ns/persistence/persistence_3_2.xsd"
version="3.2">
<persistence-unit name="jpa-hibernate-mysql"
transaction-type="RESOURCE_LOCAL">
<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.show_sql"
value="true"/>
<property name="hibernate.format_sql"
value="true"/>
</properties>
</persistence-unit>
</persistence>Where persistence.xml belongs
For a Maven or Gradle application, place the file in the standard persistence-unit location:
src/main/resources/META-INF/persistence.xmlHibernate 7.4 Maven dependencies for Java 25
The persistence file configures the persistence unit, but the application still needs Hibernate ORM and MySQL Connector/J on its runtime classpath. A Maven project can declare them as follows:
<properties>
<maven.compiler.release>25</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>7.4.5.Final</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.4.0</version>
</dependency>
</dependencies>The important migration points are simple: use the jakarta.persistence namespace, use the current MySQL Connector/J driver class, avoid obsolete version-specific MySQL dialects, and use the Jakarta Persistence 3.2 XML schema with Hibernate 7.4.
Cameron McKenzie is an AWS Certified AI Practitioner, Machine Learning Engineer, Solutions Architect and author of many popular books in the software development and Cloud Computing space. His growing YouTube channel training devs in Java, Spring, AI and ML has well over 30,000 subscribers.