em.createQuery keeps returning null

Posted by Developer106 on Stack Overflow See other posts from Stack Overflow or by Developer106
Published on 2012-06-23T20:27:24Z Indexed on 2012/06/23 21:16 UTC
Read the original article Hit count: 206

Filed under:
|
|

I have this application which i use JSF 2.0 and EclipseLink, i have entities created for a database made in MySQL, Created these entities using netbeans 7.1.2, it gets created automaticly.

Then i use session beans to work with these entities, the thing is the em.createQuery always returns a null, though I checked NamedQueries in the entities and they perfectly match

a sample from the entities named queries:-

@NamedQueries({
    @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
    @NamedQuery(name = "Users.findByUserId", query = "SELECT u FROM Users u WHERE u.userId = :userId"),
    @NamedQuery(name = "Users.findByUsername", query = "SELECT u FROM Users u WHERE u.username = :username"),
    @NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email"),

notice how i use this findByEmail query in the session bean :-

public Users findByEmail(String email){
        em.getTransaction().begin();
        String find = "Users.findByEmail";
        Query query = em.createNamedQuery(find);
        query.setParameter("email", email);
        Users user = (Users) query.getSingleResult();

but it always returns null from this em.createNamedQuery, i tried using .createQuery first but it also was no good.

the stacktrace of the exception

Caused by: java.lang.NullPointerException
    at com.readme.entities.sessionBeans.UsersFacade.findByEmail(UsersFacade.java:48)
    at com.readme.user.signup.SignupBean.checkAvailability(SignupBean.java:137)
    at com.readme.user.signup.SignupBean.save(SignupBean.java:146)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)

What Seems To Be The Problem Here ?

© Stack Overflow or respective owner

Related posts about java

Related posts about jpa