0

当我在我的 spring 项目中运行这个查询时,它只会返回一个错误。我尝试了不同的方法但没有任何结果,如果有人可以帮助我,将非常感激。我有 3 个表:用户、角色和用户角色,并且我的地址是嵌入的。也使用连接表。

所有其他类是:User、Role 和 User_Role spring security

    public List<User> getUsersByNameOrLogin(final String value){
    List<User> result = new ArrayList<User>(0);

    if(value ==null){
        return result;
    }

    final StringBuilder jpql = new StringBuilder("SELECT u FROM User u WHERE ");
    jpql.append(" u.lastname like  "+value);
   jpql.append(" OR u.firstname like  "+value);
    jpql.append(" OR u.email like  "+value);

    final String valueLike = "%"+value+"%";

    result =  entityManager.createQuery(jpql.toString(), User.class)
            .setParameter("lastname", valueLike)
           .setParameter("firstname", valueLike)
           .setParameter("email", valueLike)
            .getResultList();

    return result;
}  

错误是:

无法解析属性:姓氏:用户 [SELECT u FROM User u WHERE u.lastname like pappo OR u.firstname like pappo OR u.email like pappo]

和所有的错误信息:

    Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.QueryException:      could not resolve property: lastname of: User [SELECT u FROM se.guard.User u WHERE  u.lastname like  pappo OR u.firstname like  pappo OR u.email like  pappo]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1222)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1168)
at             org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:292)
at se.datalayer.service.UserService.getUsersByNameOrLogin(UserService.java:189)
at se.datalayer.main.Main.getUserByParam(Main.java:131)
at se.datalayer.main.Main.main(Main.java:31)
     Caused by: org.hibernate.QueryException: could not resolve property: lastname of:      User [SELECT u FROM se.guard.User u WHERE  u.lastname like  pappo OR u.firstname like      pappo OR u.email like  pappo]
at       org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
at        org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:75)
        at    org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1451)
at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:312)
at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:487)
at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:611)
at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:263)
at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:210)
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:117)
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:113)
at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:868)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1323)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4387)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4004)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1909)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1859)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1859)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:824)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:610)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:294)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:237)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:98)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1760)
at    org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:277)
... 3 more
4

2 回答 2

7

value如果要使用命名参数,为什么要包含变量?此外,属性名称区分大小写。改成:

final String jpql = "SELECT u FROM User u WHERE "
  "    u.lastName  like :lastname" +;
  " OR u.firstName like :firstname" +;
  " OR u.email     like :email";

是的,StringBuilder这里没有必要。您可以通过仅使用一个参数来进一步简化它:

final String jpql = "SELECT u FROM User u WHERE "
  "    u.lastName  like :valueLike" +;
  " OR u.firstName like :valueLike" +;
  " OR u.email     like :valueLike";

result =  entityManager.createQuery(jpql, User.class)
        .setParameter("valueLike", valueLike)
        .getResultList();
于 2012-08-19T15:30:50.370 回答
0

Tomasz回答后的解决方案是:

    public Collection<User> findUserByAnyValue(String value)
{
    List<User> result = new ArrayList<User>(0);

    if(value ==null){
        return result;
    }

    final String searchParam = "%"+value+"%";

    final String query = "SELECT u FROM User u WHERE "+

              " u.firstname like :searchParam" +
              " OR u.firstname like :searchParam" +
              " OR u.lastname  like :searchParam" +
              " OR u.username  like :searchParam" +
              " OR u.password  like :searchParam" +
              " OR u.email     like :searchParam"+
              " OR u.address.street  like :searchParam"+
              " OR u.address.zipcode  like :searchParam"+
              " OR u.address.city like :searchParam";

            result =  entityManager.createQuery(query, User.class)
                    .setParameter("searchParam", searchParam)
                    .getResultList();

    return result;

}

再次感谢托马斯。

于 2012-08-20T15:39:44.463 回答