-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Using Session.replicate with differents sessions
PostPosted: Thu Jun 21, 2007 9:13 am 
Newbie

Joined: Thu Jun 21, 2007 7:41 am
Posts: 1
Hibernate version: 3.2.3

Mapping documents:
Code:
@MappedSuperclass
public abstract class AbstratoVO implements java.io.Serializable {
   
    @Id @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    protected String id;   
   
    public String getId() {
        return id;
    }
   
    public void setId(String id) {
        this.id = id;
    }

}


Code:
@Entity
@Table(name="PROJETO")
@AttributeOverride( name="id", column = @Column(name="PROJ_CD_PROJETO") )
public class ProjetoVO extends AbstratoVO implements Serializable {
   
    @Column(name="PROJ_NM_OFICIAL", length=200, nullable=false)
    private String nomeOficial;
   
    @Column(name="PROJ_NM_ABREVIADO", length=100, nullable=false)
    private String nomeAbreviado;
   
    @Column(name="PROJ_CD_ORCAMENTARIO", length=20)
    private String codigoOrcamentario;
   
    @Column(name="PROJ_IN_ORCAMENTARIO")
    private Boolean codigoSAP;
   
    @Column(name="PROJ_TX_OBSERVACAO")
    @Lob
    private String observacao;
   
    @Column(name="PROJ_DT_INICIAL", nullable=false)
    @Temporal(value=TemporalType.DATE)
    private Date dataInicial;
   
    @Column(name="PROJ_DT_FINAL", nullable=false)
    @Temporal(value=TemporalType.DATE)
    private Date dataFinal;
   
    @OneToMany(mappedBy = "projeto")
    private List<CampanhaVO> projetoCampanha;
   
    // gets/sets   
}



Code:
@Entity
@Table(name="CAMPANHA")
@AttributeOverride( name="id", column = @Column(name="CAMP_CD_CAMPANHA") )
public class CampanhaVO extends AbstratoVO implements Serializable {
   
    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
    @JoinColumn(name="PROJ_CD_PROJETO", nullable=false)
    private ProjetoVO projeto;
   
    @Column(name="CAMP_NM_CODIGO", length=6, nullable=false)
    private String codigo;
   
    @Column(name="CAMP_DT_INICIAL", nullable=false)
    @Temporal(value=TemporalType.DATE)
    private Date dataInicial;
   
    @Column(name="CAMP_DT_FINAL")
    @Temporal(value=TemporalType.DATE)
    private Date dataFinal;
   
    // gets/sets
}

Code between sessionFactory.openSession() and session.close():

If i try to replicate the objects from the same Session, it works fine:
Code:
try{
           
            em = FabricaDeGerenciadoresDeEntidades.getInstance();
            Query q = em.createQuery("from ProjetoVO");
           
            List<ProjetoVO> projetos = q.getResultList();

            q = em.createQuery("from CampanhaVO");
            List<CampanhaVO> campanhas = q.getResultList();
           
            em.close();
           
            em = FabricaDeGerenciadoresDeEntidades.getEntityManagerSincronismo();
           
            em.getTransaction().begin();
            for(ProjetoVO prj: projetos){
                System.out.println("projeto :" + prj);
                ((org.hibernate.Session)em.getDelegate()).replicate( prj, org.hibernate.ReplicationMode.OVERWRITE);
            }
            em.getTransaction().commit();
           
            em.getTransaction().begin();
            for(CampanhaVO cmp: campanhas){
                ((org.hibernate.Session)em.getDelegate()).replicate( cmp, org.hibernate.ReplicationMode.OVERWRITE);
            }
            em.getTransaction().commit();
           
        }catch(Exception e){
            if(em.getTransaction().isActive())
                em.getTransaction().rollback();
            e.printStackTrace();
        }finally{
            if(em.isOpen()){
                em.close();
            }


But if the objects are from different sessions,
Code:
       try{
            //Session to Oracle 9
            em = FabricaDeGerenciadoresDeEntidades.getInstance();
            Query q = em.createQuery("from ProjetoVO");
           
            List<ProjetoVO> projetos = q.getResultList();
            em.close();

            // Openning a new session
            em = FabricaDeGerenciadoresDeEntidades.getInstance();
            q = em.createQuery("from CampanhaVO");
            List<CampanhaVO> campanhas = q.getResultList();
           
            em.close();
           
            //Session to HSQLDB
            em = FabricaDeGerenciadoresDeEntidades.getEntityManagerSincronismo();
           
            em.getTransaction().begin();
            for(ProjetoVO prj: projetos){
                ((org.hibernate.Session)em.getDelegate()).replicate( prj, org.hibernate.ReplicationMode.OVERWRITE);
            }
            em.getTransaction().commit();
           
            em.getTransaction().begin();
            for(CampanhaVO cmp: campanhas){
                ((org.hibernate.Session)em.getDelegate()).replicate( cmp, org.hibernate.ReplicationMode.OVERWRITE);
            }
            em.getTransaction().commit();
           
        }catch(Exception e){
            if(em.getTransaction().isActive())
                em.getTransaction().rollback();
            e.printStackTrace();
        }finally{
            if(em.isOpen()){
                em.close();
            }
        }


Full stack trace of any exception that occurs:
I take a

javax.persistence.RollbackException: Error while commiting the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)
at br.com.petrobras.bdco.servicos.sincronismo.SincronismoTest.testeSessionDiferente(SincronismoTest.java:153)
at br.com.petrobras.bdco.servicos.sincronismo.SincronismoTest.main(SincronismoTest.java:85)
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: br.com.petrobras.bdco.entidades.projeto.ProjetoVO
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:79)
at org.hibernate.impl.SessionImpl.firePersistOnFlush(SessionImpl.java:644)
at org.hibernate.impl.SessionImpl.persistOnFlush(SessionImpl.java:636)
at org.hibernate.engine.CascadingAction$9.cascade(CascadingAction.java:323)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)

Name and version of the database you are using:
Replicating some entities from Oracle 9 to HSQLDB 1.8.0

Is that the expected behaviour?

In Java Persistence with Hibernate on page 407, is writen:
Quote:
Replication takes detached objects loaded in one Session and makes them persistent in another Session.


But, is there any restriction so that all the objects have to be from the same original Session?


Thank you, very much.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.