few approaches to fix this issue:
@OneToMany(mappedBy = "test1Id", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Test2> contacts = new HashSet<>();
2.Modify the persistence code to explicitly control the order
db.persist(test1);
db.flush(); // Force flush to ensure Test1 is in the database
db.persist(test2);
3.Use @MapsId for the child entity
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("objectId") // assuming your embedded ID has an objectId field
@JoinColumn(name = "object_id")
private Test1 test1Id;
Check your Hibernate configuration
In Hibernate 6, check if you have changed any settings related to order operations or batch processing. The property hibernate.order_inserts
might be relevant here.