********* CASE CLOSED *************
I changed the query to
find("from userProfile u where u.email = ?1", new Object[]{email});
And it worked
************************************
Hello
I'm using Hibernate Annotations 3.2.1.ga together with spring jpa
I'm facing a problem when i'm trying to query with email address as parameter.
From my user repository:
Code:
public class UserRepositoryImpl extends JpaTemplate implements UserRepository {
public UserProfile getUser(String email) {
List<UserProfile> users = find("from userProfile u where u.email = " + email);
if (users == null || users.size() == 0) {
return null;
}
if (users.size() == 1) {
return users.get(0);
}
throw new RuntimeException("More than one user registered with the same email");
}
}
I get this exception:
Code:
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryException: unexpected char: '@' [from no.elo.product.tipsystem.domain.UserProfile u where u.email = john.doe@email.com]; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: unexpected char: '@' [from no.elo.product.tipsystem.domain.UserProfile u where u.email = john.doe@email.com]
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:229)
at org.springframework.orm.jpa.DefaultJpaDialect.translateExceptionIfPossible(DefaultJpaDialect.java:114)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:212)
at org.springframework.orm.jpa.JpaAccessor.translateIfNecessary(JpaAccessor.java:152)
at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:190)
at org.springframework.orm.jpa.JpaTemplate.executeFind(JpaTemplate.java:152)
at org.springframework.orm.jpa.JpaTemplate.find(JpaTemplate.java:299)
at org.springframework.orm.jpa.JpaTemplate.find(JpaTemplate.java:295)
at no.elo.product.tipsystem.repository.SaleTipRepositoryImpl.getUser(SaleTipRepositoryImpl.java:24)
at no.elo.product.tipsystem.repository.SaleTipRepositoryImpl.save(SaleTipRepositoryImpl.java:55)
at no.elo.product.tipsystem.repository.RepostitoryTest.onSetUpInTransaction(RepostitoryTest.java:27)
at org.springframework.test.AbstractTransactionalSpringContextTests.onSetUp(AbstractTransactionalSpringContextTests.java:176)
at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:89)
at junit.framework.TestCase.runBare(TestCase.java:125)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.access$001(AbstractAnnotationAwareTransactionalTests.java:47)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests$1.run(AbstractAnnotationAwareTransactionalTests.java:113)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTest(AbstractAnnotationAwareTransactionalTests.java:176)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTestTimed(AbstractAnnotationAwareTransactionalTests.java:150)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runBare(AbstractAnnotationAwareTransactionalTests.java:109)
at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:174)
at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:254)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: unexpected char: '@' [from no.elo.product.tipsystem.domain.UserProfile u where u.email = john.doe@email.com]
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:616)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:95)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.JpaTemplate$CloseSuppressingInvocationHandler.invoke(JpaTemplate.java:394)
at $Proxy13.createQuery(Unknown Source)
at org.springframework.orm.jpa.JpaTemplate$9.doInJpa(JpaTemplate.java:301)
at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:185)
... 29 more
Caused by: org.hibernate.QueryException: unexpected char: '@' [from no.elo.product.tipsystem.domain.UserProfile u where u.email = john.doe@email.com]
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:204)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:92)
... 37 more
I guess there is a simple solution to this.
Can someone help me?
Best regards
Per-Jarle