I have created a Many to Many to association with an extra column following this article. It shows and exmaple with following entites: Post, Tag, PostTag and embeddable PostTagId. Everything works fine, but I have two questions (general, not strictly related to given tutorial):
1. Article clearly states:
The
@Embeddabletype must override the default equals and hashCode methods based on the two Primary Key identifier values.
fine, but it narrows to the first question:
Considering (from doc - can’t put link as don’t have enough reputation, sorry) that I will NOT:
- intend to put instances of persistent classes in a
Set(the recommended way to represent many-valued associations) and- intend to use reattachment of detached instances
do I have to provide equals()/hashCode() for related entites as well ? (in this example it would be for Post and Tag ?)
2. Quoted doc clearly uses AND. It concern me as I though that using a collection on persistent class is enough alone to be “forced” to implement proper equals() and hashCode() isn’t it ? I was thinking about disjunction more than conjuction f.e. because the following test (from Why You Should Care About Equals and Hashcode - DZone) fails:
def "should find cart for given customer after correcting email address"() {
given:
Cart sampleCart = new Cart()
Customer sampleCustomer = new Customer()
sampleCustomer.setId(UUID.randomUUID())
sampleCustomer.setEmail("emaill@customer.com")
HashMap customerToCart = new HashMap<>()
when:
customerToCart.put(sampleCustomer, sampleCart)
then:
customerToCart.get(sampleCustomer) == sampleCart
and:
sampleCustomer.setEmail("email@customer.com")
customerToCart.get(sampleCustomer) == sampleCart
}