SpringBootJPA避免在返回json时重复结果

dxxyhpgq  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(205)

嗨,我有一个springboot项目,它使用springdatajpa。我已经编写了一个定制的jpql语句,其中返回多行数据,其中只有一个值是它们之间的差异。我与一个名为type的实体有一个@manytomy关系,我只获取该实体中的标记列以避免延迟异常。
club.java类

@Getter
@Setter
@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 = new ArrayList<>();

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

}

标记列所在的type.java类。

@Getter
@Setter
@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;

}

clubrepository.java文件

@Query("SELECT new com.vayemo.mysponsor.service.payload.response.ClubSend(c.name, c.shortName, c.description, c.email, c.logo, c.cover, c.longitude, c.latitude, a, t.tag) FROM Club c JOIN c.type t JOIN c.address a")
    List<ClubSend> findAllClubs();

所以本质上,我的查询返回club及其所有属性的重复数据,其中该行中唯一不同的数据是一个标记。我怎样才能避免这种情况?我宁愿把标签作为数组返回@elementcollection不是一个选项,因为我使用type作为实体,并且我在另一个类user中也使用相同的实体。
结果是:

[
{
        "name": "Manchester City",
        "shortName": "MC",
        "description": "Swiss Soccer Club",
        "email": "soccer@gmail.com",
        "logo": null,
        "cover": null,
        "longitude": 79.8766,
        "latitude": 6.858,
        "address": {
            "id": 1,
            "address1": "Bay Street",
            "address2": "Yorkshire",
            "postalCode": "100",
            "city": "Auckland",
            "country": "Newzeland",
            "createdDate": "2020-12-08"
        },
        "tag": "Soccer"
    },
    {
        "name": "Manchester City",
        "shortName": "MC",
        "description": "Swiss Soccer Club",
        "email": "soccer@gmail.com",
        "logo": null,
        "cover": null,
        "longitude": 79.8766,
        "latitude": 6.858,
        "address": {
            "id": 1,
            "address1": "Bay Street",
            "address2": "Yorkshire",
            "postalCode": "100",
            "city": "Auckland",
            "country": "Newzeland",
            "createdDate": "2020-12-08"
        },
        "tag": "Cricket"
    }]

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题