在find()方法JPA上的Integer To Long错误

时间:2017-12-18 14:04:35

标签: java database jpa casting openjpa

我的 find()方法存在问题。

我有一个' DueDiligenceGestori'映射数据库表的类。该表的主键是数字(19),因此我使用Long映射实体,如前所述

这是班级

        package com.xxx.model.duediligencegestori;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import com.xxx.model.AbstractVersionEntity;
import com.xxx.model.duediligence.DueDiligence;
import com.xxx.model.duediligence.DueDiligenceEnumType;
import com.xxx.model.masterData.Person;

/**
 * The persistent class for the DUEDILIGENCE_GESTORI database table.
 * 
 */
@Entity
@Table(name = "DUEDILIGENCE_GESTORI")
public class DueDiligenceGestori extends AbstractVersionEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    public DueDiligenceGestori() {
    }

    @Id
    @SequenceGenerator(name = "DUEDILIGENCE_GEST_ID_SEQ_GENERATOR", sequenceName = "DDG_SEQ", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DUEDILIGENCE_GEST_ID_SEQ_GENERATOR")
    @Column(name = "ID", unique = true, nullable = false)
    private Long id;

    @JoinColumn(name = "DUEDILIGENCE_ID", unique = true)
    @OneToOne(cascade = CascadeType.ALL)
    private DueDiligence dueDiligence;

    @Column(name = "IDENTIFICATIVO_SCHEDA", unique = true, nullable = false, length = 4000)
    private String identificativoScheda;

    @Column(length = 25)
    private String attivita;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CAPO_AREA")
    private Person capoArea;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "FUEL_S3")
    private Person fuelS3;

        public Long getId() {
        return id;
    }

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

        public DueDiligence getDueDiligence() {
        return dueDiligence;
    }

    public void setDueDiligence(DueDiligence dueDiligence) {
        this.dueDiligence = dueDiligence;
    }

        public String getAttivita() {
        return this.attivita;
    }

    public void setAttivita(String attivita) {
        this.attivita = attivita;
    }

        public Person getCapoArea() {
        return capoArea;
    }

    public void setCapoArea(Person capoArea) {
        this.capoArea = capoArea;
    }

    public Person getFuelS3() {
        return fuelS3;
    }

    public void setFuelS3(Person fuelS3) {
        this.fuelS3 = fuelS3;
    }


}

这是执行时的错误

18/12/2017 14:38:59,857 [ERROR] [com.xxx.service.BaseRestService] [] [] Rest Service response FAIL Exception {}

<openjpa-1.1.1-SNAPSHOT-r422266:965591 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: java.lang.Integer cannot be cast to java.lang.Long

       at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:882)

       at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:776)

       at kodo.kernel.KodoBroker.find(KodoBroker.java:232)

       at org.apache.openjpa.kernel.DelegatingBroker.find(DelegatingBroker.java:183)

       at org.apache.openjpa.persistence.EntityManagerImpl.find(EntityManagerImpl.java:452)

       at com.xxx.business.repository.AbstractRepository.find(AbstractRepository.java:254)

       at com.xxx.business.repository.AbstractRepository.find(AbstractRepository.java:250)

       at com.xxx.business.repository.impl.DueDiligenceGestoriRepositoryImpl.getDueDiligenceGestori(DueDiligenceGestoriRepositoryImpl.java:83)

       at com.xxx.business.service.impl.DueDiligenceGestoriServiceImpl.getDueDiligenceGestori(DueDiligenceGestoriServiceImpl.java:74)

       at com.xxx.command.duediligencegestori.SaveDueDiligenceGestoriCmd.doExecute(SaveDueDiligenceGestoriCmd.java:83)

       at com.xxx.command.duediligencegestori.SaveDueDiligenceGestoriCmd.doExecute(SaveDueDiligenceGestoriCmd.java:28)

       at com.xxx.command.AbstractCommand.execute(AbstractCommand.java:88)

       at com.xxx.service.BaseRestService.executeCommand(BaseRestService.java:58)

       at com.xxx.service.DueDiligenceGestoriRestService.saveDueDiligenceGestoriDTO(DueDiligenceGestoriRestService.java:86)

       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

       at java.lang.reflect.Method.invoke(Method.java:597)

       at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)

       at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)

       at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)

       at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)

       at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)

       at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)

       at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)

       at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)

       at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1511)

       at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1442)

       at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)

       at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)

       at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)

       at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)

       at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)

       at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)

       at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)

       at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)

       at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)

       at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)

       at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)

       at com.xxx.filter.EnvironmentsFilter.doHttpFilter(EnvironmentsFilter.java:133)

       at com.xxx.filter.AbstractFilter.doFilter(AbstractFilter.java:43)

       at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)

       at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)

       at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)

       at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)

       at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)

       at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)

       at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)

       at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)

       at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)

       at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)

Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

       at com.xxx.model.duediligencegestori.DueDiligenceGestori.pcReplaceField(DueDiligenceGestori.java)

       at org.apache.openjpa.kernel.StateManagerImpl.replaceField(StateManagerImpl.java:3018)

       at org.apache.openjpa.kernel.StateManagerImpl.storeObjectField(StateManagerImpl.java:2456)

       at org.apache.openjpa.kernel.StateManagerImpl.storeField(StateManagerImpl.java:2552)

       at org.apache.openjpa.kernel.StateManagerImpl.storeField(StateManagerImpl.java:786)

       at org.apache.openjpa.kernel.StateManagerImpl.store(StateManagerImpl.java:782)

       at org.apache.openjpa.jdbc.meta.strats.HandlerFieldStrategy.load(HandlerFieldStrategy.java:175)

       at org.apache.openjpa.jdbc.meta.FieldMapping.load(FieldMapping.java:796)

       at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:869)

       at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:821)

       at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initializeState(JDBCStoreManager.java:343)

       at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initialize(JDBCStoreManager.java:264)

       at org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:111)

       at org.apache.openjpa.datacache.DataCacheStoreManager.initialize(DataCacheStoreManager.java:335)

       at org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:111)

       at org.apache.openjpa.kernel.ROPStoreManager.initialize(ROPStoreManager.java:57)

       at org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:901)

       at kodo.kernel.KodoBroker.initialize(KodoBroker.java:65)

       at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:859)

