I’m wondering how to write a correct equals() and hashCode() for Hibernate Entities which have a Lazy Loaded ManyToOne relation to another Entity which is important as a business key. Note, that I already read the Hibernate documentation on this topic and I know I must/should not use the object id.
To clarify, here an example:
public class BusinessEntity implements Serializable
{
//for simplicity, here just the important part
private String s;
@ManyToOne(fetch= FetchType.LAZY )
private ImportantEntity anotherEntity;
@Override
public boolean equals( Object obj )
{
//Here I would like to call something like
// (obj.getAnotherEntity.getName.equals(getAnotherEntity.getName) && obj.getS().equals(getS());
return true;
}
}
Of course this is just a simplified example. But I hope I could explain my scenario. Has someone tried something like that before? I did not find anything regarding to this topic.
For simplicity I have skipped null-safety code. But the idea is to create
additional property which will persist entity name and will not expose it to the outside world