问题描述
选择所有带有标签的帖子,其中包含按 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 (将#修改为@)