Home JPAQuery join intermediate many to many table
Reply: 0

JPAQuery join intermediate many to many table

user1501 Published in August 14, 2018, 8:26 am

I'm using QueryDSL JPA, and want to do a join between two tables. I found many similar questions here already, but my case is different from all of them by one little detail. Here is a simplified version of my classes:

@Entity(name = "TABLE_A")
public class TableA {
    @Column(name = "ID_A", nullable = false)
    private Long idA;

@Entity(name = "TABLE_B")
public class TableB {
    @Column(name = "ID_B", nullable = false)
    private Long idB;

@Entity(name = "TABLE_C")
public class TableC {
    @Column(name = "ID_C", nullable = false)
    private Long idC;

    @JoinColumn(name = "ID_A", referencedColumnName = "ID_A")
    private TableA tableA;

    @JoinColumn(name = "ID_B", referencedColumnName = "ID_B")
    private TableB tableB;

Now what I want to do is join Tables A, C and B, to find the Bs which are linked to A. I know this seems like a useless step between, why not add a relation from A to B directly. In my case this is needed, these are just example classes to illustrate.

I tried this:

QTbTableA tableA = QTbTableA.tableA;
QTbTableB tableC = QTbTableC.tableC;
JPAQuery query = new JPAQuery(entityManager).from(tableA);
query.leftJoin(tableA, tableC.tableA);

The join throws an Exception because tableC.tableA is not a root path, only a property. But how do I join these tables correctly then?

Thanks in advance!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.422094 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO