The Annotation @Persistencecontext is disallowed for this Location

Asked

Viewed 122 times

1

I’m getting the message "The Annotation @Persistencecontext is disallowed for this Location" when I try to put this annotation in the code, someone knows how I can fix it?

@Stateless
public class ReajusteManager  {

public List<PlanoSaudeDTO> listarPlanoSaude() {


    @PersistenceContext //Local do erro
    EntityManager entityManager;


    @SuppressWarnings("unchecked")
    List<Object[]> listaValores = (List<Object[]>) entityManager.createNativeQuery(
    "SELECT F.NM_FUNCIONARIO\r\n" + 
    "FROM DBO_DB_RH.VW_funcionario F \r\n" + 
    "Where  rownum <30 \r\n" + 
    "Order by F.NM_FUNCIONARIO").getResultList();

    List<PlanoSaudeDTO> listaPlanoSaudeDTO = new ArrayList<PlanoSaudeDTO>();
    System.out.println("aqui");

    for (Object[] obj : listaValores) {
        PlanoSaudeDTO plano = new PlanoSaudeDTO();

        plano.setNomeFuncionario(String.valueOf(obj[0]));

        listaPlanoSaudeDTO.add(plano);
    }

    return listaPlanoSaudeDTO;

}


}

1 answer

1


This annotation is not suitable for local variables within the methods. Serves only for instance variables, which are outside the methods and within the class.

Do so:

@Stateless
public class ReajusteManager  {

    @PersistenceContext
    private EntityManager entityManager;

    private static final String LISTA_FUNCIONARIOS_SQL = 
            "SELECT F.NM_FUNCIONARIO \r\n" + 
            "FROM DBO_DB_RH.VW_funcionario F \r\n" + 
            "WHERE rownum <30 \r\n" + 
            "ORDER BY F.NM_FUNCIONARIO"

    public List<PlanoSaudeDTO> listarPlanoSaude() {

        @SuppressWarnings("unchecked")
        List<Object[]> listaValores = (List<Object[]>)
                entityManager.createNativeQuery(LISTA_FUNCIONARIOS_SQL).getResultList();

        List<PlanoSaudeDTO> listaPlanoSaudeDTO = new ArrayList<PlanoSaudeDTO>();
        System.out.println("aqui");

        for (Object[] obj : listaValores) {
            PlanoSaudeDTO plano = new PlanoSaudeDTO();
            plano.setNomeFuncionario(String.valueOf(obj[0]));
            listaPlanoSaudeDTO.add(plano);
        }

        return listaPlanoSaudeDTO;
    }
}

I do not guarantee that there are no other errors. But at least the problem you report should be fixed.

  • That’s right! Thank you!

  • @B.Fernandes If this answer solved your problem and there is no doubt left, mark it as accepted by clicking on the left side of the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.