-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate composite-id question
PostPosted: Thu Dec 13, 2007 9:23 pm 
Newbie

Joined: Fri Jun 08, 2007 3:16 am
Posts: 11
I use composite-id,the composite-id is composed by id and uid,like follows:
Test.hbm.xml
Code:
<class name="Test.Model" table="Test" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version">
<composite-id mapped="false" unsaved-value="undefined">
  <key-property name="id" column="id" type="string" />
  <key-property name="uid" column="uid" type="string" />
  </composite-id>
</class>


Model.java is follows:
Code:
package Test;
public class Model{
  private String id;
  private String uid;
  public String getId(){
   return id;
}
public void setId(String id){
  this.id=id;
}
public String getUid(){
return uid;
}
public void setUid(String uid){
this.uid=uid;
}
public boolean equals(Object obj) {
if(!(obj instanceof Model)){
  return false;
}
Model mm=(Model)obj;
return new EqualsBuilder()
    .appendSuper(super.equals(obj))
    .append(this.id, mm.id)
    .append(this.uid, mm.uid)
    .isEquals();       
}
public int hasCode(){
   return new HashCodeBuilder()
   .appendSuper(super.hashCode())
   .append(this.id)
   .append(this.uid)
   .toHashCode();           
  }
}

I have two question,
Question 1:
When I start Tomcat,tomcat raise following warning:
composite-id class does not override hashCode():Test.Model
I have realize hasCode function,why tomcat raise above warning message? How to correct my code to get rid of warning message?

Question 2:
I have two record in my database,
id uid
-----------
11 Rose
12 Kate
I want to delete id='11' record,I use following code:
Code:
Model mm=new Model();
mm.setId('11');
this.getHibernateTemplate().delete(mm);


When execute above code,I find id='11' record still in database,why it can't be deleted from database? I am puzzled with it!

Then I modify above code,like follows:
Code:
Model mm=new Model();
mm.setId('11');
List info=this.getHibernateTemplate().find("from Test.Model where id=11");
mm=(Model)info.get(0);      
this.getHibernateTemplate().delete(mm);


This time the record of id='11' has been deleted from database. Why? Why I must use this.getHibernateTemplate().find first,then I can use this.getHibernateTemplate().delete?

Any idea will be appreciated!
Best regards,
Edward


Top
 Profile  
 
 Post subject: Re: Hibernate composite-id question
PostPosted: Fri Dec 14, 2007 3:12 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
you will get rid of the message if you define the id as a class:

Code:
<composite-id name="id" class="MyIdClass">
...
</composite-id>



and then define equals and hashcode for the id class. It should also be serializable. This also quite explains why you can't delete instances by only specifying one property of a composite id. I do also believe you can not delete an object like that because it might have some complexities in terms of what happens to collections and other kind of dependent objects. I mean how do you expect a session to handle all those dependencies if it does not know what the object looked like before.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 14, 2007 3:56 pm 
Beginner
Beginner

Joined: Fri Jun 25, 2004 11:47 am
Posts: 34
Hi,

You have a typpo mistake....
Code:
public int hasCode(){
   return new HashCodeBuilder()
   .appendSuper(super.hashCode())
   .append(this.id)
   .append(this.uid)
   .toHashCode();           
  }

You wrote hasCode instead of hashCode()....


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