In Hibernate I want to save an object that has a child.In parent Object I have Set of other classes that are child. The parent hase a @OneToMany relationship with the child.
parent :
@Entity
public class TalentIdentitySetting {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "setting", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<TalentIdentitySettingPower> settingPowers;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "setting", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<TalentIdentitySettingSpeciality> settingSpecialities;
}
child :
@Entity
public class TalentIdentitySettingPower {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "IDENTITY_SETTING_ID", nullable = false)
private TalentIdentitySetting setting;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "POWER_ID", nullable = false)
private BasePower power;
}
how I can save parent and child together in one transaction. This is my problem; When the child wants to be saved the parent has not been saved yet and say parent key not found. In fact, hibernate should save the parent first and then the child.
- I used PERSIST cascade but it still didn't work.
please help me.