繁体   English   中英

class java.util.ArrayList cannot be cast to class com.patient.entity.CommentEntity (java.util.ArrayList is in module java.base of loader 'bootstrap';

[英]class java.util.ArrayList cannot be cast to class com.patient.entity.CommentEntity (java.util.ArrayList is in module java.base of loader 'bootstrap';

2021-04-25 17:56:29.483 ERROR 11940 --- [nio-8080-exec-3] oac.c.C.[.[.[/].[dispatcherServlet]: Servlet.service() for servlet [带有路径 [] 的上下文中的 dispatcherServlet] 引发异常 [请求处理失败; nested exception is java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class com.patient.entity.CommentEntity (java.util.ArrayList is in module java.base of loader 'bootstrap'; com.patient.entity .CommentEntity 位于加载程序 org.springframework.boot.devtools.restart.classloader.RestartClassLoader @d04c48e)] 的未命名模块中,根本原因

java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class com.patient.entity.CommentEntity (java.util.ArrayList is in module java.base of loader 'bootstrap'; com.patient.entity.CommentEntity is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @d04c48e) at com.patient.controller.CommentController.viewAllArticleByCommentId(CommentController.java:65) ~[classes/:na] at java.base/ jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:64) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect .Method.invoke(Method.java:564) ~[na:na]

model

        @Entity
        @Table(name="comment")
        public class CommentEntity {
        
            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            @Column(name="id")
            private Long id;
                
            @Column(name="content")
            private String content;
            
            @Column(name = "date")
            private Date date = new Date();
            
            @ManyToOne
            @JoinColumn(name = "post_id", referencedColumnName = "id", nullable = false)
            private ArticleEntity article;
    }

评论库

    @Repository
    public interface CommentRepository extends CrudRepository<CommentEntity, Long>{
        @Query(value="select comment from Comment comment where comment.post_id = :article_id",nativeQuery =true)
        
        List<CommentEntity> findByArticleId(Long article_id);
    }

Controller

@RequestMapping(path = {"/viewCommentbyArticleId/{article_id}"})
public String viewAllArticleByCommentId(Model model, @PathVariable("article_id") Optional<Long> article_id) throws RecordNotFoundException 
        {
            CommentEntity entity = (CommentEntity) service.getAllCommentByArticleId(article_id.get());
            model.addAttribute("comment", entity);
            return "saved_comment";
        }

服务

public class CommentService{
    public List<CommentEntity> getAllCommentByArticleId(Long article_id)
            {
                
                    return repository.findByArticleId(article_id);
            }
    }
   

 

    

service.getAllCommentByArticleId(article_id.get()); 返回List<CommentEntity>而不是CommentEntity

List<CommentEntity> entity = service.getAllCommentByArticleId(article_id.get());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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