Given the following:
@Entity
public class Parent implements Serializable {
@Id
private Long id;
private List<Child> children = new ArrayList<Child>();
...
}
Is it wrong to have Parent.equals () and Parent.hashCode () only use id? I understand that Child.equals () and Child.hashCode () must use an immutable set of attributes for the "natural key" for proper parent management. However, if Parent is always a top-level object (i.e., it is never the back of any association), is there anything wrong with using only id?
Are there any unwanted effects that could have occurred by doing this? I guess that maybe if I do this when I add a child (or delete), Hibernate will not be able to say that Parent has changed (and needs to be updated in the database)? In this case, should you use the children property for Parent.equals () and Parent.hashCode ()?
I ask because Hibernate docs explicity says not to use the @Id property for the "natural key" ...
source
share