My Spring Boot application uses a.o. 2 Entity classes. Entity class 1 uses a technical key id that uses a sequence. The Entity contains a list of other Entities, so a one-to-many. The Child entity uses the same sequence.
Using a seqeuence allocation (cacheing) size of 20, I see that I get EntityExistsException:
javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [nl.xyz.app1.entity.ChildFields #123456]
The Entities are:
@Entity
@Table(name = "CHILD_FIELDS")
public class ChildFields implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "my_entity_seq_gen")
@SequenceGenerator(name = "my_entity_seq_gen", sequenceName = "MYSEQ_S01")
@Column(name = "CF_ID", unique = true, nullable = false)
private Long id;
@Column(name = "CF_DETAILS_ID")
private Long detailsId;
And
@Entity
@Table(name = "PARENTS_OBJECT")
public class ParentObject implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "my_entity_seq_gen")
@SequenceGenerator(name = "my_entity_seq_gen", sequenceName = "MYSEQ_S01")
@Column(name = "PF_ID", unique = true, nullable = false)
private Long id;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "CF_DETAILS_ID")
private List<ChildFields> children;
When I use a allocation sequence of 1, then everything is ok! Why is this?
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "my_entity_seq_gen" )
@SequenceGenerator(name = "my_entity_seq_gen", sequenceName = "MYSEQ_S01", allocationSize=1)
Etc.