title: java.lang.ClassCastException with the same class object
body: <p>This piece of code irritates me, 
  sometimes it working and some other times it doesn't !</p>
  
  <blockquote>
    <p>The NamedQuery : (name = "User.findByLogin", query = "SELECT u
    FROM User u WHERE u.login = :login")</p>
  </blockquote>
  
  <pre><code>public User findByLogin(String login) {
          Query query = em.createNamedQuery("User.findByLogin");
          query.setParameter("login", login);
          try {
              return (User) query.getSingleResult();
          } catch (javax.persistence.NoResultException ex) {
              return null;
          }
      }
  </code></pre>
  
  <p>The error make me crazy !</p>
  
  <p>Avertissement: EJB5184:<strong>A system exception occurred during an invocation on EJB UserFacade</strong>, method: public dz.admin.entity.User dz.admin.service.UserFacade.findByLogin(java.lang.String)
  Avertissement: javax.ejb.EJBException
  .... <strong>Caused by: java.lang.ClassCastException: dz.elit.admin.entity.User cannot be cast to dz.elit.admin.entity.User</strong> at dz.elit.admin.service.UserFacade.findByLogin(UserFacade.java:45)</p>