Search code examples
mysqlhibernatejpainner-join

Hiberante Inner join gives an 'Path expected for join' exception


I have the below entities,

    @Entity
    @Table(name = "person")
    public class Person implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Long id;

        // Other fields 
    }

     @Entity
        @Table(name = "agreement")
        public class Agreement implements Serializable {

            private static final long serialVersionUID = 15L;

            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            @Column(name = "id")
            private Long id;

            @ManyToOne
            @JoinColumn(name = "person_id", nullable = false)
            private Person person;

            @ManyToOne
            @JoinColumn(name = "company_id", nullable = false)
            private Company company;

        }

    @Entity
    @Table(name = "company")
    public class Company implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Long id;

        @Column(name = "name", length = 100)
        private String name;
    }

I need to get all the persons who has a agreement with the company with the company ID 45. Below is the native query that I have written to fetch the details.

SELECT distinct p.* ROM person p INNER JOIN agreement a ON a.person_id = p.id where a.company_id = 45;

But I was told to convert it to a JPA entity query. And I came up with the below,

SELECT distinct p  FROM Person p INNER JOIN Agreement a WHERE a.person = p AND a.company.id = 45;

But this gives me the below exception,

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [SELECT distinct p FROM Person p INNER JOIN Agreement a WHERE a.person = p AND a.company.id = 45] at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:91) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:268) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142) at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:115) at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:76) at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302) at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240) at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1894) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:305

Can someone help me out what I have done wrong in the query ?


Solution

  • JPA uses paths in the HQL statement, to relate from one entity to the other and since it didn't find any it is complaining. You can try below query.

    SELECT distinct p FROM Agreement a JOIN a.person p WHERE a.company.id = 45;