Pessoal, estou com um problema, já faz alguns dias que estou pesquisando em fóruns, mas não encontrei nenhuma solução para o problema que vou explicar:
Tenho uma classe que realiza o update de uma entidade atravéz do merge do jpa, o método executeOperation recebe a entidade já alterada e o EntityManager, e realiza o merge.
[code]public class UpdatePatient implements CaseRules{
@Override
public ResponseExecution executeOperation(Object entity, EntityManager em) throws BaseException {
Patient patient = (Patient) entity;
ResponseExecution response = new ResponseExecution();
em.merge(patient);
...
[/code]
Tenho uma classe cujo o método preUpdate é anotado com @PreUpdate, o método faz a chamada para uma classe ‘especialista’ que faz a verificação de duplicidade:
public class PatientListener {
private PatientServicesLocal patientService;
@PrePersist
public void prePersist(Patient patient) throws BaseException {
...
}
@PreUpdate
public void preUpdate(Patient patient) throws BaseException {
//Realiza a chamada da classe responsável pela consulta...
patientService = (PatientServicesLocal) ServicesProviderByLookup
.getServicesByClassServices(PatientService.class);
patientService.execute(Constants.VERIFY_EXISTS_PATIENT_RULE, patient);
}
@PreRemove
public void preRemove(Patient patient) throws BaseException {
...
}
}
A Classe acima (PatientListener) no método preUpdate faz a chamada da classe responsável pela consulta:
public class VerifyExistsPatient implements CaseRules {
@Override
public ResponseExecution executeOperation(Object entity, EntityManager em)
throws BaseException {
ResponseExecution response = new ResponseExecution();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Patient> cq = builder.createQuery(Patient.class);
Root<Patient> patientRoot = cq.from(Patient.class);
List<Predicate> predicate = new ArrayList<Predicate>();
// Obtém por reflection os atributos que foram preenchidos em tela
ReflectionUtil ru = new ReflectionUtil(entity);
Map<Object, Object> filledAttrs = ru.getFilledAttributes();
// Preenchendo dinamicamente as condições da consulta
for (Object key : filledAttrs.keySet()) {
predicate.add(builder.equal(patientRoot.get(key.toString()),
filledAttrs.get(key)));
}
cq.where(predicate.toArray(new Predicate[predicate.size()]));
List<Patient> result = em.createQuery(cq).getResultList();
...
O problema que estou tendo, é que no momento que o listener é acionado, o método com a anotação @PreUpdate executa o código acima (VerifyExistsPatient), e no momento em que o em.createQuery(cq).getResultList() é executado, meu código entra em um “loop infinito” e fica executando sem parar o PreUpdate e consequentemente a classe VerifyExistsPatient.
Alguém já passou por isso, ou sabe o que está acontecendo ??