0
votes

I have a problem with my service: DAO:

   public Oficina find(String codigoDir3) {
    try{
        // crear la query
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Oficina> cq = cb.createQuery(Oficina.class);

        // configurar la query
        Root<Oficina> from = cq.from(Oficina.class);
        cq.where(cb.equal(from.get("codigo"), codigoDir3));
        cq.orderBy(cb.asc(from.get("codigo")));

        // obtener los datos
        TypedQuery<Oficina> ctq = getEntityManager().createQuery(cq);
        Oficina oficina = ctq.getSingleResult();
        return oficina;
    }catch(Exception ex){
        LOGGER.error("Clase: OficinaJpaDao, Metodo: find(" + codigoDir3 + "), Error:" + ex.getMessage() + " " + ex.getCause() );
        return null;
    }
}

SERVICE with @Service and @Transactional

@Override
public List<UnidadOrganicaDTO> getUnidadesOrganicas(String filtro){
    List<UnidadOrganica> unidadesOrganicas = new ArrayList<UnidadOrganica>();
    List<UnidadOrganicaDTO> unidadesOrganicasDto = new ArrayList<UnidadOrganicaDTO>();
    try{
        unidadesOrganicas = unidadOrganicaDao.findAll(filtro);
        for(UnidadOrganica uo: unidadesOrganicas){
            unidadesOrganicasDto.add(utils.uoentityToDto(uo));
        }
    }catch(Exception ex){
        log.error("AMAPDir3ServiceImpl.getUnidadesOrganicas("+filtro+"): " + ex.getMessage());
    }
    return unidadesOrganicasDto;
}

RESTFUL

@GET  
@Produces("application/json")  
@Path("/getOficina/{codigoDir3}")
public String getOficina(
    @HeaderParam("Authorization") String authorization,
    @PathParam("codigoDir3") @DefaultValue("") String codigoDir3) {

    String jsonString;

    try{

        jsonString = toJSON(servicio.getOficina(codigoDir3));

        AuditarServicio(authorization,"AMAPGENERICOS.getOficina"); 

    }catch(Exception ex){
        // registrar el error y devolver vacio
        logger.error("Dir3RestfulImpl.getOficina("+codigoDir3+"): " + ex.getLocalizedMessage());
        jsonString = JSON_EMPTY;
    }

    return jsonString; 
}

Entity Oficina have your atributes with @ManyToOne(fetch = FetchType.LAZY) And in applicationContext:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

The service deploy buy when I make a consult, it return a error could not initialize proxy - no Session (through reference chain: Thanks for your help

ERROR [Dir3RestfulImpl] Dir3RestfulImpl.getOficina(): could not initialize proxy - no Session (through reference chain: es.gobcantabria.amap.dir3.servidor.business.dto.OficinaDTO["ccaa"]->es.gobcantabria.amap.dir3.servidor.business.domain.Ccaa_$$_jvstba4_c["codIne"])
1

1 Answers

0
votes

Where does the exception comes from? Could you post the stacktrace please?

Maybe you are accessing a lazy property from the REST layer, which fail since the transaction (and Hibernate session) are committed/closed.

Any access to the persistent entity should be made from the service layer (that means, initializing lazy collections). The REST layer should only work with detached entities, with each and every data already loaded.