Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Object: [some.class@af683a] is not a known entity type

Hi all,

My name is Wellington, I'm brazilian and I'm creating a poll application in pure JPA with Eclipselink, that has at the moment 2 entities in a quite trivial One-to-many relationship: Enquete holds the one question, EnqueteResposta holds one of the many possible answers. Here's the scenario:

1. I've setup a pure JPA project with Entities and Helper classes. The entities are created from pre-existant tables in a MySQL database by Eclipse Dali I wrote no code but a Cascade.ALL in the master class.

2. The main Helper class is a CRUDAdmin that takes whatever class through Java Generics and applies JPA operations on them coded as follows:

package com.amb.ops;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

@SuppressWarnings("unchecked")
public class CRUDAdm {

        private static EntityManager getEntityManager() {
            EntityManager em =  EntityManagerFactoryHelper.getFactory().createEntityManager();
            return em;
        } 
     
        public static <T> T create(T entity) throws Exception { 
            EntityManager em = getEntityManager(); 
            Exception e = null; 
            T retornoEntity = null;      
            EntityTransaction t = em.getTransaction(); 
            try { 
                t.begin(); 
                retornoEntity = em.merge(entity); 
                t.commit(); 
            } catch (Exception ex) { 
                if (t.isActive()) { 
                    em.getTransaction().rollback(); 
                } 
                e = ex; 
            } 
            em.close();
            if (e != null) { 
                throw e; 
            } 
            return retornoEntity; 
        } 
     
        public static <T> T update(T entity) throws Exception { 
            EntityManager em = getEntityManager(); 
            Exception e = null; 
            T retornoEntity = null; 
            EntityTransaction t = em.getTransaction(); 
            try { 
                t.begin(); 
                retornoEntity = em.merge(entity); 
                t.commit(); 
            } catch (Exception ex) { 
                if (t.isActive()) { 
                    t.rollback(); 
                } 
                e = ex; 
            } 
            em.close();
            if (e != null) { 
                throw e; 
            } 
            return retornoEntity; 
        } 
     
        public static <T> void delete(T entity) throws Exception { 
            EntityManager em = getEntityManager(); 
            Exception e = null; 
            EntityTransaction t = em.getTransaction(); 
            try { 
                t.begin(); 
                entity = em.merge(entity); 
                em.remove(entity); 
                t.commit(); 
            } catch (Exception ex) { 
                if (t.isActive()) { 
                    t.rollback(); 
                } 
                e = ex; 
            } 
            em.close();
            if (e != null) { 
                throw e; 
            } 
        } 
     
        public static <T> T retrieve(Class<T> entityClass, Object id) throws Exception { 
            EntityManager em = getEntityManager(); 
            Exception e = null; 
            T retorno = null; 
            EntityTransaction t = em.getTransaction(); 
            try { 
                t.begin(); 
                retorno = em.find(entityClass, id); 
                t.commit(); 
            } catch (Exception ex) { 
                if (t.isActive()) { 
                    t.rollback(); 
                } 
                e = ex; 
            } 
            em.close();
            if (e != null) { 
                throw e; 
            } 
            return retorno; 
        } 
     
        public static <T> List<T> listAll(Class<T> entityClass, String... orderbys) throws Exception { 
            EntityManager em = getEntityManager(); 
            List<T> retorno = null; 
            try { 
                String query = "select o from " + entityClass.getSimpleName() + "  o  order by "; 
     
                for (String order : orderbys) { 
                    query += order + " , "; 
                } 
                query = query.substring(0, query.length() - 2); 
                Query q = em.createQuery(query); 
                retorno = q.getResultList(); 
            } catch (Exception ex) { 
                ex.printStackTrace(); 
            } 
            em.close();
            return retorno; 
        } 
     
        public static <T> List<T> listAll(Class<T> entityClass) throws Exception { 
            EntityManager em = getEntityManager(); 
            List<T> retorno = null; 
            try {            
                String query = "select o from " + entityClass.getSimpleName() + "  o "; 
                Query q = em.createQuery(query);             
                retorno = q.getResultList(); 
            } catch (Exception ex) { 
                ex.printStackTrace(); 
            } 
            em.close();
            return retorno; 
        } 
     
        public static <T> List<T> listByProperty(Class<T> entityClass, String propertyName, Object propertyValue) throws Exception { 
            EntityManager em = getEntityManager(); 
            List<T> retorno = null; 
            try { 
                String query = "select o from " + entityClass.getSimpleName() + "  o  where o." + propertyName + "  = :propertyValue "; 
                Query q = em.createQuery(query).setParameter("propertyValue", propertyValue); 
     
                retorno = q.getResultList(); 
            } catch (Exception ex) { 
                ex.printStackTrace(); 
            } 
            em.close();
            return retorno; 
        } 
     
                                                                         
     
