-->
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.  [ 4 posts ] 
Author Message
 Post subject: [spring+hibernate]
PostPosted: Mon May 06, 2013 5:39 am 
Newbie

Joined: Mon May 06, 2013 5:12 am
Posts: 3
Hay all,

I have a problem,


Code:
@Transactional(readOnly=false)
   public void saveAndProccess(List<Jurnal> listJurnal,   Date bgnDate, Date endDate, String user) {
      saveListJurnalToReportBook(listJurnal, bgnDate, endDate, user); // save list of jurnal to ReportBook
      
      List<ReportBook> listReportBook = getListReportBookByDateRanges(bgnDate, endDate); // get list of ReportBook
      
      for (ReportBook rb : listReportBook){
         
         System.out.println("sub is === "+rb.getEstimate().getSub());

      }
      
   }


the result is : sub is === null
but with this code ( separated calling for 2 methods)

the firts call this method
Code:
@Transactional
public void saveListJurnalToReportBook(List<Jurnal> listJurnal){
// this code to save list of jurnal to ReportBook table
}

the second call this method
Code:
@Transactional(readOnly=false)
   public void saveAndProccess(List<Jurnal> listJurnal,   Date bgnDate, Date endDate, String user) {
            
      List<ReportBook> listReportBook = getListReportBookByDateRanges(bgnDate, endDate); // get list of ReportBook
      
      for (ReportBook rb : listReportBook){
         
         System.out.println("sub is === "+rb.getEstimate().getSub());

      }
      
   }

the result : sub is T

thanks in advance

Pro


Top
 Profile  
 
 Post subject: Re: [spring+hibernate]
PostPosted: Mon May 06, 2013 6:41 am 
Newbie

Joined: Thu Jan 27, 2011 10:53 am
Posts: 12
Hi,
Can you provide a simplified mapping and a test case.
I guess your problem is due to a bad implementation of the equals and hashcode method of your bean.
Since the call is made in one transaction unit in the first case, hibernate try to find the bean in its first level cache using those methods.
There two way to fix your problem:
Implementing equals and hashcode using a functional key
Flushing the hibernate session after the save and before the get
Hope it helps


Top
 Profile  
 
 Post subject: Re: [spring+hibernate]
PostPosted: Mon May 06, 2013 10:38 pm 
Newbie

Joined: Mon May 06, 2013 5:12 am
Posts: 3
entity class ReportBook
Code:
@EmbeddedId
   private ReportBookPK reportBookPK;
@ManyToOne
   @JoinColumn(name="ID_EST", referencedColumnName="ID_EST", insertable=false, updatable=false)
   private Estimate estimate;
@Override
   public int hashCode() {
      final int prime = 31;
      int result = super.hashCode();
      result = prime * result
            + ((reportBookPK == null) ? 0 : reportBookPK.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (!super.equals(obj))
         return false;
      if (getClass() != obj.getClass())
         return false;
      ReportBook other = (ReportBook) obj;
      if (reportBookPK == null) {
         if (other.reportBookPK != null)
            return false;
      } else if (!reportBookPK.equals(other.reportBookPK))
         return false;
      return true;
   }


entity class ReportBookPK
Code:
@Column(name="ID_JURNAL", length=14)
   private String idJurnal;
   @Column(name="ID_EST", length=6)
   private String idEst;
   @Column(name="ID_SUB", length=3)
   private String idSub;

@Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result
            + ((idJurnal == null) ? 0 : idJurnal.hashCode());
      result = prime * result
            + ((idEst == null) ? 0 : idEst.hashCode());
      result = prime * result + ((idSub == null) ? 0 : idSub.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      ReportBookPK other = (ReportBookPK) obj;
      if (idJurnal == null) {
         if (other.idJurnal != null)
            return false;
      } else if (!idJurnal.equals(other.idJurnal))
         return false;
      if (idEst == null) {
         if (other.idEst != null)
            return false;
      } else if (!idEst.equals(other.idEst))
         return false;
      if (idSub == null) {
         if (other.idSub != null)
            return false;
      } else if (!idSub.equals(other.idSub))
         return false;
      return true;
   }


entity class Estimate
Code:
@EmbeddedId
   private EstimatePK estimatePK;
@OneToMany(cascade={CascadeType.ALL}, mappedBy="estimate")
   private List<ReportBook> reportBookList;


entity class EstimatePK
Code:
@Column(name="ID_EST", length=6, nullable=false)
   private String idEst;


Thanks


Top
 Profile  
 
 Post subject: Re: [spring+hibernate] solved
PostPosted: Tue May 07, 2013 4:36 am 
Newbie

Joined: Mon May 06, 2013 5:12 am
Posts: 3
thank my friend,

this problem was solved.

however this problem caused by using of the different session between save() method and getListReportBookByDateRange method.

before :
Code:
@Transactional(readOnly=false)
   public void save(ReportBook domain, String user) {
   
      domain.getUserTrailing().setVcreaby(user);
      domain.getUserTrailing().setDcrea(MyDateUtil.getLongSysDate());

      getHibernateTemplate().save(domain);
      getHibernateTemplate().flush();
      getHibernateTemplate().refresh(domain);
      
   }


then I change became
Code:
@Transactional(readOnly=false)
   public void save(ReportBook domain, String user) {
   
      domain.getUserTrailing().setVcreaby(user);
      domain.getUserTrailing().setDcrea(MyDateUtil.getLongSysDate());

      getHibernateTemplate().getSessionFactory().getCurrentSession().save(domain);
      getHibernateTemplate().getSessionFactory().getCurrentSession().flush();
      getHibernateTemplate().getSessionFactory().getCurrentSession().refresh(domain);
      
   }


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

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.