问题描述
我对JPA(以及JPQL扩展)还很陌生.希望有人能启发我这个问题.
I'm fairly new to JPA (and JPQL by extension). Hopefully, someone will be able to enlighten me with this problem.
我要执行的查询...
The query I'm trying to execute ...
String query = "select u from user u where u.email = '" + userEmail + "' and u.password = sha1('"+ userPassword + "')";
List resultList = emf.createEntityManager().createQuery(query).getResultList();
我正在得到以下异常...
And I'm getting the follwowing exception ...
Caused by: Exception [EclipseLink-8025] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing the query [select u from Utilisateur u where u.email = 'myuser' and u.mot_Passe = sha1('mypassword')], line 1, column 73: unexpected token [(].
Internal Exception: NoViableAltException(83@[()* loopback of 383:9: (d= DOT right= attribute )*])
at org.eclipse.persistence.exceptions.JPQLException.unexpectedToken(JPQLException.java:372)
...
很显然,我在这里丢失了一些东西,我应该以某种方式逃脱角色吗?用另一种方式指定它?非常感谢您的帮助.
Obviously, I'm missing something here, should I escape the character somehow? specify it in another way? I'll appreciate very much any help on this.
推荐答案
我认为JPA没有sha1函数.因此,您有两种选择:
I think that JPA doesn't have a sha1 function. So you have two options:
-
您可以在Java代码上实现sha1函数,并在将参数加载到查询中之前使用它们.
You can implementa a sha1 function on you Java code and use them before load the parameters in the query.
String query = "select u from user u where u.email = :userEmail" +
" and u.password = :userPassword";
Query jpqlQuery = em.createQuery(query)
.setParameter("userEmail", userEmail)
.setParameter("userPassword",sha1(userPassword));
如果数据库具有sha1函数,则可以编写本机查询.检查此链接: http://www.oracle.com/technetwork/articles /vasiliev-jpql-087123.html
List<Customer> customers = (List<Customer>)em.createNativeQuery
("SELECT * FROM customers", jpqlexample.entities.Customer.class)
.getResultList();
Iterator i = customers.iterator();
Customer cust;
while (i.hasNext()) {
cust = (Customer) i.next();
//do something
}
这篇关于JPA:如何使用SHA1加密来保留列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!