Named query
clxy May 20, 2009 8:59 AMI hit Named query not known
error.
The exceptiion is:
javax.el.ELException: javax.persistence.PersistenceException: org.hibernate.MappingException: Named query not known: user.login
My entitiy is
@Entity @NamedQueries({ @NamedQuery( name = "user.login", query = "SELECT u FROM User u WHERE u.email = :email and u.password = :password") }) public class User implements Serializable { ... }
My service code is
public class UserServiceJPAImpl implements UserService { private EntityManager em; public void setEntityManager(EntityManager em) { this.em = em; } public User login(String id, String password) { Query query = em.createNamedQuery("user.login"); ... } }
at components.xml, cofigured like :
<component name="userService" auto-create="true" class="foo.bar.UserServiceJPAImpl"> <property name="entityManager">#{em}</property> </component>
My test code is:
@Name("authenticator") public class AuthAction { @In Identity identity; @In UserService userService; @In(create = true) TestService testService; @Out(required = false, scope = SESSION) User user; public boolean authenticate() { testService.doSomething();// configured by annotation.can work! String userName = identity.getCredentials().getUsername(); String password = identity.getCredentials().getPassword(); User user = userService.login(userName, password);//can not work! return user != null; } }
The exception occurred when
em.createNamedQuery("user.login")
and, i take another try
Test entity
@Entity @NamedQueries( { @NamedQuery(name = "test.sql", query = "select u from TestEntity u ") }) public class TestEntity { ... }
Test service code.
@Name("testService") public class TestService { @In private EntityManager em; public void doSomething() { Query q = em.createNamedQuery("test.sql"); q.getResultList(); } }
The different is EntityManager configured by annotation not components.xml as you can see.
And test code can work well.
so, how to make EntityManager configured by components.xml work?
thanks