我试图投射,调试,但一切似乎已经正确通过。有谁知道怎么做?谢谢大家

升级OpenJPA后

[EDIT]堆栈跟踪

18/12/2017 15:39:01,519 [ERROR] [com.xxx.service.BaseRestService] [] [] Rest Service response FAIL Exception {}
<openjpa-1.1.1-SNAPSHOT-r422266:965591 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: java.lang.Integer cannot be cast to java.lang.Long
                at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:882)
                at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:776)
                at kodo.kernel.KodoBroker.find(KodoBroker.java:232)
                at org.apache.openjpa.kernel.DelegatingBroker.find(DelegatingBroker.java:183)
                at org.apache.openjpa.persistence.EntityManagerImpl.find(EntityManagerImpl.java:452)
                at com.xxx.business.repository.AbstractRepository.find(AbstractRepository.java:254)
                at com.xxx.business.repository.AbstractRepository.find(AbstractRepository.java:250)
                at com.xxx.business.repository.impl.DueDiligenceGestoriRepositoryImpl.getDueDiligenceGestori(DueDiligenceGestoriRepositoryImpl.java:83)
                at com.xxx.business.service.impl.DueDiligenceGestoriServiceImpl.getDueDiligenceGestori(DueDiligenceGestoriServiceImpl.java:74)
                at com.xxx.command.duediligencegestori.SaveDueDiligenceGestoriCmd.doExecute(SaveDueDiligenceGestoriCmd.java:83)
                at com.xxx.command.duediligencegestori.SaveDueDiligenceGestoriCmd.doExecute(SaveDueDiligenceGestoriCmd.java:28)
                at com.xxx.command.AbstractCommand.execute(AbstractCommand.java:88)
                at com.xxx.service.BaseRestService.executeCommand(BaseRestService.java:58)
                at com.xxx.service.DueDiligenceGestoriRestService.saveDueDiligenceGestoriDTO(DueDiligenceGestoriRestService.java:86)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:597)
                at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
                at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
                at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
                at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
                at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
                at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
                at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
                at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
                at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1511)
                at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1442)
                at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
                at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
                at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
                at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
                at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
                at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
                at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
                at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
                at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
                at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
                at com.xxx.filter.EnvironmentsFilter.doHttpFilter(EnvironmentsFilter.java:133)
                at com.xxx.filter.AbstractFilter.doFilter(AbstractFilter.java:43)
                at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
                at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
                at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
                at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
                at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
                at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
                at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
                at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
                at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
                at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
                at com.xxx.model.duediligencegestori.DueDiligenceGestori.pcReplaceField(DueDiligenceGestori.java)
                at org.apache.openjpa.kernel.StateManagerImpl.replaceField(StateManagerImpl.java:3018)
                at org.apache.openjpa.kernel.StateManagerImpl.storeObjectField(StateManagerImpl.java:2456)
                at org.apache.openjpa.kernel.StateManagerImpl.storeField(StateManagerImpl.java:2552)
                at org.apache.openjpa.kernel.StateManagerImpl.storeField(StateManagerImpl.java:786)
                at org.apache.openjpa.kernel.StateManagerImpl.store(StateManagerImpl.java:782)
                at org.apache.openjpa.jdbc.meta.strats.HandlerFieldStrategy.load(HandlerFieldStrategy.java:175)
                at org.apache.openjpa.jdbc.meta.FieldMapping.load(FieldMapping.java:796)
                at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:869)
                at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:821)
                at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initializeState(JDBCStoreManager.java:343)
                at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initialize(JDBCStoreManager.java:264)
                at org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:111)
                at org.apache.openjpa.datacache.DataCacheStoreManager.initialize(DataCacheStoreManager.java:335)
                at org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:111)
                at org.apache.openjpa.kernel.ROPStoreManager.initialize(ROPStoreManager.java:57)
                at org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:901)
                at kodo.kernel.KodoBroker.initialize(KodoBroker.java:65)
                at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:859)
                ... 50 more

1 个答案:

答案 0 :(得分:0)

当您调用EntitiManager.find界面时,您似乎正在使用错误的类型调用它。

某处(我在repository.impl.DueDiligenceGestoriRepositoryImpl.getDueDiligenceGestori中通过堆栈猜测)你有这样的事情:

em.find(DueDiligenceGestori.class, 12);
// or
int id; // parameter
em.find(DueDiligenceGestori.class, id);

您需要先将其转换为long。如果它是常量,那么12L,如果它是原始的,那么(long) id,如果它是一个Integer对象,则需要进行空检查,然后调用{{ 1}}。