Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.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 @Spring数据jpa中的瞬态自定义查询_Java_Spring Boot - Fatal编程技术网

Java @Spring数据jpa中的瞬态自定义查询

Java @Spring数据jpa中的瞬态自定义查询,java,spring-boot,Java,Spring Boot,我有两个实体post、comment,我想使用SpringDataJPA创建一个本机查询 @Query(value = "SELECT * , (select count(*) from comments commentObj where commentObj.post = postObj.id) as total_comments FROM posts postObj where p

我有两个实体post、comment,我想使用SpringDataJPA创建一个本机查询

@Query(value = "SELECT * , (select count(*) from comments commentObj 
                where commentObj.post = postObj.id) as total_comments 
                FROM posts postObj 
                where postObj.status ='1' 
                order by total_comments desc",nativeQuery = true)
List<Post> fetchPosts();
当我运行此查询并成功保存总注释时,它工作正常。但在数据库中创建post类型的新对象时发生错误

原因:java.sql.SQLSyntaxErrorException:字段列表中的未知列“posts\u.total\u comments”

注意:我使用了javax.persistence.Transient中的@Transient,我发现它不能持久化total_comments列。

您也应该为AppUser添加源代码,希望有人能帮助您。例如:@onetomanaymappedby=comment应该是@onetomanaymappedby=post。带有@Transient注释的字段不会持久化。您也应该为AppUser添加源代码,希望有人能帮助您。例如:@onetomanaymappedby=comment应该是@onetomanaymappedby=post。并不会持久化带有@Transient注释的字段。
@Entity(name = "posts")
@Data
public class Post{
    @Id
    private long id;
    private String post;
    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnore
    @JoinColumn(name = "user")
    private AppUser user;
    @org.springframework.data.annotation.Transient
    private  long total_comments;

   @OneToMany(mappedBy = "comment")
   @JsonIgnore
   private List<Comment> commentList;
}


@Entity(name = "comments")
@Data
public class Comment{
    @Id
    private long id;
    private String comment;
    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnore
    @JoinColumn(name = "user")
    private AppUser user;
    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnore
    @JoinColumn(name = "post")
    private Post post;
   
}