        public static <T> List<T> listByProperty(Class<T> entityClass, String propertyName, Object propertyValue, String operador, String... orderbys) throws Exception { 
            EntityManager em = getEntityManager(); 
            List<T> retorno = null; 
            try { 
                String query = null; 
                if (!operador.equalsIgnoreCase("like")) { 
                    query = "select o from " + entityClass.getSimpleName() + "  o  where o." + propertyName + "  " + operador + " :propertyValue "; 
                } else { 
                    query = "select o from " + entityClass.getSimpleName() + "  o  where upper(o." + propertyName + ")  " + operador + " upper(:propertyValue) "; 
                } 
     
                if (orderbys != null && orderbys.length > 0) { 
                    query+= " order by "; 
                    for (String order : orderbys) { 
                        query += "o." + order + " , "; 
                    } 
                    query = query.substring(0, query.length() - 2); 
                } 
     
                Query q = em.createQuery(query).setParameter("propertyValue", propertyValue); 
     
                retorno = q.getResultList(); 
            } catch (Exception ex) { 
                ex.printStackTrace(); 
            } 
            em.close();
            return retorno; 
        } 
     
        public static <T> List<T> listByProperty(Class<T> entityClass, String propertyName, Object propertyValue, String operador) throws Exception { 
            EntityManager em = getEntityManager(); 
            List<T> retorno = null; 
            try { 
                String query = "select o from " + entityClass.getSimpleName() + "  o  where o." + propertyName + "  " + operador + " :propertyValue "; 
                Query q = em.createQuery(query).setParameter("propertyValue", propertyValue); 
 
                retorno = q.getResultList(); 
            } catch (Exception ex) { 
                ex.printStackTrace(); 
            } 
            em.close();
            return retorno; 
        }   
}

3. The EntityManagerHelper class is a singleton coded as suggested in the EclipseLink docs somewhere, so, it's a singleton to the factory class.

4. Having that I packaged the classes in a jar file and used the jar file (now containing the full MODEL of the application with all db operations fully encapsulated) in the client Struts 2 app. The idea is to create the MODEL classes incrementally and adding actions as new MODEL jars arrive.

5. The struts app calls an action to update a value in the slave class EnqueteResposta, inside master class Enquete, as follows:

    public String votarEnqueteAtiva() throws Exception {
        System.out.println("inicio da votarEnqueteAtiva");
        List<Enquete> le = CRUDAdm.listByProperty(Enquete.class, "ativa", new Integer(1));
        if (le.size() == 1) {
            Enquete e = le.get(0);
            List<EnqueteResposta> resps = e.getEnqueteRespostas();
            Iterator<EnqueteResposta> i = resps.iterator();
            while (i.hasNext()) {
                EnqueteResposta er = i.next();
                if (er.getId() == voto) er.setTotal(er.getTotal() + 1);
            }
            CRUDAdm.update(le);
            eab = new EnqueteAtivaBean();
            eab.setPergunta(e.getQuestao());
            List<String> re = new LinkedList<String>();
            List<String> pe = new LinkedList<String>();
            i = resps.iterator();
            int totalGeral = 0;
            while (i.hasNext()) {
                EnqueteResposta er = i.next();
                re.add(er.getResposta());
                totalGeral += er.getTotal();                
            }
            eab.setRespostas(re);
            i = resps.iterator();
            while (i.hasNext()) {
                EnqueteResposta er = i.next();
                pe.add(""+er.getTotal()*100/totalGeral);
            }
            eab.setPercents(pe);
            HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
            HttpSession session = request.getSession();
            session.setAttribute("enqueteAtiva",eab);
        }
        System.out.println("fim da votarEnqueteAtiva");
        return NONE;
    }
 
Then I get the StackTrace below:

java.lang.IllegalArgumentException: Object: [com.amb.entidades.Enquete@af683a] is not a known entity type.
    org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeCloneWithReferences(UnitOfWorkImpl.java:3456)
    org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.mergeCloneWithReferences(RepeatableWriteUnitOfWork.java:363)
    org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeCloneWithReferences(UnitOfWorkImpl.java:3427)
    org.eclipse.persistence.internal.jpa.EntityManagerImpl.mergeInternal(EntityManagerImpl.java:452)
    org.eclipse.persistence.internal.jpa.EntityManagerImpl.merge(EntityManagerImpl.java:429)
    com.amb.ops.CRUDAdm.update(CRUDAdm.java:46)
    com.hoog.acoes.EnqueteAtiva.votarEnqueteAtiva(EnqueteAtiva.java:59)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


Now WHAT CAN THAT BE? Do you guys need any other info to analyze this issue?

The CRUDAdmin method to list the Enquete is working quite nicely.

Any help will be appreciated.

Cheers all,

Wellington

Back to the top