Search code examples
javajpajta

Caused by: org.hibernate.MappingException: Unknown entity: New Object. JTA


Due the complexity of my dataBase, i decided create a new Object where I specifying just the parameters that I will to use. To do custom query I'm using createNativeQuery in JPA. Following my Object.

POJO:

@Entity
public class Unidade implements Serializable{

    private static final long serialVersionUID = 1L;


    private Long numeroUnidade;
    private String nomeUnidade;
    private Integer numeroCep;
    private Integer numeroCepComplemento;
    private String tipoServico;
    private Integer status;
    private String logradouro;
    private String bairro;
    private String cidade;
    private String uf;
    private Integer statusVinculo; 

   //getter and setters

Currently I'm using the following select to return the simple integer and works without show any problem.

@PersistenceContext(unitName="projetoAtividade")
private EntityManager em;

public void selectTeste(){
        int quantidade = (Integer) em.createNativeQuery("select count(*) from abc.nattbu29_escritorio;").getSingleResult();
        System.out.println("========== QUANTIDADE DE REGISTRO NA TABELA ===========");
        System.out.println(quantidade);
    }

Although the previous query worked fine, when a submit a complex query putting the respective alias, using too createNativeQuery and putting the object return, the hibernate throw a exception (org.hibernate.MappingException: Unknown).

Remembering that I create a new class putting @Entity specifying.

Query:

@SuppressWarnings("unchecked")
        public List<UnidadeTeste> buscarUnidadeGenerica(int numeroCeps, int numeroComplementoCeps){
            List<UnidadeTeste> lsita = new ArrayList<UnidadeTeste>();
            String query = "SELECT "
                    +"TBZ21.NATURAL as numeroPv, "
                    +"TBZ21.UNIDADE as nomePv, "
                    +"TBZ21.SITUACAO as statusVinculacao, "
                    +"TBJ55.TPO_ATUACAO_U44 as tipoServAZW, "
                    +"TBJ55.SITUACAO_ATUAL as status, "
                    +"TBH18.LOCALIDADE as cidade, "
                    +"'' as bairro, "
                    +"'' as logradouro, "
                    +"TBH18.CEP as numeroCep, "
                    +"TBH18.CEP_COMPLEMENTO as numeroCepComplemento, "
                    +"TBH18.FK_AZWVWL22_UNISG as uf "
                    +"FROM AZW.AZWTBJ55_RGATUACAO TBJ55 "
                    +"LEFT JOIN AZW.AZWTBH18_LOCALIDADE TBH18 ON "
                    +"TBH18.LOCALIDADEE = TBJ55.LOCALIDADEE "
                    +"LEFT JOIN AZW.AZWTBZ21_UNIDADE TBZ21 ON "
                    +"TBJ55.NATURAL = TBZ21.NATURAL "
                    +"WHERE TBH18.CEP = ?1 "
                        +"AND TBH18.CEP_COMPLEMENTO = ?2; "

            lsita = em.createNativeQuery(query, Unidade.class)
                    .setParameter(1, numeroCeps)
                    .setParameter(2, numeroComplementoCeps)
                    .getResultList();
            System.out.println(lsita.size()+"<<<<=====================");
            return lsita;
        }

Error:

Caused by: org.hibernate.MappingException: Unknown entity: br.com.project.modelo.UnidadeTeste
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1031) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.getSQLLoadable(SQLQueryReturnProcessor.java:336) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processRootReturn(SQLQueryReturnProcessor.java:377) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:356) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:172) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:87) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.engine.query.spi.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:67) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.engine.query.spi.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:155) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.internal.AbstractSessionImpl.getNativeSQLQueryPlan(AbstractSessionImpl.java:218) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:224) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:156) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:252) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    ... 84 more

In my search through the internet, almost all forum topics was unanimous in the solution answer: " Put the @Entity before class declaration" but, it not works to me.

I had "spent" all my day trying looking for a solutions worked but, unfortunately, I not had successes. Someone can say a new way to solve my problem? I tested in 2 different project and hapens the same problem.

Question complement:

Folks, I tried create a @SqlResultSetMapping into my class, the coding following, but now, the hibernate trows the another exception:

 ERROR [org.jboss.ejb3.invocation] (http--127.0.0.1-8888-2) JBAS014134: EJB Invocation failed on component AtividadeEJB for method public void br.com.project.jboss.modelo.AtividadeEJB.testeChamada(): javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown SqlResultSetMapping [UnidadeResult]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:166) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:230) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:304) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

My new POJO:

@Entity
@SqlResultSetMapping(name="UnidadeResult", 
entities={ 
    @EntityResult(entityClass=UnidadeTeste.class, fields={
        @FieldResult(name="tx_descricao", column="nomePv"),
        @FieldResult(name="nu_idntr_pessoa", column="id")})},
columns={
    @ColumnResult(name="nomePv")}
)


public class UnidadeTeste implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long id;

    private String nomePv;

    public String getNomePv() {
        return nomePv;
    }

    public void setNomePv(String nomePv) {
        this.nomePv = nomePv;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

My EJB:

@SuppressWarnings("unchecked")
    public void testeChamada(){
        List<Object> lista = em.createNativeQuery("SELECT nu_idntr_pessoa as id, email as nomePv FROM pessoa;", "UnidadeResult").getResultList();
        System.out.println("===============>>"+lista.size());
    }

Solution

  • if you want to return a native query's results to an object I would suggest using SqlResultSetMapping.

    Otherwise, set your return type to:

    List<Object[]> results = em.createNativeQuery(query)
                    .setParameter(1, numeroCeps)
                    .setParameter(2, numeroComplementoCeps)
                    .getResultList();
    

    And then loop through the result list, and cast each member of the Object array to its data type.