Domanda

I have entity class User

@Entity(name = "user")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@Column(unique = true, nullable = false)
private String email;

  /** skipped **/

And stateless EJB DAO class(I am using jpa/hibernate) with method

@Override
public User findByEmail(String name) {
    TypedQuery<User> query = entityManager.createQuery("from User where email= :email", User.class);
    query.setParameter("email", name);
    return query.getSingleResult();
}

When I execute this method from page(using jsf), it works fine. The problem occurse when I execute this method from WebService EJB bean. I've got following exception

org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [from User where email= :email] 

My web service is very simple:

@Stateless(name = "webServiceBean")
@WebService(serviceName = "webService")
public class MyWebService{

@EJB
private UserDao dao;

public String findUser(String email) {
    return dao.findByEmail(email);
}

}

Application is deployed on JBoss7. I am using EJB 3.1 and hibernate 4.3 Does anybody knows what is the reason of this exception? And why it occurs only in WebService?

È stato utile?

Soluzione

  1. Make sure User, along with the package name, is listed in hibernate config file.
  2. Either use just @Entity or @Entity(name = "User"). You have named your entity as user but in your query trying to refer to it as User.

Check the docs for the @Entity's name attribute.

public abstract java.lang.String name

(Optional) The entity name. Defaults to the unqualified name of the entity class. This name is used to refer to the entity in queries. The name must not be a reserved literal in the Java Persistence query language.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top