最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Is it possible to join two entities in a specification without a relationship between them, but with a common field - Sta

programmeradmin0浏览0评论

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"

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论