加入hibernate的路径

时间:2016-02-08 14:40:25

标签: java hibernate

我遇到了hibernate HQL查询和简单的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);

我的Java文件包含以下代码:

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;
    }
}

我在控制台中收到以下错误:

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

1 个答案:

答案 0 :(得分:0)

您需要在@OneToMany@OneToOne之间建立关联(PersonPhisicalPerson)。有了这样的关联,你就不需要where条款

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

在示例中pfPerson的属性,与PhisicalPerson相关联。

class Person {

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

}

class PhisicalPerson {

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

}