javadatabasejpacastingopenjpa

Integer To Long error on find() methods JPA


I have a problem with the find() method.

I have a 'DueDiligenceGestori' class that maps a database table. The primary key of the table is a number(19) and therefore I mapped the entity with a Long as previously done

Here is the class

        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;
    }


}

Here is the error at execution

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)

I tried to cast, debug but everything seems to have passed correctly. Does anyone know how to act? Thank you all

[EDIT] stack trace after upgrade OpenJPA

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

Solution

  • It seem that when you're calling the EntityManager.find interface, you're invoking it with the wrong type.

    Somewhere (I'm guessing by the stack in repository.impl.DueDiligenceGestoriRepositoryImpl.getDueDiligenceGestori) you have something like this:

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

    You need to convert that to long first. If it's a constant, then 12L, if it's a primitive, then (long) id, if it's an Integer object, you need a null check, then call id.longValue().