Hi,
I've got same problem as here (
http://forum.hibernate.org/posting.php? ... y&t=965543). After persist a Set inside my graph, i sucessfully tried to add a repeated State (same name) into a
detached Country.
I'm a beginner and I'm keen to understand this behavior.
Hibernate Log
14:42:10,796 INFO Version:15 - Hibernate EntityManager 3.2.1.GA
14:42:10,828 INFO Version:15 - Hibernate Annotations 3.2.1.GA
14:42:10,843 INFO Environment:514 - Hibernate 3.2.4.sp1
14:42:10,843 INFO Environment:547 - hibernate.properties not found
14:42:10,843 INFO Environment:681 - Bytecode provider name : cglib
14:42:10,859 INFO Environment:598 - using JDK 1.4 java.sql.Timestamp handling
Code:
public class Country implements Serializable {
@Id @GeneratedValue
private Long id;
private String name;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="idCountry")
private Set<Country> countries = new HashSet<Country>();
......
public void addState(State state) {
if (!this.getStates().add(state)){
throw new BusinessException("State exists");
}
public int hashcode() {
return 29 * getNome().hashCode();
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pais)) return false;
final Pais pais = (Pais) o;
if (!nome.equals(pais.getNome())) return false;
return true;
}
}
Code:
public class UnidadeFederativa implements Serializable {
@Id @GeneratedValue
private Long id;
private String name;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="idState")
private Set<City> city = new HashSet<City>();
public int hashcode() {
return 29 * getNome().hashCode();
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof State)) return false;
final State state = (State) o;
if (!name.equals(state.name)) return false;
return true;
}
1) It seems PersistentSet isn't using equals() when it gets "if (!this.getStates().add(state))". Is it true?
2) I've understood that if I had used ID in equals/hashcode it would be the cause of the problems. But I'm not using ID on equals/hashcode. I've read this article (
http://www.hibernate.org/109.html) and thought that implementations with business keys shouldn't get problems.
3) How can I solve this issue!? I have also tried with refresh/flush inside session but without sucess.
Thanks.