Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类java.util.ArrayList不能强制转换为类com.patient.entity.CommentEntity(java.util.ArrayList位于加载程序“bootstrap”的模块java.base中);_Java_Spring Boot - Fatal编程技术网

类java.util.ArrayList不能强制转换为类com.patient.entity.CommentEntity(java.util.ArrayList位于加载程序“bootstrap”的模块java.base中);

类java.util.ArrayList不能强制转换为类com.patient.entity.CommentEntity(java.util.ArrayList位于加载程序“bootstrap”的模块java.base中);,java,spring-boot,Java,Spring Boot,2021-04-25 17:56:29.483错误11940-[nio-8080-exec-3] o、 a.c.c.c.[/].[dispatcherServlet]:Servlet.service for 路径为[]的上下文中的servlet[dispatcherServlet]引发异常 [请求处理失败;嵌套异常为 java.lang.ClassCastException:无法强制转换类java.util.ArrayList 类com.patient.entity.CommentEntity

2021-04-25 17:56:29.483错误11940-[nio-8080-exec-3] o、 a.c.c.c.[/].[dispatcherServlet]:Servlet.service for 路径为[]的上下文中的servlet[dispatcherServlet]引发异常 [请求处理失败;嵌套异常为 java.lang.ClassCastException:无法强制转换类java.util.ArrayList 类com.patient.entity.CommentEntity java.util.ArrayList位于 加载程序“引导”的java.base模块; com.patient.entity.CommentEntity位于加载程序的未命名模块中 org.springframework.boot.devtools.restart.classloader.RestartClassLoader @d04c48e]有根本原因

java.lang.ClassCastException:无法强制转换类java.util.ArrayList 类com.patient.entity.CommentEntity java.util.ArrayList位于 加载程序“引导”的java.base模块; com.patient.entity.CommentEntity位于加载程序的未命名模块中 org.springframework.boot.devtools.restart.classloader.RestartClassLoader @d04c48e at com.patient.controller.CommentController.viewAllArticleByCommentIdCommentController.java:65 ~[classes/:na]at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0Native 方法~[na:na]at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invokeNativeMethodAccessorImpl.java:64 ~[na:na]at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invokeDelegatingMethodAccessorImpl.java:43 ~[na:na]at java.base/java.lang.reflect.Method.invokeMethod.java:564~[na:na]

模型

评论库

控制器

服务

service.getAllCommentByArticledArticle_id.get;返回的列表不是CommentEntity


@RequestMappingpath={/ViewCommentByArticle id/{article_id}}公共字符串viewAllArticleByCommentIdModel,@PathVariablearticle_id可选article_id抛出RecordNotFoundException{List entity=service.GetAllCommentByArticle_id.get;model.addAttributecomment,entity;返回保存的_comment;}现在我得到如下错误:SQL[从comment comment中选择comment,其中comment.post_id=?];嵌套异常为org.hibernate.exception.sqlgrammareexception:无法执行查询
        @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);
    }
@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);
            }
    }
   

 

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