I read https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/. I tried suggestion config like(using spring data JPA,hibernate 5.0 as vendor ):
public class PaperSubjectType{
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@MapsId
private PaperSetting paperSetting;
..
}
class PaperSetting{
@Id
@GeneratedValue
private Long id;
..
}
first I tried the example:
PaperSetting paperSettingInDb = paperSettingRepository.findOne(1);
PaperSubjectType paperSubjectType = new PaperSubjectType();
paperSubjectType.setSubjectCode("91");
paperSubjectType.setPaperSetting(paperSettingInDb);
paperSubjectTypeRepository.save(paperSubjectType);
error:detached entity passed to persist:PaperSetting. it seems hibernate take PaperSetting as detached when cascade
2 if I want to create both PaperSubjectType and PaperSetting together,do I need to do this:
PaperSetting paperSetting = new PaperSetting();
paperSetting.setxx;
PaperSetting paperSettingInDbNew = paperSettingRepository.save(paperSetting);
PaperSubjectType paperSubjectType = new PaperSubjectType();
paperSubjectType.setPaperSetting(paperSettingInDbNew);
paperSubjectTypeRepository.save(paperSubjectType);
or I should use bidirectional in this situation? thank you!