I am query data from Entity which has ManytoOne relation on other entities. Using Specification able to fetch the Record of only Root. But I need to fetch the column from relational entities as well.
I am new to Spring boot and trying to build API.
Query root always references entities, then how to use the custom query in the specification.
return new Specification<CallMessage>() {
private static final long serialVersionUID = 1L;
@Override
public Predicate toPredicate(Root<CallMessage> root,
CriteriaQuery<?> query, CriteriaBuilder cb)
{
List<Predicate> predicates = new ArrayList<>();
In<Long> inClause = cb.in(root.get("bed_address"));
inClause.value("XYZ");
predicates.add(inClause);
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}
}
@Entity(name = "calls")
public class CallMessage implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@OneToMany(targetEntity = PresetModel.class, mappedBy = "id", orphanRemoval = false)
private Set<PresetModel> presetModels;
}
@Entity(name = "reports_preset_filter")
public class PresetModel extends AuditModel{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
private String preset_name;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name="call_id" ,nullable=false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private CallMessage callMessage ;
}
While using JpaSpecificationExecutor can't use another query to join these two tables. It there any way to fetch records like- call_message { id: 1, reports_preset_filter : [ ] }
I am trying to build filter API with multiple Predicates, to put here I have trimmed down the List of Predicates as well as other columns of Entities.