简体   繁体   中英

Path expected for join hibernate

I have a problem with hibernate HQL queries and simple INNER JOIN

String hql = "SELECT NEW es.criteria.crc.model.queryObjects.NameQuery(pf.name) FROM Person as p INNER JOIN PhisicalPerson as pf WHERE pf.idPersona = p.idPersona";
    return personService.query(hql);

My Java file has the following code:

public class NameQuery{
    private String name;

    public NameQuery(String name) {
        super();
        this.name= name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name= name;
    }
}

I recieve the following error in console:

   Caused By: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join!

You need to have an association ( @OneToMany , @OneToOne ) between Person and PhisicalPerson . Having such an association you don't need the where clause

SELECT NEW es.criteria.crc.model.queryObjects.NameQuery(pf.name) 
    FROM Person as p INNER JOIN p.pf

In the example pf is a property of Person , associated to PhisicalPerson .

class Person {

    @OneToOne(mappedBy = "person")
    private PhisicalPerson pf;

}

class PhisicalPerson {

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fk_person")
    private Person person;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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