在实体中,我只想查询ID和ManyToMany列。
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(length = 64)
@Size(max = 64)
@NotEmpty
private String title;
@ManyToMany(fetch = FetchType.EAGER)
@ToString.Exclude
@NotEmpty
@JoinTable(
name = "article_tag",
joinColumns = @JoinColumn(name = "articleId"),
inverseJoinColumns = @JoinColumn(name = "tagId"))
private Set<Tag> tags;
//...other fields
}
我正在用这个jpql
@Query("SELECT a.tags,a.id FROM Article a WHERE a.id in :ids")
List<Article> findIdAndTags(List<Long> ids);
但是我得到了一个sql语法错误。因为hib生成的sql包含.“”作为列名
select
. as col_0_0_,
article0_.id as col_1_0_,
tag2_.id as id1_45_,
tag2_.circleId as circleid6_45_,
tag2_.createdTime as createdt2_45_,
tag2_.essence as essence3_45_,
tag2_.modifiedTime as modified4_45_,
tag2_.name as name5_45_
from
article article0_
inner join
article_tag tags1_
on article0_.id=tags1_.articleId
inner join
tag tag2_
on tags1_.tagId=tag2_.id
where
article0_.id in (
? , ? , ?
)
我希望使用正确的sql,例如
select
article0_.id as col_1_0_,
tag2_.id as id1_45_,
tag2_.circleId as circleid6_45_,
tag2_.createdTime as createdt2_45_,
tag2_.essence as essence3_45_,
tag2_.modifiedTime as modified4_45_,
tag2_.name as name5_45_
from
article article0_
inner join
article_tag tags1_
on article0_.id=tags1_.articleId
inner join
tag tag2_
on tags1_.tagId=tag2_.id
where
article0_.id in (
? , ? , ?
)
我可以得到一个文章实体只有id和标签字段,其他都是空的。
1条答案
按热度按时间hrirmatl1#
不能直接选择复数属性,而必须将其联接。请改用以下查询: