简体   繁体   English

带有联接的休眠查询:QuerySyntaxException:意外令牌

[英]Hibernate query with join: QuerySyntaxException: unexpected token

I want to make a query using join , i tested it but i have this error 我想使用join进行查询,我对其进行了测试,但出现此错误

org.hibernate.hql.ast.QuerySyntaxException: unexpected token.. org.hibernate.hql.ast.QuerySyntaxException:意外令牌。

i did: 我做到了:

public List <Card>getCard(Client c) {
        ClientDAO cd = new ClientDAO();
        List<Card> ca = getSessionFactory().getCurrentSession().createQuery("select ca.column1,e.column2 from card ca join ens e on ca.ide=e.ide where ca.idclient="+ cd.getClient(c).getIdclient()).list();//this is a method to get the current client
        return ca;

You have used an SQL syntax to write a HQL query. 您已使用SQL语法编写HQL查询。 In case the Card entity has an ens association, you would write a query like this: 如果Card实体具有ens关联,则可以编写如下查询:

List<Card> ca = getSessionFactory().getCurrentSession()
    .createQuery(
        "select ca " +
                "from Card ca " +
                "join fetch ca.ens " +
                "where ca.idclient = :idclient")
        .setParameter("idclient", cd.getClient(c).getIdclient())
        .list();
    return ca;
}

If you want an SQL query, since you have a projection, you would write a native query instead: 如果您要进行SQL查询,因为有投影,您可以改写一个本机查询:

List<Object[]> ca = getSessionFactory().getCurrentSession()
    .createSQLQuery(
        "select ca.column1, e.column2 " +
        "from card ca " +
        "join ens e on ca.ide=e.ide " +
        "where ca.idclient = :idclient")
    .setParameter("idclient", cd.getClient(c).getIdclient())
.list();

In this case you get a List of Object[] . 在这种情况下,您将获得Object[]List Each Object[] is associated to a selected row, and each array element is a column value. 每个Object[]与选定的行相关联,并且每个数组元素都是列值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 休眠异常。 QuerySyntaxException:意外令牌:HAVING - Hibernate exception. QuerySyntaxException: unexpected token: HAVING Hibernate HQL不断抛出“ QuerySyntaxException:意外令牌” - Hibernate HQL keeps throwing 'QuerySyntaxException: unexpected token' Hibernate查询语法异常:org.hibernate.hql.ast.QuerySyntaxException:意外令牌 - Hibernate Query Syntax exception : org.hibernate.hql.ast.QuerySyntaxException: unexpected token Postgresql- Hibernate查询语法异常:org.hibernate.hql.ast.QuerySyntaxException:附近的意外令牌 - Postgresql- Hibernate Query Syntax exception : org.hibernate.hql.ast.QuerySyntaxException: unexpected token at near HQL查询QuerySyntaxException中的异常意外令牌:&gt; = - Exception in HQL query QuerySyntaxException unexpected token: >= Hibernate:org.hibernate.hql.ast.QuerySyntaxException:意外令牌 - Hibernate: org.hibernate.hql.ast.QuerySyntaxException: unexpected token org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌 - org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:FETCH - org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: FETCH 获取QuerySyntaxException:意外令牌 - Getting QuerySyntaxException: unexpected token QuerySyntaxException:意外令牌: - QuerySyntaxException: unexpected token:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM