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”);
but
emFactory = (Persistence.createEntityManagerFactory(“SPATIAL-JPA”));
fails with
Cannot create EntityManagerFactory.
Exception in thread “main” java.lang.ExceptionInInitializerError
** at recipe.HSDB2.getEmFactory(HSDB2.java:155)**
** at recipe.HSDB2.createEntityManager(HSDB2.java:162)**
** at recipe.HSDB2.main(HSDB2.java:30)**
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(HSDB2.java:158)
at recipe.HSDB2.createEntityManager(HSDB2.java:165)
at recipe.HSDB2.main(HSDB2.java:30)
Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
at org.hibernate.boot.spi.XmlMappingBinderAccess.(XmlMappingBinderAccess.java:43)
at org.hibernate.boot.MetadataSources.(MetadataSources.java:87)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.(EntityManagerFactoryBuilderImpl.java:209)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.(EntityManagerFactoryBuilderImpl.java:171)
at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:76)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:179)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:127)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:69)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:52)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at recipe.HSDB2.getEmFactory(HSDB2.java:155)
… 2 more
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)

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.