Detach an entity from JPA/EJB3 persistence context

后端 未结 14 2109
面向向阳花
面向向阳花 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:11

    As far as I know, the only direct ways to do it are:

    1. Commit the txn - Probably not a reasonable option
    2. Clear the Persistence Context - EntityManager.clear() - This is brutal, but would clear it out
    3. Copy the object - Most of the time your JPA objects are serializable, so this should be easy (if not particularly efficient).
    0 讨论(0)
  • 2020-12-13 04:11

    Do deal with a similar case I have created a DTO object that extends the persistent entity object as follows:

    class MyEntity
    {
       public static class MyEntityDO extends MyEntity {}
    
    }
    

    Finally, an scalar query will retrieve the desired non managed attributes:

    (Hibernate) select p.id, p.name from MyEntity P
    (JPA)       select new MyEntity(p.id, p.name) from myEntity P
    
    0 讨论(0)
  • 2020-12-13 04:14

    I think you can also use method EntityManager.refresh(Object o) if primary key of the entity has not been changed. This method will restore original state of the entity.

    0 讨论(0)
  • 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();
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-13 04:19

    If there aren't too many properties in the bean, you might just create a new instance and set all of its properties manually from the persisted bean.

    This could be implemented as a copy constructor, for example:

    public Thing(Thing oldBean) {
      this.setPropertyOne(oldBean.getPropertyOne());
      // and so on
    }
    

    Then:

    Thing newBean = new Thing(oldBean);
    
    0 讨论(0)
  • 2020-12-13 04:20

    If you get here because you actually want to pass an entity across a remote boundary then you just put some code in to fool the hibernazi.

    for(RssItem i : result.getChannel().getItem()){
    }
    

    Cloneable wont work because it actually copies the PersistantBag across.

    And forget about using serializable and bytearray streams and piped streams. creating threads to avoid deadlocks kills the entire concept.

    0 讨论(0)
提交回复
热议问题