6 Replies Latest reply: Aug 11, 2010 2:03 PM by 843833 RSS

    IllegalArgumentException: Unknown entity bean class

    843833
      Hi. I try to make a connection with mysql database by using entity classes which i create via Netbeans' tools. But when i try to use the create method for entity persist, in the entity controller class, i get an error message like that;

      (The User class is marked with the @Entity annonation)


      java.lang.IllegalArgumentException: Unknown entity bean class: class entity.User, please verify that this class has been marked with the @Entity annotation.
      at org.eclipse.persistence.internal.jpa.EntityManagerImpl.find(EntityManagerImpl.java:576)
      at org.eclipse.persistence.internal.jpa.EntityManagerImpl.find(EntityManagerImpl.java:460)
      at controller.UserJpaController.findUser(UserJpaController.java:125)
      at controller.UserJpaController.create(UserJpaController.java:43)
      at bean.UserBean.save(UserBean.java:115)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
      at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
      at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
      at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
      at javax.faces.component.UICommand.broadcast(UICommand.java:315)
      at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
      at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
      at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
      at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
      at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
      at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
      at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
      at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
      at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
      at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
      at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
      at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
      at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
      at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
      at java.lang.Thread.run(Thread.java:619)


      i use glass fish v3

      persistence.xml is automaticaly created by netbeans.

      Please help.

      Edited by: MOD on Aug 5, 2010 9:41 AM
        • 2. Re: IllegalArgumentException: Unknown entity bean class
          843833
          Can you post your entity class here so we can see it?
          • 3. Re: IllegalArgumentException: Unknown entity bean class
            843833
            This is entity class which i created automaticly using Netbeans IDE tools;
            /*
             * To change this template, choose Tools | Templates
             * and open the template in the editor.
             */
            
            package entity;
            
            import java.io.Serializable;
            import javax.persistence.Basic;
            import javax.persistence.Column;
            import javax.persistence.Entity;
            import javax.persistence.Id;
            import javax.persistence.NamedQueries;
            import javax.persistence.NamedQuery;
            import javax.persistence.Table;
            
            /**
             *
             * @author PC
             */
            @Entity
            @Table(name = "users")
            @NamedQueries({
                @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
                @NamedQuery(name = "User.findByUsername", query = "SELECT u FROM User u WHERE u.username = :username"),
                @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password"),
                @NamedQuery(name = "User.findByName", query = "SELECT u FROM User u WHERE u.name = :name"),
                @NamedQuery(name = "User.findBySurname", query = "SELECT u FROM User u WHERE u.surname = :surname")})
            public class User implements Serializable {
                private static final long serialVersionUID = 1L;
                @Id
                @Basic(optional = false)
                @Column(name = "username")
                private String username;
                @Basic(optional = false)
                @Column(name = "password")
                private String password;
                @Basic(optional = false)
                @Column(name = "name")
                private String name;
                @Basic(optional = false)
                @Column(name = "surname")
                private String surname;
            
                public User() {
                }
            
                public User(String username) {
                    this.username = username;
                }
            
                public User(String username, String password, String name, String surname) {
                    this.username = username;
                    this.password = password;
                    this.name = name;
                    this.surname = surname;
                }
            
                public String getUsername() {
                    return username;
                }
            
                public void setUsername(String username) {
                    this.username = username;
                }
            
                public String getPassword() {
                    return password;
                }
            
                public void setPassword(String password) {
                    this.password = password;
                }
            
                public String getName() {
                    return name;
                }
            
                public void setName(String name) {
                    this.name = name;
                }
            
                public String getSurname() {
                    return surname;
                }
            
                public void setSurname(String surname) {
                    this.surname = surname;
                }
            
                @Override
                public int hashCode() {
                    int hash = 0;
                    hash += (username != null ? username.hashCode() : 0);
                    return hash;
                }
            
                @Override
                public boolean equals(Object object) {
                    // TODO: Warning - this method won't work in the case the id fields are not set
                    if (!(object instanceof User)) {
                        return false;
                    }
                    User other = (User) object;
                    if ((this.username == null && other.username != null) || (this.username != null && !this.username.equals(other.username))) {
                        return false;
                    }
                    return true;
                }
            
                @Override
                public String toString() {
                    return "entity.User[username=" + username + "]";
                }
            
            }
            • 4. Re: IllegalArgumentException: Unknown entity bean class
              843833
              And this is the JPAController class which i created automaticly using Netbeans IDE tools.
              /*
               * To change this template, choose Tools | Templates
               * and open the template in the editor.
               */
              
              package controller;
              
              import controller.exceptions.NonexistentEntityException;
              import controller.exceptions.PreexistingEntityException;
              import entity.User;
              import java.util.List;
              import javax.persistence.EntityManager;
              import javax.persistence.EntityManagerFactory;
              import javax.persistence.Persistence;
              import javax.persistence.Query;
              import javax.persistence.EntityNotFoundException;
              import javax.persistence.criteria.CriteriaQuery;
              import javax.persistence.criteria.Root;
              
              /**
               *
               * @author PC
               */
              public class UserJpaController {
              
                  public UserJpaController() {
                      emf = Persistence.createEntityManagerFactory("MyPersonelWebBlogPU");
                  }
                  private EntityManagerFactory emf = null;
              
                  public EntityManager getEntityManager() {
                      return emf.createEntityManager();
                  }
              
                  public void create(User user) throws PreexistingEntityException, Exception {
                      EntityManager em = null;
                      try {
                          em = getEntityManager();
                          em.getTransaction().begin();
                          em.persist(user);
                          em.getTransaction().commit();
                      } catch (Exception ex) {
                          if (findUser(user.getUsername()) != null) {
                              throw new PreexistingEntityException("User " + user + " already exists.", ex);
                          }
                          throw ex;
                      } finally {
                          if (em != null) {
                              em.close();
                          }
                      }
                  }
              
                  public void edit(User user) throws NonexistentEntityException, Exception {
                      EntityManager em = null;
                      try {
                          em = getEntityManager();
                          em.getTransaction().begin();
                          user = em.merge(user);
                          em.getTransaction().commit();
                      } catch (Exception ex) {
                          String msg = ex.getLocalizedMessage();
                          if (msg == null || msg.length() == 0) {
                              String id = user.getUsername();
                              if (findUser(id) == null) {
                                  throw new NonexistentEntityException("The user with id " + id + " no longer exists.");
                              }
                          }
                          throw ex;
                      } finally {
                          if (em != null) {
                              em.close();
                          }
                      }
                  }
              
                  public void destroy(String id) throws NonexistentEntityException {
                      EntityManager em = null;
                      try {
                          em = getEntityManager();
                          em.getTransaction().begin();
                          User user;
                          try {
                              user = em.getReference(User.class, id);
                              user.getUsername();
                          } catch (EntityNotFoundException enfe) {
                              throw new NonexistentEntityException("The user with id " + id + " no longer exists.", enfe);
                          }
                          em.remove(user);
                          em.getTransaction().commit();
                      } finally {
                          if (em != null) {
                              em.close();
                          }
                      }
                  }
              
                  public List<User> findUserEntities() {
                      return findUserEntities(true, -1, -1);
                  }
              
                  public List<User> findUserEntities(int maxResults, int firstResult) {
                      return findUserEntities(false, maxResults, firstResult);
                  }
              
                  private List<User> findUserEntities(boolean all, int maxResults, int firstResult) {
                      EntityManager em = getEntityManager();
                      try {
                          CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
                          cq.select(cq.from(User.class));
                          Query q = em.createQuery(cq);
                          if (!all) {
                              q.setMaxResults(maxResults);
                              q.setFirstResult(firstResult);
                          }
                          return q.getResultList();
                      } finally {
                          em.close();
                      }
                  }
              
                  public User findUser(String id) {
                      EntityManager em = getEntityManager();
                      try {
                          return em.find(User.class, id);
                      } finally {
                          em.close();
                      }
                  }
              
                  public int getUserCount() {
                      EntityManager em = getEntityManager();
                      try {
                          CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
                          Root<User> rt = cq.from(User.class);
                          cq.select(em.getCriteriaBuilder().count(rt));
                          Query q = em.createQuery(cq);
                          return ((Long) q.getSingleResult()).intValue();
                      } finally {
                          em.close();
                      }
                  }
              
              }
              When i instantiate this JPAController class and use its create method to persist a User it gives me the error which i posted in my first message.
              • 5. Re: IllegalArgumentException: Unknown entity bean class
                843833
                I dont know what i have done but it works now. I cant understand.

                I have just installed tomcat 6.0 to run my application in tomcat in the afternoon. And i had gotten the same error.

                But now i run my application on glass fish v3 and it works.

                Does that make any sense? Or must i have done something else which i can not realise now?

                Edited by: MOD on Aug 7, 2010 12:35 PM

                Edited by: MOD on Aug 7, 2010 12:39 PM
                • 6. Re: IllegalArgumentException: Unknown entity bean class
                  843833
                  &#305;t does not work again -.-