最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin5PV0评论

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"

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far