-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hashcode and Equals on PK
PostPosted: Sat Dec 01, 2007 1:33 pm 
Beginner
Beginner

Joined: Sat Oct 20, 2007 8:28 am
Posts: 28
hi here is my entity :
Code:
@Entity
@AccessType("property")
@Table(name = "stats")
@IdClass(StatsPK.class)
public class Stats implements Serializable,Comparable<Stats>{

   private String statType;
   private String paperName;
   private Date beginDate;
   private Date endDate;
   private double value;
   private String timePeriod;
   
   @Id
   public String getStatType() {return statType;}
   public void setStatType(String statType) {this.statType = statType;}
   
   @Id
   public String getPaperName() {return paperName;}
   public void setPaperName(String paperName) {this.paperName = paperName;}
   
   @Id
   public Date getBeginDate() {return beginDate;}
   public void setBeginDate(Date beginDate) {this.beginDate = beginDate;}
   
   @Id
   public Date getEndDate() {return endDate;}
   public void setEndDate(Date endDate) {this.endDate = endDate;}
   
   @Column(name="value")
   public double getValue() {return value;}
   public void setValue(double value) {this.value = value;}
   
   @Column(name="timePeriod")
   public String getTimePeriod() {return timePeriod;}
   public void setTimePeriod(String timePeriod) {this.timePeriod = timePeriod;}
   
   @Override
   public String toString() {
      return this.getStatType() + " : " +this.getPaperName()+" : "+this.getBeginDate() + " : " + this.getEndDate() + " : " + this.getValue();
   }
   public int compareTo(Stats stats) {
      return
         this.getBeginDate().hashCode() - stats.getBeginDate().hashCode() +
         this.getEndDate().hashCode() - stats.getEndDate().hashCode() +
         this.getTimePeriod().hashCode() - stats.getTimePeriod().hashCode() +
         this.getStatType().hashCode() - stats.getStatType().hashCode() ;
         
   }
}



and the PK
Code:
@Embeddable
public class StatsPK implements Serializable{
   
   private String statType;
   private String paperName;
   private Date beginDate;
   private Date endDate;

   
   @Column(name="stattype")
   public String getStatType() {return statType;}
   public void setStatType(String statType) {this.statType = statType;}
   
   @Column(name="papername")
   public String getPaperName() {return paperName;}
   public void setPaperName(String paperName) {this.paperName = paperName;}
   
   @Column(name="begindate")
   public Date getBeginDate() {return beginDate;}
   public void setBeginDate(Date beginDate) {this.beginDate = beginDate;}
   
   @Column(name="enddate")
   public Date getEndDate() {return endDate;}
   public void setEndDate(Date endDate) {this.endDate = endDate;}
   
   @Override
   public int hashCode() {
      return this.paperName.hashCode()
               +this.statType.hashCode()
               +this.beginDate.hashCode()
               +this.endDate.hashCode();
   }
   @Override
   public boolean equals(Object other) {
      if ((other==null)||!(other instanceof StatsPK ))return false;
      StatsPK statsPK = (StatsPK)other;
      return (      
               this.paperName.equals(statsPK.paperName) &&
               this.statType.equals(statsPK.statType) &&
               this.beginDate.getTime()==statsPK.beginDate.getTime() &&
               this.endDate.getTime()==statsPK.endDate.getTime()
            );
   }
   


now you can see the stattype is the main diffrence , i have many PK which that is the only diffrence the same paper can and should have the same exact PK and only differ on the type and value
when i run per stattype it's all good beside the fact i'm doing xTimeOfType
DB saves data no problem no violation of key , for some reason when i do all type of stats per papername at one time it fails ok PK
Code:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session


any idea ?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 02, 2007 9:30 am 
Beginner
Beginner

Joined: Tue Nov 27, 2007 9:44 am
Posts: 46
Hi

Can you please post the code you are running when the Exception occurs?

Thanks,
Frank


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 02, 2007 10:39 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
In addition to what Alm Ghandi asked for, please post the print out of your session statistic right before the error occurs.

It should contain all the keys of the contained entities. I guess you'll need a reasonable definition of the toString() method, in order to make the output human readable.

regards
Jens


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 7:44 am 
Beginner
Beginner

Joined: Sat Oct 20, 2007 8:28 am
Posts: 28
thanks for your time guys , solved it out .
turns out it wasn't hibernate related but something burried deep inside on the engine that generates the time period and in a list of 60-70 years * 100 stocks it's tricky to find one block of week that repeats .

schauder - what are session statistics ?
allso i'll reuse this thread , where can i read as to how hibernate caches ?
i'm looking to improve speed and this might help me .


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 4:22 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
http://www.hibernate.org/hib_docs/v3/ap ... tatistics()

To the question about the cache: http://www.google.com/search?q=hibernate+cache


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 6:45 pm 
Beginner
Beginner

Joined: Sat Oct 20, 2007 8:28 am
Posts: 28
schauder wrote:
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#getStatistics()

To the question about the cache: http://www.google.com/search?q=hibernate+cache


thanks you good man .
in regards to the cache i obviously did that but i was looking for someting a bit more with realy complex examples etc...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 2:03 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
What kind of complex examples are you looking for?

If the first three google hits (especially this one: http://www.devx.com/dbzone/Article/29685 ) aren't enough, we need a little more information about what you are looking for.

kind regards

Jens

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.