spring引导数据jpa查询具有@manytomany集合的实体

eivnm1vs  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(301)

我有一个名为club的实体,它与名为type的实体有@manytomy关系。这种类型有各种各样的俱乐部标签,比如它是足球俱乐部还是游泳俱乐部。所以一个俱乐部可以有多种类型,也可以在俱乐部之间共享某种类型。当我想选择一个俱乐部时,我还想显示与访问类型集合的俱乐部相关联的标签。我该如何做到这一点?

@Data
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Club {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(length = 20)
    private String name;

    @Column(length = 20)
    private String shortName;

    @Column(length = 80)
    private String description;

    @Column(length = 50)
    private String email;

    private boolean status;

    @Lob
    private Blob logo;

    @Lob
    private Blob cover;

    @Column(length = 50)
    private String website;

    private double longitude;

    private double latitude;

    @Column(length = 20)
    private String registrationId;

    @CreatedDate
    @Temporal(TemporalType.DATE)
    @Column(nullable = false, updatable = false)
    private Date registrationDate;

    @Temporal(TemporalType.DATE)
    private Date lastActiveDate;

    @OneToMany(mappedBy = "club")
    private List<Favourite> favourites;

    @ManyToMany
    private List<Type> type;

    @OneToOne(fetch = FetchType.LAZY)
    private Address address;

}
@Entity
@Data
@EntityListeners(AuditingEntityListener.class)
public class Type {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(length = 10)
    private String tag;

    @ManyToMany(mappedBy = "type")
    private List<User> user;

    @ManyToMany(mappedBy = "type")
    private List<Club> club;

    @CreatedDate
    @Temporal(TemporalType.DATE)
    @Column(nullable = false, updatable = false)
    private Date createdDate;

}

在clubrespository.java中,我试图像这样访问类型集合的标记变量。

@Repository
public interface ClubRepository extends JpaRepository<Club, Integer> {

    @Query("SELECT new com.service.payload.response.ClubSend(c.id, c.name, c.type.tag, c.shorName, c.description, c.email, c.logo, c.cover, c.longitude, c.latitude, c.address) FROM Club c JOIN c.type WHERE c.id=c.type.club.id")
    List<Club> findAllClubs();

}

clubsend是我创建的一个dto类,用于Map要显示的字段,但是当我这样做时,我收到了这个错误。

Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: illegal attempt to dereference collection [club0_.id.type] with element property reference [tag] [SELECT new com.vayemo.mysponsor.service.payload.response.ClubSend(c.id, c.name, c.type.tag, c.shorName, c.description, c.email, c.logo, c.cover, c.longitude, c.latitude, c.address) FROM com.vayemo.mysponsor.service.model.Club c JOIN c.type WHERE c.id=c.type.club.id]

Caused by: org.hibernate.QueryException: illegal attempt to dereference collection [club0_.id.type] with element property reference [tag] [SELECT new com.vayemo.mysponsor.service.payload.response.ClubSend(c.id, c.name, c.type.tag, c.shorName, c.description, c.email, c.logo, c.cover, c.longitude, c.latitude, c.address) FROM com.vayemo.mysponsor.service.model.Club c JOIN c.type WHERE c.id=c.type.club.id]

Caused by: org.hibernate.QueryException: illegal attempt to dereference collection [club0_.id.type] with element property reference [tag]
gmol1639

gmol16391#

你的问题是 c.type 在查询中是一个列表,因此 c.type.club 无法取消引用。如果要访问type对象,则需要在连接期间为其提供别名(如下所示 ... FROM Club c JOIN c.type t ... )但据我所知,你的where子句隐含在 @ManyToMany 关系。如果你想了解更多细节,请查看这个答案。

相关问题