简体   繁体   中英

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in hibernate

I am getting the below exception after migrating hibernate version from 3.x to 5.4.24 ga in my application.

Using db2 database.

Problem is with criteria.list(); -> Tring to cast coSubscrbnSvcStaHstSid value from BIGINT into LONG and throwing the exception.

Based on the column type, sql typdescriptor is selecting and doing the casting to LONG type in hibernate 5 code.

  • DB Column type is BIGINT and Hibernate entity column type is long.

How can I avoid this error?. Please help.

Exception is..

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
    at org.hibernate.type.descriptor.java.LongTypeDescriptor.unwrap(LongTypeDescriptor.java:19)
    at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$1.doBind(BigIntTypeDescriptor.java:46)
    at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:73)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:276)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:271)
    at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:2228)
    at org.hibernate.loader.Loader.bindParameterValues(Loader.java:2197)
    at org.hibernate.loader.Loader.bindPreparedStatement(Loader.java:2132)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2109)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2041)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2019)
    at org.hibernate.loader.Loader.doQuery(Loader.java:948)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
    at org.hibernate.loader.Loader.doList(Loader.java:2850)
    at org.hibernate.loader.Loader.doList(Loader.java:2832)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2664)
    at org.hibernate.loader.Loader.list(Loader.java:2659)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1877)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:370)
    at com.visa.apps.vsmng.dao.ServiceSubscrnStatusHistoryDao.selectServiceStatus(ServiceSubscrnStatusHistoryDao.java:62)

My code

@SuppressWarnings("unchecked")
public List<ServiceSubscrnStatusSummary> selectServiceStatus(long coSid, String svcStatus) {
    List<String> svcStaCdsList = new ArrayList<String>();
    if (svcStatus != null && !svcStatus.trim().equals("")) {
        String[] statusCds = svcStatus.split(",");
        for (String statusCd : statusCds) {
            svcStaCdsList.add(statusCd);
        }
    }
    Criteria criteria = createCriteria("hist");
    criteria.createCriteria("companySubscrnSvc").createAlias("service", "svc").createCriteria("companySubscrn").add(Restrictions.eq("coSid", coSid));
    criteria.createAlias("serviceSubscrnStatus", "sss").add(Restrictions.not(Restrictions.in("sss.svcSubscrnStaCd", svcStaCdsList)));
    
    ProjectionList proList = Projections.projectionList();
    proList.add(Projections.property("coSubscrbnSvcStaHstSid"), "coSubscrbnSvcStaHstSid");
    proList.add(Projections.property("svc.svcCd"), "serviceCd");
    proList.add(Projections.property("svc.svcNm"), "serviceName");
    proList.add(Projections.property("sss.svcSubscrnStaCd"), "svcSubsStaCd");
    proList.add(Projections.property("sss.svcSubscrnStaDesc"), "svcSubsStaName");
    proList.add(Projections.property("svcSubscrnStaTs"), "updTs");
    
    criteria.setProjection(proList);
    
    // Only display first entry, ignore duplicates
    DetachedCriteria firstEntryQuery = DetachedCriteria.forClass(ServiceSubscrnStatusHistory.class, "fd");
    firstEntryQuery.add(Restrictions.eqProperty("fd.companySubscrnSvc", "hist.companySubscrnSvc"));
    firstEntryQuery.add(Restrictions.eqProperty("fd.serviceSubscrnStatus", "hist.serviceSubscrnStatus"));
    firstEntryQuery.add(Restrictions.leProperty("fd.svcSubscrnStaTs", "hist.svcSubscrnStaTs"));
    ProjectionList proj = Projections.projectionList();
    proj.add(Projections.rowCount());    
    firstEntryQuery.setProjection(proj);
    criteria.add(Subqueries.gt(2, firstEntryQuery));
    criteria.addOrder(Order.desc("svcSubscrnStaTs"));
    List<Object[]> list = criteria.list();  -> Here is the issue

    List<ServiceSubscrnStatusSummary> finalList = new ArrayList<ServiceSubscrnStatusSummary>();
    list.forEach(objectArray -> {
        BigInteger sid = (BigInteger) objectArray[0];
        ServiceSubscrnStatusSummary serviceSubscrnStatusSummary = new ServiceSubscrnStatusSummary();
        serviceSubscrnStatusSummary.setCoSubscrbnSvcStaHstSid(sid.longValue());
        serviceSubscrnStatusSummary.setServiceCd((String) objectArray[1]);
        serviceSubscrnStatusSummary.setServiceName((String) objectArray[2]);
        serviceSubscrnStatusSummary.setSvcSubsStaCd((String) objectArray[3]);
        serviceSubscrnStatusSummary.setSvcSubsStaName((String) objectArray[4]);
        serviceSubscrnStatusSummary.setUpdTs((Timestamp) objectArray[5]);
        finalList.add(serviceSubscrnStatusSummary);
    });
    return finalList;
}

