簡體   English   中英

Srping/Hibernate:spring.entity.Downloads 不能轉換為 java.lang.Integer

[英]Srping/ Hibernate: spring.entity.Downloads cannot be cast to java.lang.Integer

我最近開始嘗試學習java並完成了一個spring/hibernate課程,目的是在我的業余時間解決工作中的一些問題。 不用說,我遇到了障礙。

我想知道是否有人可以看到下面附加的代碼有任何明顯錯誤可能導致以下錯誤:

    SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/sonya-local] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: com.sonya.spring.entity.Downloads cannot be cast to java.lang.Integer] with root cause
java.lang.ClassCastException: com.sonya.spring.entity.Downloads cannot be cast to java.lang.Integer
    at org.hibernate.type.descriptor.java.IntegerTypeDescriptor.unwrap(IntegerTypeDescriptor.java:19)
    at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$1.doBind(IntegerTypeDescriptor.java:46)
    at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:74)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:277)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:272)
    at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:53)
    at org.hibernate.hql.internal.ast.exec.BasicExecutor.doExecute(BasicExecutor.java:82)
    at org.hibernate.hql.internal.ast.exec.BasicExecutor.execute(BasicExecutor.java:59)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:429)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:374)
    at org.hibernate.internal.SessionImpl.executeUpdate(SessionImpl.java:1495)
    at org.hibernate.query.internal.AbstractProducedQuery.doExecuteUpdate(AbstractProducedQuery.java:1507)
    at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1485)
    at com.sonya.spring.dao.DownloadsDAOImpl.archiveProduct(DownloadsDAOImpl.java:128)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy50.archiveProduct(Unknown Source)
at com.sonya.spring.service.DownloadsServiceImpl.archiveProduct(DownloadsServiceImpl.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

基本上,我希望在一個小的審查期(1 小時)之后在表之間移動數據,到目前為止我已經成功添加了時間戳和其他一些功能,例如搜索、api 調用等。

這樣做的最終目標是附加一個調度程序並讓它全天定期運行,但現在我已經添加了一個 url 映射來手動觸發它。

當點擊 URL 時,我收到上述錯誤:

我的代碼:

控制器代碼

 @GetMapping("/archiveproducts")
    public String archiveDownloads (){

         List<Downloads> theDownloads = downloadsService.getDownloads();


         for (Downloads archiveDownload : theDownloads)  {

            // display the download ... just for clarity
            System.out.println(archiveDownload);

            Date timer = new Date();
            Date purge = archiveDownload.getTimestamp();
            long hours  = (timer.getTime() - purge.getTime()) / DateTimeConstants.MILLIS_PER_HOUR;

            if (hours >= 1) {

            downloadsService.archiveProduct(archiveDownload);

            }
        }
        return "list-downloads";
 }

下載DAOimpl代碼:(java:128)

public List<Downloads> archiveProduct(Downloads archiveDownload) {

    Session currentSession = sessionFactory.getCurrentSession();

    Query theCopyQuery = 
            currentSession.createQuery("insert into Archive(fieldOne, fieldTwo, fieldThree, fieldFour, fieldFive, , fieldSix, FieldSeven)"
                    + "sfieldOne, fieldTwo, fieldThree, fieldFour, fieldFive, , fieldSix, FieldSeven from Downloads where id=:theId");
    theCopyQuery.setParameter("theId", archiveDownload);

    theCopyQuery.executeUpdate();

    List<Downloads> archiveProducts = theCopyQuery.getResultList();

    Query theDeleteQuery = 
            currentSession.createQuery("delete from Downloads where id=:theId");
    theDeleteQuery.setParameter("theId", archiveDownload);

    theDeleteQuery.executeUpdate(); 

   // return the results        
   return archiveProducts;
}

下載ServiceImpl.java:65

@Override
@Transactional
public List<Downloads> archiveProduct(Downloads archiveDownload) {

    return downloadsDAO.archiveProduct(archiveDownload);
}

我知道它告訴我我不能將實體轉換為整數。 但我不明白這個整數是從哪里來的,也不明白如何修復/重新喜歡它。我對其中一個 API 使用了類似的代碼方法,它工作正常。

@PostMapping("/listdownloads")
public List<Downloads> addDownloads (@RequestBody List<Downloads> theDownloads){

        for (Downloads tempDownload : theDownloads) {

        // display the download ... just for clarity
         tempDownload.setId(0);
         tempDownload.setTimestamp(null);
         System.out.println(tempDownload);

          // save to the database
          downloadsService.saveProduct(tempDownload);
        }   

干杯,

丹尼

我認為問題出在這條線上。

theDeleteQuery.setParameter("theId", archiveDownload);

它需要一個整數作為 Id,但您傳遞了錯誤消息中提到的Downloads對象。 你可以嘗試這樣的事情。

theDeleteQuery.setParameter("theId", archiveDownload.getId());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM