Can't get Hibernate with JPA to work in Eclipse

I’ve imported the Hibernate Spatial tutorial into Eclipse but when the main application is run, it can’t find the SPATIAL-JPA definition in persistence.xml. persistence.xml is in target\classes\META-INF and is found in the application with:
URL resource = Thread.currentThread().getContextClassLoader().getResource(“META-INF/persistence.xml”);
emFactory = (Persistence.createEntityManagerFactory(“SPATIAL-JPA”));
fails with
Cannot create EntityManagerFactory.
Exception in thread “main” java.lang.ExceptionInInitializerError
** at recipe.HSDB2.getEmFactory(**
** at recipe.HSDB2.createEntityManager(**
** at recipe.HSDB2.main(**
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named SPATIAL-JPA

The application runs fine from the command line using the mvn exec command.

If it runs fine with Maven, then you should import the project in Eclipse from Maven.

I haven’t used Eclipse since 2005 as I had switched to using IntelliJ IDEA and never looked back. If you import the project from Maven in IDEA, it works like a charm.

I had imported the Maven project. It imported nicely, builds and runs until it tries to create and EntityManagerFactory.

I’ve used Eclipse since before it was Eclipse but will try the community version of IDEA.

I tried to run in IDEA but got similar but slightly different errors - no idea the cause.
Cannot create EntityManagerFactory.
Exception in thread “main” java.lang.ExceptionInInitializerError
at recipe.HSDB2.getEmFactory(
at recipe.HSDB2.createEntityManager(
at recipe.HSDB2.main(
Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
at org.hibernate.boot.spi.XmlMappingBinderAccess.(
at org.hibernate.boot.MetadataSources.(
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.(
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.(
at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(
at javax.persistence.Persistence.createEntityManagerFactory(
at javax.persistence.Persistence.createEntityManagerFactory(
at recipe.HSDB2.getEmFactory(
… 2 more
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(
at java.base/java.lang.ClassLoader.loadClass(

You got a ClassNotFoundException for JAXB. sounds like Java 9 modules issue.

Check this StackOverflow question for more details:

Thank you.
This was actually a new problem in moving to IDEA. The classNotFound error didn’t occur in Eclipse for whatever reasons.

The real problem was that the DB2 JDBC driver jar file was not in the build path. I found it by debugging into HibernatePersistenceProvider until it got an exception. Too bad that by the time it exits, it just says:
javax.persistence.PersistenceException: No Persistence provider found for EntityManager named SPATIAL-JPA.

Adding the DB2 JDBC driver to the path made it work for both Eclipse and IDEA.
I’m not sure why it worked fine from the command line - must be different environment.

By the way, where did you get the Hibernate Spatial Tutorial from?

The official bundle contains the following tutorials only:

  • annotations
  • basic
  • entitymanager
  • envers
  • osgi

And, there’s no SPATIAL-JPA in the entire hibernate-orm project.

Karel Maesen’s spatial tutorial. I’ve been working with him to implement DB2 support in Hibernate Spatial.

When Hibernate Spatial was incorporated into Hibernate ORM, it seems that the tutorial was left out. You should open a Jira issue for this.