Hibernate and PostgreSQL configuration using persistence.xml and EntityManager

In this tutorial I am going to show you how to use Hibernate with Entity Manager. It is based on my previous tutorial and it is an enhancement of this code. In previous tutorial I was using “hibernate-entitymanager” dependency, so there is no need to do any modifications in pom.xml now.

1. Create persistence.xml file in src/main/resources/META-INF directory. You can also remove hibernate.cfg.xml, but it is not mandatory.

2. Modify AppTest class to be consistent with EntityManager usage.


3. After that all you should have directory structure like below:


4. Execute mvn test command in the project’s directory.

If everything is OK, you should have your second entry in the database persist by Hibernate!


Complete source code: https://github.com/jvmhub/Hibernate-and-PostgreSQL-configuration-using-persistence.xml-and-EntityManager