微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过 id 标签,Spring Data Jpa Bidirectional 查找带有标签的帖子列表

如何解决通过 id 标签,Spring Data Jpa Bidirectional 查找带有标签的帖子列表

选择所有带有标签的帖子,其中包含按 id 标记标签,并使用 DTO 选择某些字段。

id 为 1 的标签标题是 JAVA33333333,我想要上面这个输出 标签/1 GET

> split(V(gg)$name,clusters(gg)$membership)
$`1`
 [1] "1" "A" "B" "C" "D" "E" "2" "F" "G" "3"

$`2`
[1] "4" "I" "J" "K" "L"

我的投影和查询返回错误

“无法提取 ResultSet;sql [n/a];嵌套异常是 org.hibernate.exception.sqlGrammarException:无法提取 ResultSet”,

{
   "id":1,"title":"meu camarada a gente filips","createdAt":"17-06-2021","slug":"meu-camarada-a-gente-filips","summary":"Criar conteúdo para blogs  ","tags":[
      {
         "id":1,"title":"JAVA33333333"
      },{
         "id":2,"title":"python33333333"
      }
   ]
},{
   "id":2,"title":"LULA LIVRE","slug":"lula-ladrao","summary":"Criar conteúdo para blogs ",{
   "id":3,"title":"LULA LIVRE pcsharp","slug":"lula-ladrao-pcsharp","summary":"Criar conteúdo suficiente","title":"JAVA33333333"
      }
   ]
}

发布

public interface PostResponseProjection {
    Long getId();
    String getTitle();
    LocalDateTime getCreatedAt();
    String getSlug();
    String getSummary();
    List<TagProjectionResponse> getTags();
}
public interface TagProjectionResponse {
    Long getId();
    String getTitle();
}

@Repository
public interface TagRepository extends JpaRepository<Tag,Long> {
 
  @Query("SELECT post.id as id "+
        ",post.title as title "+            
        ",post.createdAt as createdAt"+
        ",post.slug as slug"+
        ",post.summary as summary"+
        ",post.tags as tags "+
        "FROM Tag tag INNER JOIN tag.posts post WHERE tag.id=:tagId")
  Set<PostResponseProjection> findAllPostWthByTagId(@Param("tagId") Long id);
}

标签

@Entity
@Table(name = "post")
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
@ToString
public class Post {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;   
    private String title;
    @Column(columnDeFinition = "TEXT")
    private String body;
    @JsonFormat(shape = Shape.STRING,pattern = "dd-MM-yyyy")
    private LocalDate createdAt = LocalDate.Now();
    private String slug;
    private PublishedStatus status;
    @Size(max = 100)
    private String summary;
    
    @ManyToMany(cascade = {
        CascadeType.PERSIST,CascadeType.MERGE
    },fetch = FetchType.EAGER)
    @JoinTable(name = "post_tag",joinColumns = @JoinColumn(name="post_id"),inverseJoinColumns = @JoinColumn(name="tag_id")
    )
    @JsonProperty("tags")
     @JsonIgnoreProperties("posts")    
    private Set<Tag> tags = new HashSet<>();
}

编辑:如果你在下面使用这个查询,你会得到另一个错误

@Entity
@Table(name = "tag")
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
@ToString
public class Tag {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;    
    @ManyToMany(mappedBy = "tags",fetch = FetchType.EAGER) 
    private Set<Post> posts = new HashSet<>();
}

请求的元组值 [index=0,realType=com.microservices.post.model.Post] 不能分配给 请求类型 [java.util.Set];嵌套异常是 java.lang.IllegalArgumentException: 请求的元组值 [index=0,realType=com.microservices.post.model.Post] 不能分配给 请求类型 [java.util.Set]",

编辑 2:如果在 PostRepository 中使用查询 Select Post 工作正常,但 hibernate 使 6 select 这太大了。

@Query("SELECT tag.posts FROM Tag tag INNER JOIN tag.posts post WHERE tag.id=:tagId")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。