java.lang.IllegalArgumentException:无法设置java.lang.String字段错误

时间:2013-06-20 08:53:50

标签: web-applications jstl dao illegalargumentexception

当我尝试将特定实体加载到JSP表单时,我收到以下错误消息。当我尝试加载类型为“USER”的实体时会发生这种情况。所以这是错误信息:

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.<init>(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)

根据此错误消息,问题出在at com.mysite.bookingmeeting.dao.implementation.UserProfileDaoImpl.findAllUsers(UserProfileDaoImpl.java:47)at org.apache.jsp.admin.ManageUsers_jsp._jspService(ManageUsers_jsp.java:133)。我已经检查了那些特定页面中的代码,并且它们之前工作正常,但由于某些原因现在它不起作用。

UserProfileDaoImpl()类的方法 findAllUsers()的代码如下:

@Override
    public Collection<UserProfile> findAllUsers() {
        EntityManager em = getEntityManager();
        Query q = em.createQuery("SELECT u FROM UserProfile u WHERE u.userType = 'USER'");
        List<UserProfile> results;
        results = q.getResultList();
        return results;
    }

ManageUsers.jsp 的代码如下:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Manage Users</title>
    </head>
    <body>
        <h2>Create User</h2>
        <%@include file="../WEB-INF/jspf/createUser.jspf" %>
        <h2>Delete Users</h2>
        <%@include file="../WEB-INF/jspf/DeleteUserForm.jspf" %>
    </body>
</html>

我已经在没有DeleteUserForm.jspf的情况下对上面的文件进行了测试,它运行良好没有任何问题,因此主要原因是 DeleteUserForm.jsp ,此处此代码的代码如下:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="java.util.Collection"%>
<%@page import="com.mysite.bookingmeeting.user.UserProfile"%>
<%@page import="com.mysite.bookingmeeting.dao.implementation.UserProfileDaoImpl"%>
<%@page import="com.mysite.bookingmeeting.dao.UserProfileDao"%>
<%
    UserProfileDao userProfiles = new UserProfileDaoImpl();
    Collection<UserProfile> allUsers = userProfiles.findAllUsers();

    session.setAttribute("users", allUsers);
%>
<%@ page pageEncoding="UTF-8" %>
<form name="deleteUser" action="DeleteUserServlet">
    <table border="0">
        <tbody>
            <tr>
                <td>User Name: </td>
                <td><select name="deleteUsers" multiple="multiple">
                        <c:forEach var="users" items="${users}">
                            <option value='<c:out value="${users.getId()}"/>'>
                                <c:out value="${users.firstName}"/> 
                                <c:out value="${users.lastName}"/>
                            </option>
                        </c:forEach>
                    </select>
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Delete Users" /></td>
            </tr>
        </tbody>
    </table>
</form>

此代码之前也有效,但现在它无效。任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:-1)

我解决了这个问题,我发现我尝试将String类型变量设置为UserProfile类型变量。