简体   繁体   中英

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] in context with path [] threw exception [Request processing failed; 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 is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @d04c48e)] with root cause

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

Comment Repository

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

Service

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

 

    

service.getAllCommentByArticleId(article_id.get()); returns a List<CommentEntity> not a CommentEntity .

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

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