[英]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.