如何将查询转换为hibernate查询语言?

时间:2012-09-10 01:30:02

标签: mysql hql

如何用hibernate查询语言编写这个查询

select * from preferred_space p, building b, floor f, space_type st, space s
    where p.user_id=11
    and p.space_id=s.id
    and s.building_id=b.id
    and s.floor_id=f.id
    and s.space_type_id=st.id 
    order by p.id;

如果我这样做,它会向我显示错误?

 String sql =    "from     PreferredSpace p, Space s, Building b, Floor f, SpaceType st " +
                 "where    p.userId = ? " +
                 "and      p.spaceId = s.id" +
                 "and      s.buildingId = b.id" +
                 "and      s.floorId = f.id" +
                 "and      s.spaceTypeId = st.id" +
                 "order by p.id";

错误:

ERROR o.h.hql.internal.ast.ErrorCounter - line 1:242: unexpected token: s
16:18:17.569 [http-bio-8080-exec-24] ERROR o.h.hql.internal.ast.ErrorCounter - line 1:242: unexpected token: s
antlr.NoViableAltException: unexpected token: s

为以“s”开头的每一行显示相同的错误。还有“by”。

1 个答案:

答案 0 :(得分:2)

您需要添加空格:

String sql = "from     PreferredSpace p, Space s, Building b, Floor f, SpaceType st " +
             "where    p.userId = ? " +
             "and      p.spaceId = s.id " +
             "and      s.buildingId = b.id " +
             "and      s.floorId = f.id " +
             "and      s.spaceTypeId = st.id " +
             "order by p.id";