title: Hibernate throws IllegalArgumentException: object is not an instance of declaring class
body: <p>I'm trying to create a small webapp, for once, without the use of spring, but my hibernate configuration appears to have some problems. Reading from the database works flawless, however persisting appears to work only in my unit tests. When I try to persist an object when the application is running on glassfish and using MySQL database (as opposed to HSQLDB which i'm using in unit test), I get the following exception </p>
  
  <pre><code>[2015-02-19T17:06:29.378+0200] [glassfish 4.1] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=31 _ThreadName=http-listener-1(4)] [timeMillis: 1424358389378] [levelValue: 900] [[
    StandardWrapperValve[fi.quickquiz.rest.RESTConfig]: Servlet.service() for servlet fi.quickquiz.rest.RESTConfig threw exception
  java.lang.IllegalArgumentException: object is not an instance of declaring class
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:169)
      at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:346)
      at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4746)
      at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4465)
      at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:243)
      at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:511)
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:116)
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
      at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
      at fi.quickquiz.repositories.DBRepository.persist(DBRepository.java:54)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
      at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
      at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786)
      at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
      at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
      at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
      at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
      at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
      at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
      at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
      at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
      at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
      at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
      at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
      at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758)
      at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746)
      at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
      at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
      at com.sun.proxy.$Proxy245.persist(Unknown Source)
      at fi.quickquiz.repositories.__EJB31_Generated__DBRepository__Intf____Bean__.persist(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.jboss.weld.util.reflection.Reflections.invokeAndUnwrap(Reflections.java:414)
      at org.jboss.weld.bean.proxy.EnterpriseBeanProxyMethodHandler.invoke(EnterpriseBeanProxyMethodHandler.java:127)
      at org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke(EnterpriseTargetBeanInstance.java:56)
      at org.jboss.weld.bean.proxy.InjectionPointPropagatingEnterpriseTargetBeanInstance.invoke(InjectionPointPropagatingEnterpriseTargetBeanInstance.java:65)
      at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:100)
      at fi.quickquiz.repositories.DBRepository$Proxy$_$$_Weld$EnterpriseProxy$.persist(Unknown Source)
      at fi.quickquiz.rest.QuizService.createQuiz(QuizService.java:28)
      at fi.quickquiz.rest.QuizService$Proxy$_$$_WeldClientProxy.createQuiz(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
      at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151)
      at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
      at org.glassfish.jersey.server.model.internal.VoidVoidDispatcherProvider$VoidToVoidDispatcher.doDispatch(VoidVoidDispatcherProvider.java:77)
      at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
      at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:387)
      at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:331)
      at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:103)
      at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:271)
      at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
      at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
      at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
      at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254)
      at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
      at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:372)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
      at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
      at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
      at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
      at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
      at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
      at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
      at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
      at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
      at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>this is my persistence.xml</p>
  
  <pre><code>&lt;persistence&gt;
      &lt;persistence-unit name="quizzes" transaction-type="JTA"&gt;
          &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
          &lt;jta-data-source&gt;jdbc/quickquiz&lt;/jta-data-source&gt;
          &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml"/&gt;
              &lt;property name="hibernate.transaction.jta.platform"
                        value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt;
              &lt;property name="hibernate.current_session_context_class" value="thread"/&gt;
              &lt;property name="hibernate.connection.pool_size" value="1"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>and hibernate.cfg.xml</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
  
      &lt;session-factory&gt;
  
          &lt;mapping class="fi.quickquiz.jpa.Quiz"/&gt;
  
      &lt;/session-factory&gt;
  
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>Quiz:</p>
  
  <pre><code>@Entity
  public class Quiz extends AbstractDomainObject {
  
      private String description;
  
      private String name;
  
      @Column(length = 3000)
      public String getDescription() {
          return description;
      }
  
      @Column(length = 1000)
      public String getName() {
          return name;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  }
  </code></pre>
  
  <p>AbstractDomainObject:
  @MappedSuperclass
  public class AbstractDomainObject implements Serializable {</p>
  
  <pre><code>private Date created;
  
  private Long id;
  
  private Date modified;
  
  @Column
  @Temporal(TemporalType.TIMESTAMP)
  public Date getCreated() {
      return created;
  }
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  public Long getId() {
      return id;
  }
  
  @Column
  @Temporal(TemporalType.TIMESTAMP)
  public Date getModified() {
      return modified;
  }
  
  public void setCreated(Date created) {
      this.created = created;
  }
  
  public void setId(Long id) {
      this.id = id;
  }
  
  public void setModified(Date modified) {
      this.modified = modified;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and finally DBRepository, that is actually responsible for persisting the god damn thing:</p>
  
  <pre><code>@Stateless
  @TransactionAttribute(TransactionAttributeType.REQUIRED)
  @TransactionManagement(TransactionManagementType.CONTAINER)
  public class DBRepository {
  
      @PersistenceContext(unitName = "quizzes")
      private EntityManager entityManager;
  
      public Session getSession() {
          Session session = null;
          Object delegate = entityManager.getDelegate();
          if (delegate instanceof Session) {
              session = (Session) delegate;
          } else if (delegate instanceof EntityManagerImpl) {
              session = ((EntityManagerImpl) delegate).getSession();
          } else {
              throw new IllegalStateException("JPA Entity Manager Delegate type cannot be determined. Delegate ["
                  + delegate + "]");
          }
          return session;
      }
  
      public &lt;T extends AbstractDomainObject&gt; List&lt;T&gt; listAll(Class&lt;T&gt; t) {
          return getSession().createCriteria(t).list();
      }
  
      public &lt;T extends AbstractDomainObject&gt; void persist(T obj) {
          getSession().persist(obj);
      }
  
  }
  </code></pre>