History entity

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="CO_SUBSCRBN_SVC_STA_HST_SID")
private long coSubscrbnSvcStaHstSid;

@Column(name="CRT_TS")
private Timestamp crtTs;

@Column(name="CRT_USER_ID")
private String crtUserId;

@Column(name="REMRKS_TXT")
private String remrksTxt;

@Column(name="SVC_SUBSCRN_STA_TS")
private Timestamp svcSubscrnStaTs;

@Column(name="UPD_TS")
private Timestamp updTs;

@Column(name="UPD_USER_ID")
private String updUserId;

//bi-directional many-to-one association to SubscriptionService
@ManyToOne
@JoinColumn(name="CO_SUBSCRBN_SVC_SID")
private SubscriptionService companySubscrnSvc;

//bi-directional many-to-one association to ServiceSubscrnStatus
@ManyToOne
@JoinColumn(name="SVC_SUBSCRBN_STA_SID")
private ServiceSubscrnStatus serviceSubscrnStatus;

public ServiceSubscrnStatusHistory() {
}

public long getCoSubscrbnSvcStaHstSid() {
    return this.coSubscrbnSvcStaHstSid;
}

public void setCoSubscrbnSvcStaHstSid(long coSubscrbnSvcStaHstSid) {
    this.coSubscrbnSvcStaHstSid = coSubscrbnSvcStaHstSid;
}

public Timestamp getCrtTs() {
    return this.crtTs;
}

public void setCrtTs(Timestamp crtTs) {
    this.crtTs = crtTs;
}

public String getCrtUserId() {
    return this.crtUserId;
}

public void setCrtUserId(String crtUserId) {
    this.crtUserId = crtUserId;
}

public String getRemrksTxt() {
    return this.remrksTxt;
}

public void setRemrksTxt(String remrksTxt) {
    this.remrksTxt = remrksTxt;
}

public Timestamp getSvcSubscrnStaTs() {
    return this.svcSubscrnStaTs;
}

public void setSvcSubscrnStaTs(Timestamp svcSubscrnStaTs) {
    this.svcSubscrnStaTs = svcSubscrnStaTs;
}

public Timestamp getUpdTs() {
    return this.updTs;
}

public void setUpdTs(Timestamp updTs) {
    this.updTs = updTs;
}

public String getUpdUserId() {
    return this.updUserId;
}

public void setUpdUserId(String updUserId) {
    this.updUserId = updUserId;
}

public SubscriptionService getCompanySubscrnSvc() {
    return this.companySubscrnSvc;
}

public void setCompanySubscrnSvc(SubscriptionService companySubscrnSvc) {
    this.companySubscrnSvc = companySubscrnSvc;
}
public ServiceSubscrnStatus getServiceSubscrnStatus() {
    return serviceSubscrnStatus;
}

public void setServiceSubscrnStatus(ServiceSubscrnStatus serviceSubscrnStatus) {
    this.serviceSubscrnStatus = serviceSubscrnStatus;
}

}

for your reference I am attaching the hibernate code here..

@SuppressWarnings({ "unchecked" })
@Override
public <X> X unwrap(Long value, Class<X> type, WrapperOptions options) {
    if ( value == null ) {
        return null;
    }
    if ( Long.class.isAssignableFrom( type ) ) {
        return (X) value;  -> Here is converting from BIGINT to LONG, and breaking.
    }

How can I avoid this error?

You need to change the value of the field in java from Long to BigInteger . You cannot do an easy conversion, because of the reasons outline here .

Does this solve your problem? Let me know in the comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM