title: java.lang.IllegalArgumentException: Can not set java.lang.String field error
body: <p>I get following error message when I try to load specific entities to my JSP form. This occurs when I try to load entities which types are "USER". So here's the error message:</p>
  
  <pre><code>WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
  java.lang.IllegalArgumentException: Can not set java.lang.String field com.mysite.bookingmeeting.user.UserProfile.userName to java.lang.Long
      at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
      at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
      at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55)
      at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
      at java.lang.reflect.Field.get(Field.java:372)
      at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:32)
      at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValue(AbstractComponentTuplizer.java:64)
      at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValues(AbstractComponentTuplizer.java:70)
      at org.hibernate.tuple.component.PojoComponentTuplizer.getPropertyValues(PojoComponentTuplizer.java:86)
      at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:353)
      at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:184)
      at org.hibernate.engine.EntityUniqueKey.generateHashCode(EntityUniqueKey.java:63)
      at org.hibernate.engine.EntityUniqueKey.&lt;init&gt;(EntityUniqueKey.java:44)
      at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:596)
      at org.hibernate.type.EntityType.resolve(EntityType.java:382)
      at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
      at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
      at org.hibernate.loader.Loader.doQuery(Loader.java:729)
      at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
      at org.hibernate.loader.Loader.doList(Loader.java:2220)
      at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
      at org.hibernate.loader.Loader.list(Loader.java:2099)
      at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
      at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
      at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
      at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
      at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
      at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:65)
      at com.mysite.bookingmeeting.dao.implementation.UserProfileDaoImpl.findAllUsers(UserProfileDaoImpl.java:47)
      at org.apache.jsp.admin.ManageUsers_jsp._jspService(ManageUsers_jsp.java:133)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:328)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
      at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
      at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
      at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
      at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
      at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
      at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
      at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
      at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
      at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
      at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
      at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>According to this error message the problem lies in <code>at com.mysite.bookingmeeting.dao.implementation.UserProfileDaoImpl.findAllUsers(UserProfileDaoImpl.java:47)</code> and <code>at org.apache.jsp.admin.ManageUsers_jsp._jspService(ManageUsers_jsp.java:133)</code>. I have checked those codes in those specific pages and they worked before fine but for some reasons now it won't work. </p>
  
  <p>The code for method <strong>findAllUsers()</strong> of <strong>UserProfileDaoImpl()</strong> class is following:</p>
  
  <pre><code>@Override
      public Collection&lt;UserProfile&gt; findAllUsers() {
          EntityManager em = getEntityManager();
          Query q = em.createQuery("SELECT u FROM UserProfile u WHERE u.userType = 'USER'");
          List&lt;UserProfile&gt; results;
          results = q.getResultList();
          return results;
      }
  </code></pre>
  
  <p>And code for <strong>ManageUsers.jsp</strong> is following:</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;title&gt;Manage Users&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h2&gt;Create User&lt;/h2&gt;
          &lt;%@include file="../WEB-INF/jspf/createUser.jspf" %&gt;
          &lt;h2&gt;Delete Users&lt;/h2&gt;
          &lt;%@include file="../WEB-INF/jspf/DeleteUserForm.jspf" %&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I have tested above file without DeleteUserForm.jspf and it works well with no problems, so the main cause is <strong>DeleteUserForm.jsp</strong> and here the code of this form is following:</p>
  
  <pre><code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
  &lt;%@page import="java.util.Collection"%&gt;
  &lt;%@page import="com.mysite.bookingmeeting.user.UserProfile"%&gt;
  &lt;%@page import="com.mysite.bookingmeeting.dao.implementation.UserProfileDaoImpl"%&gt;
  &lt;%@page import="com.mysite.bookingmeeting.dao.UserProfileDao"%&gt;
  &lt;%
      UserProfileDao userProfiles = new UserProfileDaoImpl();
      Collection&lt;UserProfile&gt; allUsers = userProfiles.findAllUsers();
  
      session.setAttribute("users", allUsers);
  %&gt;
  &lt;%@ page pageEncoding="UTF-8" %&gt;
  &lt;form name="deleteUser" action="DeleteUserServlet"&gt;
      &lt;table border="0"&gt;
          &lt;tbody&gt;
              &lt;tr&gt;
                  &lt;td&gt;User Name: &lt;/td&gt;
                  &lt;td&gt;&lt;select name="deleteUsers" multiple="multiple"&gt;
                          &lt;c:forEach var="users" items="${users}"&gt;
                              &lt;option value='&lt;c:out value="${users.getId()}"/&gt;'&gt;
                                  &lt;c:out value="${users.firstName}"/&gt; 
                                  &lt;c:out value="${users.lastName}"/&gt;
                              &lt;/option&gt;
                          &lt;/c:forEach&gt;
                      &lt;/select&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input type="submit" value="Delete Users" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/tbody&gt;
      &lt;/table&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>This code also worked before but now its not working. Any help is really appreciated.</p>