Hi !
I am using Hibernate 3 with Spring Framework for hibernate 3.
I have a relation many-to-one between Member and FriendMatchingProfile.
FirendMatchinfProfile extends Profile.
Code:
/////////////////////////// Member ////////////////////////
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import java.util.Collections;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.HashSet;
/**
*
* @author
*
* @hibernate.class table="member"
*/
public class Member implements Serializable
{
/** identifier field */
private Integer id;
/** persistent field */
private String userName;
/** persistent field */
private String passwd;
/** persistent field */
private String name;// firstname+middlename+lastname
private Set friendMatchingProfiles;
/** Creates a new instance of Member */
Member()
{
}
public static Member generate(String userName, String name)
{
Member m = new Member();
m.setName(name);
m.setUserName(userName);
m.friendMatchingProfiles = new LinkedHashSet();
return m;
}
public String toString()
{
return new ToStringBuilder(this)
.append("id", getId()).append("userName", this.getUserName())
.toString();
}
public boolean equals(Object other)
{
if ( !(other instanceof Member) ) return false;
Member castOther = (Member) other;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getId())
.toHashCode();
}
/**
*
* @hibernate.id generator-class="native" column="id"
*/
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
/**
*
* @hibernate.property
* column="user_name"
* not-null="true"
* unique="true"
*/
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
/**
*
* @hibernate.property
* column="name"
* not-null="true"
*/
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
/**
*
* @hibernate.property
* column="passwd"
* not-null="false"
*/
public String getPasswd()
{
return passwd;
}
public void setPasswd(String passwd)
{
this.passwd = passwd;
}
/**
* @hibernate.set role="friendMatchingProfiles" readonly="true" cascade="all-delete-orphan" lazy="true" where="type='FriendMatching'"
order-by="id asc"
* @hibernate.collection-key column="member_id"
* @hibernate.collection-one-to-many class=".FriendMatchingProfile"
*/
public Set getFriendMatchingProfiles()
{
return friendMatchingProfiles;
}
public void addFriendMatchingProfile(FriendMatchingProfile fmp)
{
fmp.setMember(this);
this.friendMatchingProfiles.add(fmp);
}
public void removeFriendMatchingProfile(FriendMatchingProfile fmp)
{
this.friendMatchingProfiles.remove(fmp);
}
public void setFriendMatchingProfiles(Set friendMatchingProfiles)
{
this.friendMatchingProfiles = friendMatchingProfiles;
}
}
and for Profile and FriendMatchingProfile
Code:
////////////////////////// Profile
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import java.util.Collections;
import java.util.Set;
import java.util.LinkedHashSet;
/**
*
* @author
*
* @hibernate.class table="profile"
* @hibernate.discriminator column="type" type="string"
*/
public abstract class Profile implements Serializable
{
/** identifier field */
private Integer id;
private Byte age;
private Byte sex;
private Byte clothingStyle;
private Byte musicType;
private Byte otherActivities;
private Byte personType;
private Byte personality;
private Byte dailyActivities;
Profile()
{
}
public String toString()
{
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
public boolean equals(Object other)
{
if ( !(other instanceof Profile) ) return false;
Profile castOther = (Profile) other;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
}
public int hashCode()
{
return new HashCodeBuilder()
.append(getId())
.toHashCode();
}
/**
*
* @hibernate.id generator-class="native" column="id"
*/
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
/**
*
* @hibernate.property
* column="age"
* not-null="false"
*/
public Byte getAge()
{
return age;
}
public void setAge(Byte age)
{
this.age = age;
}
/**
*
* @hibernate.property
* column="sex"
* not-null="false"
*/
public Byte getSex()
{
return sex;
}
public void setSex(Byte sex)
{
this.sex = sex;
}
/**
*
* @hibernate.property
* column="clothing_style"
* not-null="false"
*/
public Byte getClothingStyle()
{
return clothingStyle;
}
public void setClothingStyle(Byte clothingStyle)
{
this.clothingStyle = clothingStyle;
}
/**
*
* @hibernate.property
* column="music_type"
* not-null="false"
*/
public Byte getMusicType()
{
return musicType;
}
public void setMusicType(Byte musicType)
{
this.musicType = musicType;
}
/**
*
* @hibernate.property
* column="other_activ"
* not-null="false"
*/
public Byte getOtherActivities()
{
return otherActivities;
}
public void setOtherActivities(Byte otherActivities)
{
this.otherActivities = otherActivities;
}
/**
*
* @hibernate.property
* column="person_type"
* not-null="false"
*/
public Byte getPersonType()
{
return personType;
}
public void setPersonType(Byte personType)
{
this.personType = personType;
}
/**
*
* @hibernate.property
* column="personality"
* not-null="false"
*/
public Byte getPersonality()
{
return personality;
}
public void setPersonality(Byte personality)
{
this.personality = personality;
}
/**
*
* @hibernate.property
* column="daily_activ"
* not-null="false"
*/
public Byte getDailyActivities()
{
return dailyActivities;
}
public void setDailyActivities(Byte dailyActivities)
{
this.dailyActivities = dailyActivities;
}
}
///////////////////////// FriendMatchingProfile ///////
/*
* FriendMatchingProfile.java
*
* Created on April 11, 2005, 5:36 PM
*/
package ;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/**
*
* @author GFL
*
* @hibernate.subclass discriminator-value="FriendMatching"
*/
public class FriendMatchingProfile extends Profile
{
private String friendName;
FriendMatchingProfile()
{
}
public static FriendMatchingProfile generate(String friendName)
{
FriendMatchingProfile p = new FriendMatchingProfile();
p.setFriendName(friendName);
return p;
}
private Member member;
/**
* @hibernate.many-to-one
* column="member_id"
* class="Member"
* not-null="true"
*
*/
public Member getMember()
{
return member;
}
public void setMember(Member member)
{
this.member = member;
}
/**
*
* @hibernate.property
* column="friend_name"
* not-null="true"
*/
public String getFriendName()
{
return friendName;
}
public void setFriendName(String friendName)
{
this.friendName = friendName;
}
public boolean equals(Object other)
{
if ( !(other instanceof FriendMatchingProfile) ) return false;
FriendMatchingProfile castOther = (FriendMatchingProfile) other;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getId())
.toHashCode();
}
}
///////////////////// end CODE
I have a unit test case like this one:
Code:
.....
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
....
// executed in one session
public void testGetDeleteSaveProfile()
{
Member m = null;
m = Member.generate("user1", "caca de maca");
service.saveMember(m);
m = service.getMember("user1");
FriendMatchingProfile xxxx = FriendMatchingProfile.generate("name 44 ");
xxxx.setAge(new Byte("21"));
// add profile
m.addFriendMatchingProfile(xxxx);
// save member. OK
service.saveMember(m);
// get member again. OK
m = service.getMember("user1");
// load from databse. OK
FriendMatchingProfile y =
service.getFriendMatchingProfile(xxxx.getId());
// assertion is passed . OK
assertTrue(y.equals(xxxx));
// print y (FriendMatchingProfile@e99ce5[id=9])
System.out.println("--------------y:"+y);
// the following prints all profiles including profile xxxx . OK ( [FriendMatchingProfile@10c0f66[id=3], FriendMatchingProfile@e265d0[id=4], FriendMatchingProfile@18a6e6e[id=6], FriendMatchingProfile@1581e80[id=7], FriendMatchingProfile@3a9d95[id=8], FriendMatchingProfile@e99ce5[id=9]])
System.out.println("------------profiles:"+m.getFriendMatchingProfiles());
// the following prints "...-----contain=false" !!!!!!!!. NOT OK
System.out.println("---------------contain="+m.getFriendMatchingProfiles().contains(y));
m.removeFriendMatchingProfile(xxxx);
service.saveMember(m);
/////// xxxx IS NOT REMOVED FROM DATABASE !!!!!!!. NOT OK
}
I do not know:
- why profile "xxxx" was not found in collection since collection contains this object.
- why profile "xxxx" was not removed from database since cascade="all-delete-orphan" when removing "xxxx" from collection.
Thanks,
Florian