与hibernate和spring社交一起工作,
我正在尝试通过电子邮件地址查询数据库。当我执行此查询时:
公共帐户findAccountByUsername(String username){ Session session = sessionFactory.getCurrentSession(); String selectQuery = "FROM Account as account WHERE account.username = “+username; Query query = session.createQuery(selectQuery); @SuppressWarnings(“unchecked”) List results = query.list(); if (!results.iterator().hasNext()) return null; return results.iterator().next(); }
公共帐户findAccountByUsername(String username){
Session session = sessionFactory.getCurrentSession(); String selectQuery = "FROM Account as account WHERE account.username
= “+username; Query query = session.createQuery(selectQuery); @SuppressWarnings(“unchecked”) List results = query.list();
if (!results.iterator().hasNext()) return null; return results.iterator().next(); }
我得到这个例外
2013-01-22 14:37:13,090 [DEBUG] [HibernateTransactionManager,doBegin(),569]-将Hibernate事务公开为JDBC事务[com.mchange.v2.c3p0.impl.NewProxyConnection@3b249bb2] 2013-01-22 14 :37:13,352 [DEBUG] [QueryTranslatorImpl,parse(),272]-parse()-HQL:从masterPackage.model.orm.Account作为帐户WHERE account.username = myEmail@gmail.com 2013-01-22 14: 37:13,383 [DEBUG] [AbstractPlatformTransactionManager,processRollback(),843]-启动事务回滚2013-01-22 14:37:13,384 [DEBUG] [HibernateTransactionManager,doRollback(),672]-在会话[org]上回滚Hibernate事务.hibernate.impl.SessionImpl @ 294a7134] 2013-01-22 14:37:13,385 [DEBUG] [JDBCTransaction,rollback(),186]-回滚 ....... 2013-01-22 14:37:18,818 [WARN] [ProviderSignInController,oauth2Callback(),177]-处理OAuth2回调时发生异常(意外字符:’@’[FROM masterpackage.model.orm.Account作为帐户WHERE account.username = myEmail@gmail.com])。重定向到/ signin
2013-01-22 14:37:13,090 [DEBUG] [HibernateTransactionManager,doBegin(),569]-将Hibernate事务公开为JDBC事务[com.mchange.v2.c3p0.impl.NewProxyConnection@3b249bb2] 2013-01-22 14 :37:13,352 [DEBUG] [QueryTranslatorImpl,parse(),272]-parse()-HQL:从masterPackage.model.orm.Account作为帐户WHERE account.username = myEmail@gmail.com 2013-01-22 14: 37:13,383 [DEBUG] [AbstractPlatformTransactionManager,processRollback(),843]-启动事务回滚2013-01-22 14:37:13,384 [DEBUG] [HibernateTransactionManager,doRollback(),672]-在会话[org]上回滚Hibernate事务.hibernate.impl.SessionImpl @ 294a7134] 2013-01-22 14:37:13,385 [DEBUG] [JDBCTransaction,rollback(),186]-回滚
.......
2013-01-22 14:37:18,818 [WARN] [ProviderSignInController,oauth2Callback(),177]-处理OAuth2回调时发生异常(意外字符:’@’[FROM masterpackage.model.orm.Account作为帐户WHERE account.username = myEmail@gmail.com])。重定向到/ signin
有办法解决此问题吗?
总有一种方法可以将电子邮件地址中的@字符另存为其他字符,但是我想问是否有比该解决方案更好的东西。
不要连接HQL查询。请改用命名参数。它是Hibernate 中查询对象模式的实现。
对于您的情况:
Session session = sessionFactory.getCurrentSession(); String selectQuery = "FROM Account as account WHERE account.username = :usernameParam"; Query query = session.createQuery(selectQuery); query.setParameter("usernameParam", username); @SuppressWarnings("unchecked") List<Account> results = query.list(); if(results.isEmpty()){ return null; } else { return result; }
Offtop: 建议永远不要为这种方法返回 null 值。更好的是返回一个空集合。例如,返回new ArrayList <>(); 因此,您可以隐式使用Null Object模式。