Build a jpa specification query without retriving child entity data

53 views Asked by At

I'm trying to build a search query to find all Test entity data. Here are my classes...

TestParent.java

@Entity
public class TestParant{
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private String createdBy;

  @OneToMany(
            targetEntity = Channel.class,
            cascade = {CascadeType.ALL},
            fetch = FetchType.LAZY)
    @JoinColumn(name = "test_id")
    private List<Channel> channels;
 }

Channel.java

@Entity
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Channel{

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Enumerated(EnumType.STRING)
    private Channel channelName;


    @ManyToOne
    @JoinColumn(name = "id", referencedColumnName = "id")
    @JsonBackReference
    private Test test;

    @OneToMany(
            targetEntity = TvSet.class,
            cascade = {CascadeType.ALL},
            fetch = FetchType.LAZY)
    @JoinColumn(name = "channel_id")
    private List<TvSet> tvSets;

}

My specification query...

   public Specification<TEST> generateFilterTestQuery(FilterRequest filterRequest) {
        log.info("start to build query  from" + filterRequest);
        return (root, cq, cb) -> {
            Predicate p = cb.conjunction();
            Join<Channel, Campaign> channel = root.join("channels", JoinType.INNER);
            if (!filterRequest.getStartCreatedDate().isEmpty() && !filterRequest.getEndCreatedDate().isEmpty())
                p = cb.and(p,
                        cb.between(root.get(CREATED_DATE),
                                filterRequest.getStartCreatedDate(), filterRequest.getEndCreatedDate()));
            else if (!(filterRequest.getStartCreatedDate().isEmpty()))
                p = cb.and(p,
                        cb.greaterThanOrEqualTo(root.get(CREATED_DATE),
                                filterRequest.getStartCreatedDate()));
            else if (!(filterRequest.getEndCreatedDate().isEmpty()))
                p = cb.and(p,
                        cb.lessThanOrEqualTo(root.get(CREATED_DATE),

                                filterRequest.getEndCreatedDate()));
            if (!filterRequest.getCreatedBy().isEmpty()) {
                p = cb.and(p,
                        cb.equal(root.get("createdBy"), filterRequest.getCreatedBy()));
            }
          
            if (!filterRequest.getBrandId().isEmpty()) {
                p = cb.and(p,
                        cb.equal(root.get("brandId"), filterRequest.getBrandId()));
            }
            if (!filterRequest.getChannel().isEmpty()) {

                p = cb.and(p,
                        cb.equal(channel.get("channelName"), Channel.valueOf(filterRequest.getChannel())));
            }
            if (filterRequest.getSort().equals(SortEnum.ASC)) {
                cq.orderBy(cb.asc(root.get(CREATED_DATE)));
            } else {
                cq.orderBy(cb.desc(root.get(CREATED_DATE)));
            }
            return p;
        };
    }

And my service method...

public Page<TEST> filter(FilterRequest filterRequest) throws BaseException {
        try {
            log.info("Getting campaign for filterRequest : {} ", filterRequest);
            // Find Sorted Creatives By filterRequest
            Pageable pageable =
                    PageRequest.of(filterRequest.getPageNumber(), filterRequest.getPageSize());
            return repository.findAll(generateFilterTestQuery(filterRequest),pageable);
        } catch (IllegalArgumentException e) {
            log.error(e.getMessage());
            throw new BaseException(e.getMessage());
        }
    }

The result I'm getting...

   "content": [
    {
      "id": 516,
      "name": "Tes",
      "createdBy": "test",
      "brandId": "1",
      "createdDate": 1671529546,
      "channels": [
        {
          "id": 312,
          "channelName": "TV1",
          "channelStatus": "DONE",
          "tvSet": [
            {
              "id": 312,
                "locations": [
                  {
                    "id": 302,
                    "isGeoLocation": true,
                    "cities": [
                      {
                        "id": 282,
                        "distanceUnit": "kilometer",
                      },
                      {
                        "id": 283,
                        "distanceUnit": "kilometer",
                      }
                    ],
       
    }

But I want to get results(channels ) that without getting tvSet array like in below

   "content": [
    {
      "id": 516,
      "name": "Tes",
      "createdBy": "test",
      "brandId": "1",
      "createdDate": 1671529546,
      "channels": [
        {
          "id": 312,
          "channelName": "TV1",
          "channelStatus": "DONE",
          "tvSet": [],
       
    }

Can someone please help me to resolve this issue?? Thank you!!

0

There are 0 answers