Detach an entity from JPA/EJB3 persistence context

后端 未结 14 2111
面向向阳花
面向向阳花 2020-12-13 03:26

What would be the easiest way to detach a specific JPA Entity Bean that was acquired through an EntityManager. Alternatively, could I have a query return detached objects in

14条回答
  •  既然无缘
    2020-12-13 04:17

    Since I am using SEAM and JPA 1.0 and my system has a fuctinality that needs to log all fields changes, i have created an value object or data transfer object if same fields of the entity that needs to be logged. The constructor of the new pojo is:

        public DocumentoAntigoDTO(Documento documentoAtual) {
        Method[] metodosDocumento = Documento.class.getMethods();
        for(Method metodo:metodosDocumento){
            if(metodo.getName().contains("get")){
                try {
                    Object resultadoInvoke = metodo.invoke(documentoAtual,null);
                    Method[] metodosDocumentoAntigo = DocumentoAntigoDTO.class.getMethods();
                    for(Method metodoAntigo : metodosDocumentoAntigo){
                        String metodSetName = "set" + metodo.getName().substring(3);
                        if(metodoAntigo.getName().equals(metodSetName)){
                            metodoAntigo.invoke(this, resultadoInvoke);
                        }
                    }
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

提交回复
热议问题