Search code examples
jpaspring-bootquerydsl

What is root path in QueryDSL? Can you explain with an example?


I have the following two entity classes: Country and Type

@Entity
@Table(name = "countries")
public class Country {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id ;

    @Column(name = "iso_code")
    private String isoCode;

    public Country() {
    }

    public Country(String isoCode) {
        this.isoCode = isoCode;
    }

    public Country(int id, String isoCode) {
        this.id = id;
        this.isoCode = isoCode;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getIsoCode() {
        return isoCode;
    }

    public void setIsoCode(String isoCode) {
        this.isoCode = isoCode;
    }

       @Override
    public String toString() {
        return "Country{" +
                "id=" + id +
                ", isoCode='" + isoCode + '\'' +
                '}';
    }
}


@Entity
@Table(name = "types")
public class Type {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "type")
    private String type;

 @ManyToOne
@JoinColumn(name = "country_id")
private Country country;

    @ManyToOne
    @JoinColumn(name = "group_id")
    private Group group;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(int priority) {
        this.group = group;
    }
}

I am trying to retrieve groups using the following in the repository class:

        QType qType = QType.type1;
        QCountry qCountry = QCountry.country;
        QGroup qGroup = QGroup.group;
        QGroup qGroup1 = qType.group;

        JPAQuery queryGroup = new JPAQuery(em);

        QueryBase queryBaseGroups = queryGroup.from(qGroup).innerJoin(qGroup1, qGroup).innerJoin(qType.country, qCountry);

However, I get the error -

java.lang.IllegalArgumentException: Undeclared path 'type1'. Add this path as a source to the query to be able to reference it.

New to JPA. What am I doing wrong here?


Solution

  • So this was solved by adding qType to the from function in the query.

        QueryBase queryBaseGroups = queryGroup.from(qGroup, qType).innerJoin(qGroup1, qGroup).innerJoin(qType.country, qCountry);