I have two java classes:
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@EqualsAndHashCode(callSuper = false)
@FieldDefaults(level = AccessLevel.PRIVATE)
@Entity
@Table(name = "parent", schema = "public")
public class ParentEntity extends BaseEntity {
@Column(name = "name", length = 13)
@Comment("Name")
String name;
@Comment("Foreign key, child ID")
@OneToOne(
fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
ChildEntity child;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@EqualsAndHashCode(callSuper = false)
@FieldDefaults(level = AccessLevel.PRIVATE)
@Entity
@Table(name = "child", schema = "public")
public class ChildEntity extends BaseEntity {
@Column(name = "child_name")
@Comment("Child name")
String childName;
}
I have also ChildControllerIntegrTest with @BeforeEach method:
@BeforeEach
public void setup() {
addressRepository.deleteAll();
... some other code...
}
When I run tests all other tests pass but this one. It throws this error:
[ERROR: update or delete on table "child" violates foreign key constraint "fkcge1j432o5rq638rods2iqocq" on table "parent"
Detail: Key (id)=(19) is still referenced from table "parent".] [delete from public.child where id=?]; SQL [delete from public.child where id=?]; constraint [fkcge1j432o5rq638rods2iqocq]
How can I fix it? Thank you in advance
I tried removing CascadeType(as recommended), from the property, but other tests fail, because Child entity becomes transient.