1 Reply Latest reply on Dec 5, 2012 1:31 AM by klauskinski

    <Classname> is not mapped [SELECT ...

    klauskinski Newbie

      Hi,

      I'm trying to retrieve an "Users" object from my database querying by username/password.

      I have the following Entity:

       

      @Entity(name = "users")

      public class User  implements Serializable {

          private static final long    serialVersionUID    = 7898904802394084838L;

          @Id

          private long    userid;

          private String    name;

          private String    password;

          @Column(name = "att_new", columnDefinition = "TINYINT(1)")

          private boolean    attNew;

          @Column(name = "perm_login", columnDefinition = "TINYINT(1)")

          private boolean    permLogin;

          @Column(name = "perm_admin", columnDefinition = "TINYINT(1)")

          private boolean    permAdmin;

       

      I have the following Object trying to retrieve a User object:

       

      @Stateless

      public class UserBean implements UserIf {

       

          @PersistenceContext(unitName = "JPADB")

          private EntityManager entityManager;

       

          public UserBean() {   }

       

          public void setEntityManager(EntityManager entityManager) {

              this.entityManager = entityManager;

          }

       

          @Override

          public User findUserByUsernamePassword(User user) {

              String q = "SELECT p from User p WHERE p.name = :name AND p.password = SHA1(:pass)";

              Query query = entityManager.createQuery(q);

      //        String q = "SELECT * from users as p WHERE p.name = ? AND p.password = SHA1(?)";

      //        Query query = entityManager.createNativeQuery(q,User.class);

              query.setParameter( "name", user.getName() );

              query.setParameter( "pass", user.getPassword() );

              User u = (User) query.getSingleResult();

              return u;

          }

      }

       

      When I now call findUserByUsernamePassword from inside an EJB I'm getting:

       

      Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [SELECT p from User p WHERE p.name = :name AND p.password = SHA1(:pass)]

       

      What is causing the error here?

       

      Remark: UserBean is not used as an EJB! It is instantiated in an EJB and called from there (after injecting entityManager).

        • 1. Re: <Classname> is not mapped [SELECT ...
          klauskinski Newbie

          The Class User itself seems not to be problematic I guess, this method in UserBean for example works without problems and returns the correct User object:

           

              @Override
              public User findUserById(User user) {
                  User u = entityManager.find(User.class, user.getUserid());
                  return u;
              }

           

          Edit:

           

          This one works now:

           

          @Override

          public User findUserByUsernamePassword(User user) {

              String q = "SELECT p from " + User.class.getName() + " p WHERE p.name = :name AND p.password = SHA1(:pass)";

              Query query = entityManager.createQuery(q);

              query.setParameter("name", user.getName());

              query.setParameter("pass", user.getPassword());

              User u = (User) query.getSingleResult();

              return u;

          }

           

          I had to use User.class.getName() instead of "User" for whatever reason.

           

          But: it only works if the User Entity is included in the deployed JAR file an not loaded from an external JAR file. When I load it from an external JAR file, I get the error message:

           

          Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: could not locate named parameter [name]

           

          When I disable my jboss-deployment-structure.xml (which loads the external JAR as a dependency module) and move the entity package back into my JBoss deployment, it works.... why?