java.lang.IllegalArgumentException:托管类型[EntityTypeImpl @ 530144602:Cities]中不存在属性[state_id]

时间:2016-09-28 08:16:13

标签: jpa identity illegalargumentexception

我是javaEE的新手。最近我使用jax-rs和使用JPA处理数据库并使用Netbeans作为IDE和TomeEE作为Web服务器来处理restful Web Services。

关于我的问题, 我有一个名为city的表,其中包含3列:

id,state_id,name

(state_id被定义为另一列的外键被称为状态)

我想选择所有具有某个state_id的城市,例如在表格中,存在1000行,其state_id为8。

这是我的JPA代码:

    public List<T> findByStateID(Short id){
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(entityClass);
        javax.persistence.criteria.Root<T> r = cq.from(entityClass);
        cq.select(r);
        cq.where(getEntityManager().getCriteriaBuilder().equal(r.get("state_id"), id));
        return getEntityManager().createQuery(cq).getResultList();
    }

这是Web服务代码:

    @GET
    @Path("State/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Cities> findByState(@PathParam("id") Short id){
        return super.findByStateID(id);
    }

知道我的问题是,当我访问目标网址时,我会收到打击错误:

HTTP Status 500 - Error processing webservice request

type Exception report

message Error processing webservice request

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Error processing webservice request
    org.apache.tomee.webservices.CXFJAXRSFilter.doFilter(CXFJAXRSFilter.java:98)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.apache.openejb.server.httpd.EEFilter.doFilter(EEFilter.java:65)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

root cause

java.lang.IllegalArgumentException: The attribute [state_id] is not present in the managed type [EntityTypeImpl@530144602:Cities [ javaType: class entity.Cities descriptor: RelationalDescriptor(entity.Cities --> [DatabaseTable(cities)]), mappings: 4]].
    org.eclipse.persistence.internal.jpa.metamodel.ManagedTypeImpl.getAttribute(ManagedTypeImpl.java:148)
    org.eclipse.persistence.internal.jpa.querydef.FromImpl.get(FromImpl.java:312)
    rest.AbstractFacade.findByStateID(AbstractFacade.java:50)
    rest.CitiesFacadeREST.findByState(CitiesFacadeREST.java:49)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205)
    org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186)
    org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:181)
    org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:100)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205)
    org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186)
    org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:85)
    org.apache.openejb.core.stateless.StatelessContainer._invoke(StatelessContainer.java:236)
    org.apache.openejb.core.stateless.StatelessContainer.invoke(StatelessContainer.java:203)
    org.apache.openejb.util.proxy.ProxyEJB$Handler.invoke(ProxyEJB.java:74)
    rest.CitiesFacadeREST$$LocalBeanProxy.findByState(rest/CitiesFacadeREST.java)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.apache.openejb.server.cxf.rs.OpenEJBEJBInvoker.performInvocation(OpenEJBEJBInvoker.java:95)
    org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
    org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:189)
    org.apache.openejb.server.cxf.rs.OpenEJBEJBInvoker.invoke(OpenEJBEJBInvoker.java:68)
    org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:99)
    org.apache.openejb.server.cxf.rs.AutoJAXRSInvoker.invoke(AutoJAXRSInvoker.java:64)
    org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59)
    org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96)
    org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
    org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:254)
    org.apache.openejb.server.cxf.rs.CxfRsHttpListener.doInvoke(CxfRsHttpListener.java:251)
    org.apache.tomee.webservices.CXFJAXRSFilter.doFilter(CXFJAXRSFilter.java:94)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.apache.openejb.server.httpd.EEFilter.doFilter(EEFilter.java:65)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

note The full stack trace of the root cause is available in the Apache Tomcat (TomEE)/8.5.3 (7.0.1) logs.
Apache Tomcat (TomEE)/8.5.3 (7.0.1)

这是Cities实体类:

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author seyed
 */
@Entity
@Table(name = "cities")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Cities.findAll", query = "SELECT c FROM Cities c"),
    @NamedQuery(name = "Cities.findById", query = "SELECT c FROM Cities c WHERE c.id = :id"),
    @NamedQuery(name = "Cities.findByName", query = "SELECT c FROM Cities c WHERE c.name = :name")})
public class Cities implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Short id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 65)
    @Column(name = "name")
    private String name;
    @JoinColumn(name = "state_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private States stateId;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "city")
    private Collection<Users> usersCollection;

    public Cities() {
    }

    public Cities(Short id) {
        this.id = id;
    }

    public Cities(Short id, String name) {
        this.id = id;
        this.name = name;
    }

    public Short getId() {
        return id;
    }

    public void setId(Short id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public States getStateId() {
        return stateId;
    }

    public void setStateId(States stateId) {
        this.stateId = stateId;
    }

    @XmlTransient
    public Collection<Users> getUsersCollection() {
        return usersCollection;
    }

    public void setUsersCollection(Collection<Users> usersCollection) {
        this.usersCollection = usersCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Cities)) {
            return false;
        }
        Cities other = (Cities) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Cities[ id=" + id + " ]";
    }

}

如评论中所述,我在jpa部分将state_id更改为stateId,现在我收到此错误:

HTTP Status 500 - Error processing webservice request

type Exception report

message Error processing webservice request

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Error processing webservice request
    org.apache.tomee.webservices.CXFJAXRSFilter.doFilter(CXFJAXRSFilter.java:98)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.apache.openejb.server.httpd.EEFilter.doFilter(EEFilter.java:65)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

