简体   繁体   English

如何在 QueryDSL 中创建一个没有实体或与两个表关联的 QClass

[英]How to create a QClass , without an entity or associated with two tables in QueryDSL

let's say that I have three classes假设我有三个班级

    @Entity
    public class Book {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;

        @Column
        private String name;
    }

    @Entity
    public class Author {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;

        @Column
        private String fistName;

        @Column
        private String lastName;
    }

    @Entity
    public class AuthorBookRelation {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;

        @ManyToOne
        @JoinColumn(name = "author", referencedColumnName = "id")
        private Author author;

        @ManyToOne
        @JoinColumn(name = "book", referencedColumnName = "id")
        private Book book;
    }

And accordingly, I have three Qclasses因此,我有三个 Qclasses

QAuthor QBook QAuthorBookRelation. QAuthor QBook QAuthorBookRelation。

but I can do just two classes.但我只能上两节课。

@Entity
    public class Book {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;

        @Column
        private String name;
    }

    @Entity
    public class Author {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;

        @Column
        private String fistName;

        @Column
        private String lastName;

        @ManyToMany
        @JoinTable(
                name = "author_book_relation",
                joinColumns = {@JoinColumn(name = "author")},
                inverseJoinColumns = {@JoinColumn(name = "book")}
        )
        private List<Book> books = new ArrayList<>();
    }

Then there is not need for class AuthorBookRelation.那么就不需要 class AuthorBookRelation。 But i need QAuthorBookRelation to create a request with queryDSL但我需要 QAuthorBookRelation 来创建一个带有 queryDSL 的请求

How to create a class that is bound to two tables?如何创建一个绑定到两个表的 class?

I think you're missing the point of JPA, JPQL and QueryDSL.我认为您错过了 JPA、JPQL 和 QueryDSL 的要点。 You should join associated entities.您应该加入关联实体。 There is absolutely no need for association entities in any of these technologies, unless you need to associate data with this association.在这些技术中绝对不需要关联实体,除非您需要将数据与此关联关联。

Otherwise, you'd just do:否则,您只需执行以下操作:

List<Tuple> result = query().from(QBook.book)
     .innerJoin(QBook.book.author, author)
     .select(QBook.book, QAuthor.author)
     .fetch();

Which is equivalent to the following JPQL:这相当于以下 JPQL:

SELECT book, author FROM Book book INNER JOIN book.author author

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM