Using Spring and Hibernate With JPA And Entity Interfaces

I’m currently setting up a new Hibernate project and finally getting around to trying out JPA annotations instead of using any Hibernate configuration or mapping files– what the hell took me so long!!??! Anyhow, among the many gotchas I came across, one was how to deal with using Interfaces with Hibernate entity beans and associations.

When you declare an association to a Hibernate Entity with an Interface without using the correct annotation syntax to point Hibernate at the implementing class, you will likely see exceptions like this while running JUnit 4 tests in Eclipse:

  1. java.lang.IllegalStateException: Failed to load ApplicationContext
  2.     at org.springframework.test.context.TestContext.getApplicationContext(
  3.     at
  4.     at
  5.     at org.springframework.test.context.TestContextManager.prepareTestInstance(
  6.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(
  7.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(
  8.     at org.junit.internal.runners.JUnit4ClassRunner.runMethods(
  9.     at org.junit.internal.runners.JUnit4ClassRunner$
  10.     at org.junit.internal.runners.ClassRoadie.runUnprotected(
  11.     at org.junit.internal.runners.ClassRoadie.runProtected(
  12.     at
  13.     at
  14.     at org.junit.internal.runners.CompositeRunner.runChildren(
  15.     at org.junit.runners.Suite.access$000(
  16.     at org.junit.runners.Suite$
  17.     at org.junit.internal.runners.ClassRoadie.runUnprotected(
  18.     at org.junit.internal.runners.ClassRoadie.runProtected(
  19.     at
  20.     at
  21.     at
  22.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
  23.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
  24.     at
  25.     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
  26. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘geoSessionFactory’ defined in class path resource [com/peerdigital/spring/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.peerdigital.geolocation.model.DefaultCountryEntityImpl.regionEntity references an unknown entity: com.peerdigital.geolocation.model.DefaultRegionEntityImpl
  27.     at
  28.     at
  29.     at$
  30.     at Method)
  31.     at
  32.     at$1.getObject(
  33.     at
  34.     at
  35.     at
  36.     at
  37.     at
  38.     at
  39.     at
  40.     at
  41.     at
  42.     at org.springframework.test.context.TestContext.loadApplicationContext(
  43.     at org.springframework.test.context.TestContext.getApplicationContext(
  44.     … 23 more
  45. Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.peerdigital.geolocation.model.DefaultCountryEntityImpl.regionEntity references an unknown entity: com.peerdigital.geolocation.model.DefaultRegionEntityImpl
  46.     at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(
  47.     at org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(
  48.     at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(
  49.     at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(
  50.     at org.hibernate.cfg.Configuration.buildMappings(
  51.     at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(
  52.     at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(
  53.     at
  54.     at
  55.     … 39 more

Most of the solutions I saw made you create a duplicate persistence.xml that had all of the same information in it which seemed silly to me. Eventually with the help of Google I came across the answer somewhere (which I no longer have around) which was to modify the field annotation to point at the class which implements the Interface:

  1. @ManyToOne(targetEntity = DefaultRegionEntityImpl.class)
  2. @JoinColumn(name = “region_code_fk”, referencedColumnName = “region_code_pk”, insertable = false, updatable = false)
  3. private RegionEntity regionEntity;

One other thing to check– make sure your @Entity annotation is importing the JPA annotation (javax.persistence.Entity), and NOT the Hibernate annotation (org.hibernate.annotations.Entity). If you import the Hibernate @Entity annotation it will not find the entity bean.

Hopefully that’s of some help to someone. Just a quick post– back to work I go!

This entry was posted in Frameworks, Languages and tagged , , , , , . Bookmark the permalink.

2 Responses to "Using Spring and Hibernate With JPA And Entity Interfaces"

Leave a reply