root cause

javax.persistence.PersistenceException: Exception [EclipseLink-6078] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.QueryException
Exception Description: The class of the argument for the object comparison is incorrect. 
Expression: [
Base entity.Cities] 
Mapping: [org.eclipse.persistence.mappings.ManyToOneMapping[stateId]] 
Argument: [1]
Query: ReadAllQuery(referenceClass=Cities )
    org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:484)
    rest.AbstractFacade.findByStateID(AbstractFacade.java:51)
    rest.CitiesFacadeREST.findByState(CitiesFacadeREST.java:49)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205)
    org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186)
    org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:181)
    org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:100)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205)
    org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186)
    org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:85)
    org.apache.openejb.core.stateless.StatelessContainer._invoke(StatelessContainer.java:236)
    org.apache.openejb.core.stateless.StatelessContainer.invoke(StatelessContainer.java:203)
    org.apache.openejb.util.proxy.ProxyEJB$Handler.invoke(ProxyEJB.java:74)
    rest.CitiesFacadeREST$$LocalBeanProxy.findByState(rest/CitiesFacadeREST.java)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.apache.openejb.server.cxf.rs.OpenEJBEJBInvoker.performInvocation(OpenEJBEJBInvoker.java:95)
    org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
    org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:189)
    org.apache.openejb.server.cxf.rs.OpenEJBEJBInvoker.invoke(OpenEJBEJBInvoker.java:68)
    org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:99)
    org.apache.openejb.server.cxf.rs.AutoJAXRSInvoker.invoke(AutoJAXRSInvoker.java:64)
    org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59)
    org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96)
    org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
    org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:254)
    org.apache.openejb.server.cxf.rs.CxfRsHttpListener.doInvoke(CxfRsHttpListener.java:251)
    org.apache.tomee.webservices.CXFJAXRSFilter.doFilter(CXFJAXRSFilter.java:94)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.apache.openejb.server.httpd.EEFilter.doFilter(EEFilter.java:65)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

root cause

Exception [EclipseLink-6078] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.QueryException
Exception Description: The class of the argument for the object comparison is incorrect. 
Expression: [
Base entity.Cities] 
Mapping: [org.eclipse.persistence.mappings.ManyToOneMapping[stateId]] 
Argument: [1]
Query: ReadAllQuery(referenceClass=Cities )
    org.eclipse.persistence.exceptions.QueryException.incorrectClassForObjectComparison(QueryException.java:601)
    org.eclipse.persistence.mappings.OneToOneMapping.buildObjectJoinExpression(OneToOneMapping.java:298)
    org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:830)
    org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:1474)
    org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildNormalSelectStatement(ExpressionQueryMechanism.java:550)
    org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareSelectAllRows(ExpressionQueryMechanism.java:1722)
    org.eclipse.persistence.queries.ReadAllQuery.prepareSelectAllRows(ReadAllQuery.java:885)
    org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:816)
    org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:666)
    org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:911)
    org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:615)
    org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:872)
    org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1134)
    org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:460)
    org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1222)
    org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
    org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1857)
    org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1839)
    org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1804)
    org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258)
    org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:473)
    rest.AbstractFacade.findByStateID(AbstractFacade.java:51)
    rest.CitiesFacadeREST.findByState(CitiesFacadeREST.java:49)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205)
    org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186)
    org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:181)
    org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:100)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205)
    org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186)
    org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:85)
    org.apache.openejb.core.stateless.StatelessContainer._invoke(StatelessContainer.java:236)
    org.apache.openejb.core.stateless.StatelessContainer.invoke(StatelessContainer.java:203)
    org.apache.openejb.util.proxy.ProxyEJB$Handler.invoke(ProxyEJB.java:74)
    rest.CitiesFacadeREST$$LocalBeanProxy.findByState(rest/CitiesFacadeREST.java)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.apache.openejb.server.cxf.rs.OpenEJBEJBInvoker.performInvocation(OpenEJBEJBInvoker.java:95)
    org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
    org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:189)
    org.apache.openejb.server.cxf.rs.OpenEJBEJBInvoker.invoke(OpenEJBEJBInvoker.java:68)
    org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:99)
    org.apache.openejb.server.cxf.rs.AutoJAXRSInvoker.invoke(AutoJAXRSInvoker.java:64)
    org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59)
    org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96)
    org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
    org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:254)
    org.apache.openejb.server.cxf.rs.CxfRsHttpListener.doInvoke(CxfRsHttpListener.java:251)
    org.apache.tomee.webservices.CXFJAXRSFilter.doFilter(CXFJAXRSFilter.java:94)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.apache.openejb.server.httpd.EEFilter.doFilter(EEFilter.java:65)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

note The full stack trace of the root cause is available in the Apache Tomcat (TomEE)/8.5.3 (7.0.1) logs.
Apache Tomcat (TomEE)/8.5.3 (7.0.1)

2 个答案:

答案 0 :(得分:0)

查询默认使用java名称。如果您有疑问,可以设置一个jpa元模型生成器,您将能够使用类型的类型而不是字符串。

答案 1 :(得分:0)

因为jpa / hibernate使用的是属性名而不是列名,所以必须使用private States stateId而不是private States state_Id

因此您必须将equal(r.get("state_id"), id))更改为equal(r.get("stateId"), id)); `