1

There is entity by name Transaction which has two mapped(@OneToOne) child entities by name ServiceTransaction and OtherFeeTransaction. Able to filter using Specification for all the fields but for the child entity fields at the same time.

Transaction Entity:

 public class Transaction implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(
        name = "UUID",
        strategy = "org.hibernate.id.UUIDGenerator"
    )
    @Column(name = "id", updatable = false, nullable = false, columnDefinition = "uuid")
    private UUID id;

    @Column(name = "txn_id")
    private String txnId;

    @Column(name = "account_no")
    private String accountNo;

    @Column(name = "date", nullable = false)
    private Date date;

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "service_id")
    private ServiceTransaction serviceTransaction;

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "other_id")
    private OtherFeeTransaction otherFeeTransaction;

    }

OtherFee Entity:

public class OtherFeeTransaction implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
    name = "UUID",
    strategy = "org.hibernate.id.UUIDGenerator"
)
@Column(name = "id", updatable = false, nullable = false, columnDefinition = "uuid")
private UUID id;

@Column(name = "other_fee_id")
private UUID otherFeeId;

@Column(name = "type_data_id")
private String typeDataId;

}

Service Entity:

public class ServiceTransaction implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
    name = "UUID",
    strategy = "org.hibernate.id.UUIDGenerator"
)
@Column(name = "id", updatable = false, nullable = false, columnDefinition = "uuid")
private UUID id;

@Column(name = "parcel_id")
private String cadastreId;

@Column(name = "ba_unit_id")
private String titleId;

}

Specification

Predicate service = cb.like(root.<ServiceTransaction>get("serviceTransaction").get("titleId"),
                "%" + criteria.getTitleId() + "%");

Predicate other = cb.like(root.<OtherFeeTransaction>get("otherFeeTransaction").get("typeDataId"),
                "%" + criteria.getTitleId() + "%");
//works if any one of the predicates is added, but doesn't work if both given like below
predicateList.add(service);
predicateList.add(other);

criteriaQuery.where(cb.and(predicateList.toArray(new Predicate[predicateList.size()])))
            .getRestriction();

If any one of the predicate's service or other is given able to get the result but not if both predicates given.

Even tried with or caluse it deos not work

5
  • Please add the entities.
    – M. Deinum
    Commented Mar 10, 2020 at 11:54
  • added the entities Commented Mar 10, 2020 at 12:04
  • And what is the problem exactly/
    – M. Deinum
    Commented Mar 10, 2020 at 12:26
  • I dont know what is your criteria. But you should try to query database first. Maybe there is no data that is satisfied by the criteria.
    – sonerokur
    Commented Mar 10, 2020 at 13:35
  • Also, you created both predicates with "criteria.getTitleId()", is it correct ?
    – sonerokur
    Commented Mar 10, 2020 at 13:38

1 Answer 1

1

I got it worked, Forgot to use LeftJoin on the Mapped entites

   Predicate service = cb.like(root.join("serviceTransaction",JoinType.LEFT).get("cadastreId"),
            "%" + criteria.getParcelId() + "%");

        Predicate other = cb.like(root.join("otherFeeTransaction",JoinType.LEFT).get("typeDataId"),
            "%" + criteria.getParcelId() + "%");

        predicateList.add(cb.or(service,other));

Not the answer you're looking for? Browse other questions tagged or ask your own question.