News Stay informed about the latest enterprise technology news and product updates.

The right MySQL persistence.xml example file for JPA 2.2 and Hibernate 5

As MySQL installations continue to migrate and evolve to newer versions, the online JPA and Hibernate 5 tutorials haven’t kept up with quite as much vigor. As a result, many developers that learned JPA 2.2 and started with Hibernate 5 on MySQL have run into roadblocks before their SessionFactory or EntityManager ever gets a chance to start.

What’s the issue? Expired MySQL persistence.xml properties for attributes such as the dialect and the JDBC driver.

As you can see from the Hibernate 5, MySQL persistence.xml example below, the necessary changes are not difficult to make.

Use the correct MySQL dialect

For those who use MySQL Community Server version 8, they must also use the MySQL8Dialect. Modern versions of MySQL use InnoDB under the covers, and if a developer uses the older MySQLDialect, it will trigger the MyISAM exception.

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect" />

If you don’t use version 8, here’s are some MySQL dialects that might be more appropriate than the default if you’ve migrated beyond MySQL version 4:

  • MySQL55Dialect
  • MySQL57Dialect
  • MySQL5Dialect
  • MySQL8Dialect
fix MySQL MyISAM Exception

Use the correct dialect in your MySQL persistence.xml JPA file.

The correct MySQL persistence.xml driver

Another issue I see quite regularly is developers reference example MySQL JPA persistence.xml files that use the older com.mysql.jdbc.Driver. If a developer executes queries against a MySQL 8 database with this driver it will produce unsettling warning in your Hibernate logs. The com.mysql.cj.jdbc.Driver is the correct one to use.

<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>

With those adjustments in mind, here’s what a modern MySQL persistence.xml JPA example file looks like:

MySQL persistence.xml example

MySQL persistence.xml example for JPA 2.2

You can find a copy of this example MySQL persistence.xml file for Hibernate 5 and JPA 2.2 on my GitHub account.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchAppArchitecture

SearchSoftwareQuality

SearchCloudComputing

SearchSecurity

SearchAWS

Close