HashSet remove() problem...
derkd Sep 10, 2010 1:27 PMHi all,
I have a problem with the hashset. I try to remove an object from it but it resolves into false. Let me explain by code:
public String removeTeam(Team team) {
log.debug("remove team");
log.debug("team ID: " + team.getId());
log.debug("team.toString() " + team.toString());
Planning planning = planningHome.getInstance();
log.debug("planning ID: " + planning.getId());
log.debug("planning amount of teams: " + planning.getTeams().size());
for(Team _team : planning.getTeams() ){
log.debug("planning team ID " + _team.getId() );
log.debug("planning team.toString() " + _team.toString() );
log.debug("team vs planning team equals? " + team.equals(_team));
}
log.debug("removed or what? " + planning.getTeams().remove(team));
log.debug("planning amount of teams after removal: " + planning.getTeams().size());
planningHome.update();
return "removeTeam";
}The output:
19:09:51,939 DEBUG [ScheduleAction] remove team
19:09:51,940 DEBUG [ScheduleAction] team ID: 1
19:09:51,940 DEBUG [ScheduleAction] team.toString() eu.emolife.commitment.entity.project.Team@1
19:09:51,940 DEBUG [PlanningHome] getInstance is called, trying to return a planning
19:09:51,940 DEBUG [ScheduleAction] planning ID: 1
19:09:51,940 DEBUG [ScheduleAction] planning amount of teams: 1
19:09:51,940 DEBUG [ScheduleAction] planning team ID 1
19:09:51,940 DEBUG [ScheduleAction] planning team.toString() eu.emolife.commitment.entity.project.Team@1
19:09:51,941 DEBUG [ScheduleAction] team vs planning team equals? true
19:09:51,941 DEBUG [ScheduleAction] removed or what? false
19:09:51,941 DEBUG [ScheduleAction] planning amount of teams after removal: 1
19:09:51,943 DEBUG [PlanningHome] updated entity eu.emolife.commitment.entity.department.Planning 1
here the Planning class code:
@Entity
public class Planning implements Serializable {
/**
*
*/
private static final long serialVersionUID = 7257535247848794177L;
private Long id;
private Long version;
private Date date;
private ShiftDay shiftDay;
private Set<Team> teams = new HashSet<Team>(0);
private Project project;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Version
@Column(name = "obj_version", nullable = false)
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@OneToOne
public ShiftDay getShiftDay() {
return shiftDay;
}
public void setShiftDay(ShiftDay shiftDay) {
this.shiftDay = shiftDay;
}
@OneToMany(cascade = CascadeType.ALL)
public Set<Team> getTeams() {
return teams;
}
public void setTeams(Set<Team> teams) {
this.teams = teams;
}
@ManyToOne
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || !(obj instanceof Planning))
return false;
Planning other = (Planning)obj;
if (id == other.getId()) return true;
if (id == null) return false;
// equivalence by id
return id.equals(other.getId());
}
public int hashCode() {
if (id != null) {
return id.hashCode();
} else {
return super.hashCode();
}
}here the Team class code:
@Entity
public class Team implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1785144411928625999L;
private Long id;
private Long obj_version;
private String name;
private Date date;
private Set<ProjectTeamEmployee> projectTeamEmployees = new HashSet<ProjectTeamEmployee>(0);
private ScoreCard scoreCard;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Version
public Long getObj_version() {
return obj_version;
}
public void setObj_version(Long objVersion) {
obj_version = objVersion;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Temporal(TemporalType.TIMESTAMP)
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@OneToMany(cascade = CascadeType.ALL)
public Set<ProjectTeamEmployee> getProjectTeamEmployees() {
return projectTeamEmployees;
}
public void setProjectTeamEmployees(Set<ProjectTeamEmployee> projectTeamEmployees) {
this.projectTeamEmployees = projectTeamEmployees;
}
@OneToOne(cascade = CascadeType.ALL)
public ScoreCard getScoreCard() {
return scoreCard;
}
public void setScoreCard(ScoreCard scoreCard) {
this.scoreCard = scoreCard;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || !(obj instanceof Team))
return false;
Team other = (Team)obj;
if (id == other.getId()) return true;
if (id == null) return false;
// equivalence by id
return id.equals(other.getId());
}
public int hashCode() {
if (id != null) {
return id.hashCode();
} else {
return super.hashCode();
}
}I can't see what I'm doing wrong...