Files
hibernate-demo/src/main/java/com/ankurm/hibernatedemo/persistenceannotations/IdentityHashSetEntity.java
T

42 lines
1.1 KiB
Java
Executable File

package com.ankurm.hibernatedemo.persistenceannotations;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
/**
* Deliberately relies on the JPA/Hibernate DEFAULT {@code equals()}/{@code hashCode()}
* (identity-based, inherited from Object) with a surrogate {@code @GeneratedValue} id that is
* null until the first flush. This is the entity used to reproduce the classic
* "HashSet.contains() returns false after persist()" symptom.
*
* <p>Docs: docs/05-jpa-persistence-annotations.md, Topic 2.
*/
@Entity
public class IdentityHashSetEntity {
@Id
@GeneratedValue
private Long id;
private String label;
public IdentityHashSetEntity() {
}
public IdentityHashSetEntity(String label) {
this.label = label;
}
public Long getId() {
return id;
}
public String getLabel() {
return label;
}
// Deliberately NOT overriding equals()/hashCode() -- uses Object identity.
// A second class below overrides them using the surrogate id, to show that trap too.
}