I am writing a search by fields. There are two entities between which there is only a @ManyToOne relationship. Is it possible to make a join if the root element is a DataListEntity and a specification needs to be built on its basis?
- But without creating a @OneToMany relationship in DataListEntity
- Without subqueries
public static Specification<DataListEntity> joinWithDataListOptions() {
return (root, query, cb) -> {
Join<DataListEntity, DataListOptionEntity> join = root.join("join_field", JoinType.LEFT); //FIELD????
return cb.conjunction();
};
}
@Entity
@Data
@Accessors(chain = true)
@Table(name = "data_list")
@FieldNameConstants
public class DataListEntity {
@Id
@GeneratedValue(generator = "uuid")
private UUID id;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "key")
private String key;
}
Second an entity that has relationship to first entity how @ManyToOne
@Entity
@Data
@Accessors(chain = true)
@Table(name = "data_list_option")
public class DataListOptionEntity implements EasyLoggable {
@Id
@GeneratedValue(generator = "uuid")
private UUID id;
@Column(name = "data_list_id")
private UUID dataListId;
@Column(name = "business_account_id")
private UUID businessAccountId;
@Column(name = "option")
private String option;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "data_list_id", insertable = false, updatable = false)
private DataListEntity dataList;
}
Why do I need this? To then use the connection in the search by table fields. I don't want to make a connection for each search by field, but to use one connection and add many predicates to it, but then call the logic in other methods and connect through "and"