admin管理员组文章数量:1024615
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"
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"
本文标签:
版权声明:本文标题:java - Is it possible to join two entities in a specification without a relationship between them, but with a common field - Sta 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745609385a2158912.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论