title: Class loading error with Spring Boot and Hibernate 5
body: <p>I recently upgraded a Spring Boot based application from Hibernate 4 to Hibernate 5. Since then I observe a class loading problem. Obviously, the hibernate classes and my domain class get loaded by two different class loaders. This only happens if I launch the application with Spring DevTools and Hibernate 5. The combinations DevTools/Hibernate 4, mvn spring-boot:run/Hibernate 5 work.</p>
  
  <p>The problem can be reproduced with the following simple spring boot app (the full eclipse project is available <a href="http://1drv.ms/1KRVYUQ" rel="nofollow">here</a>)</p>
  
  <pre><code>@Entity
  public class Employee implements Serializable {
      private static final long serialVersionUID = 1L;
  
      private Long     id;
      private String   firstName;
      private String   lastName;
  
      public Employee() {  
      }
  
      public Employee(String firstName, String lastName) {
          this.firstName = firstName;
          this.lastName = lastName;
      }
  
      @Id @GeneratedValue
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getFirstName() {
          return firstName;
      }
  
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
      public String getLastName() {
          return lastName;
      }
  
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
  
      public String toString() {
          return id + ": " + lastName + ", " + firstName;
      }
  }
  
  public class AppConfig {
  
      @Autowired
      private DataSource dataSource;
  
      @SuppressWarnings("serial")
      @Bean
      public LocalSessionFactoryBean sessionFactory() {
  
          LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
          sessionFactory.setDataSource(dataSource);
          sessionFactory.setPackagesToScan("problem.domain");
          sessionFactory.setHibernateProperties(new Properties() {
              {
                  setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyTenSevenDialect");
                  setProperty("hibernate.hbm2ddl.auto", "create-drop");
                  setProperty("hibernate.current_session_context_class", "thread");
              }
          });
  
          return sessionFactory;
      }
  }
  
  @Component
  public class DatabaseInitializer implements ApplicationRunner {
  
      @Autowired
      private SessionFactory sessionFactory;
  
      @Override
      public void run(ApplicationArguments args) throws Exception {
  
          Session session = sessionFactory.getCurrentSession();
          Transaction tx = session.beginTransaction();
          Employee empl = new Employee("John", "Doe");
          session.persist(empl);
          tx.commit();
      }
  }
  
  @SpringBootApplication
  public class SpringBootMain {
  
      public static void main(String[] args) throws Exception {
          SpringApplication.run(SpringBootMain.class, args);
      }
  }
  
  pom.xml:
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;swt6.spring&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate5-problem&lt;/artifactId&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
      &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.3.2.RELEASE&lt;/version&gt;
          &lt;relativePath /&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
          &lt;hibernate.version&gt;5.1.0.Final&lt;/hibernate.version&gt;
          &lt;derby.version&gt;10.12.1.1&lt;/derby.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.derby&lt;/groupId&gt;
              &lt;artifactId&gt;derby&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>Running this program with Spring DevTools results in the following error:</p>
  
  <pre><code>2016-02-15 18:30:48.315  INFO 13828 --- [  restartedMain] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@55ad1b60'
  2016-02-15 18:30:48.509  INFO 13828 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
  2016-02-15 18:30:48.536  INFO 13828 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
  2016-02-15 18:30:48.582 ERROR 13828 --- [  restartedMain] o.h.p.access.spi.GetterMethodImpl        : HHH000122: IllegalArgumentException in class: problem.domain.Employee, getter method of property: id
  2016-02-15 18:30:48.583 ERROR 13828 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed
  
  java.lang.IllegalStateException: Failed to execute ApplicationRunner
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
      at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:787) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
      at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:777) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
      at problem.main.SpringBootMain.main(SpringBootMain.java:10) [classes/:na]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_66]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_66]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_66]
      at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_66]
      at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.3.2.RELEASE.jar:1.3.2.RELEASE]
  Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of problem.domain.Employee.id
      at org.hibernate.property.access.spi.GetterMethodImpl.get(GetterMethodImpl.java:64) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
      at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:223) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
      at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4633) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
      at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4344) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
      at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:226) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
      at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:499) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:99) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
      at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:778) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:751) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:756) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_66]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_66]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_66]
      at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_66]
      at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
      at com.sun.proxy.$Proxy56.persist(Unknown Source) ~[na:na]
      at problem.main.DatabaseInitializer.run(DatabaseInitializer.java:24) ~[classes/:na]
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
      ... 11 common frames omitted
  Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_66]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_66]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_66]
      at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_66]
      at org.hibernate.property.access.spi.GetterMethodImpl.get(GetterMethodImpl.java:41) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
      ... 29 common frames omitted
  
  2016-02-15 18:30:48.585  INFO 13828 --- [  restartedMain] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/D:/P20058/Documents/FH/Lehre/SWT6U/Uebungen/SpringWeb/hibernate5-problem/target/classes/]
  2016-02-15 18:30:48.585  INFO 13828 --- [  restartedMain] utoConfigurationReportLoggingInitializer : 
  
  Error starting ApplicationContext. To display the auto-configuration report enable debug logging (start with --debug)
  
  
  2016-02-15 18:30:48.585  INFO 13828 --- [  restartedMain] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@63e38bca: startup date [Mon Feb 15 18:30:46 CET 2016]; root of context hierarchy
  2016-02-15 18:30:48.587  INFO 13828 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
  2016-02-15 18:30:48.587  INFO 13828 --- [  restartedMain] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed drop of schema as part of SessionFactory shut-down'
  2016-02-15 18:30:48.604  INFO 13828 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
  2016-02-15 18:30:48.604  INFO 13828 --- [  restartedMain] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed drop of schema as part of SessionFactory shut-down'
  </code></pre>