我的实体如下:
@Entity
@Table(name="`surveys`")
@Inheritance
@DiscriminatorColumn(name="`type`")
public abstract class Survey implements Serializable, Comparable<Survey> {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
[...]
}
上面抽象的 Survey 可以有两种具体的类:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorValue(value="SV")
public class SupervisorSurvey extends Survey implements Serializable {
@OneToOne(mappedBy="supervisorSurvey",cascade=CascadeType.ALL)
private SurveyPair surveyPair;
[...]
}
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorValue(value="US")
public class UserSurvey extends Survey implements Serializable {
@OneToOne(mappedBy="userSurvey",cascade=CascadeType.ALL)
private SurveyPair surveyPair;
[...]
}
然后,SurveyPair 的一个实例抓取了这两种类型中的一种:
@Entity
@Table(name="`surveypairs`")
public class SurveyPair implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@OneToOne
@JoinColumn(name="`userSurveyId`",unique=true)
private UserSurvey userSurvey;
@OneToOne
@JoinColumn(name="`supervisorSurveyId`",unique=true)
private SupervisorSurvey supervisorSurvey;
}
现在的问题:在我创建这样的对象之后:
// survey pair
SurveyPair surveyPair = new SurveyPair(reference);
// user side
UserSurvey usSurvey = new UserSurvey(user);
usSurvey.setSurveyPair(surveyPair);
surveyPair.setUserSurvey(usSurvey);
// supervisor side
SupervisorSurvey svSurvey = new SupervisorSurvey(Application.getLoggedUser(), user);
svSurvey.setSurveyPair(surveyPair);
surveyPair.setSupervisorSurvey(svSurvey);
SurveyInput.getInstance().persist(surveyPair);
上述呼吁坚持:
JPAHelper db = new JPAHelper();
EntityManager em = db.getEntityManager();
em.getTransaction().begin();
em.persist(surveyPair.getUserSurvey());
em.persist(surveyPair.getSupervisorSurvey());
em.persist(surveyPair);
em.getTransaction().commit();
em.close();
正确插入 SurveyPair(UserSurvey 和 SupervisorSurvey 的外来 ID 列值不为空),但插入 UserSurvey 和 SupervisorSurvey 时使用 NULL ID。
为什么会这样?如果我在persist 方法之后调试代码,我可以检查对象实例的ID 并导致它们被实际填充。