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
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. But i need QAuthorBookRelation to create a request with queryDSL
How to create a class that is bound to two tables ?
I think you're missing the point of JPA, JPQL and 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:
SELECT book, author FROM Book book INNER JOIN book.author author