一对一关联关系基于主键映射的异常 IdentifierGenerationException

具体异常:org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property[com.pms.entities.personnelManagement.TrainingStaff.record]

意思是说我的没有外键的一端,即 需要把主键作为另一端的外键的一端的主键为空(id 为空)。

出现这个异常的原因:

举例说明:

类Record 和 类 TrainingStaff 是基于主键的映射的一对一关联关系。 TrainingStaff 端( t 端 )的id 既作为主键又作为外键,也就是 t 端来维护关联关系。

①、Record record = (Record) testDao.getT(Record.class, 1);//此时我从数据库里获取了一个Record对象,刚好这个对象为null。

②、testDao.save(new Record("100000", "changda", 1, new Date(), ..........)//构造器太长内容省略。

③、testDao.save(new TrainingStaff(100, true, "表现非常好", record, trainingContent));//我这里的record 不是②中的record,而是① 中的record,所以关联关系设定错误。而且①中的record刚好为null,所以就报了attempted to assign id from null 这样的错误

原文地址:https://www.cnblogs.com/GooPolaris/p/7919650.html