我有一个非常简单的问题和选项之间的一对多关系,选项具有指向问题的外键(questionId),现在我要在一个XML配置中插入一个包含选项列表的问题对象。
问题:
选项:
配置:
如您所见,我想使用刚刚自动生成的
问题:
public Class Question{
...
private Integer questionId;
private List<Option> options;
....
}
选项:
public Class Option{
...
private Integer optionId;
private String context;
private Integer questionId; //FK
....
}
配置:
...
<insert id="insertQuestion" parameterType="com.pojos.Question" useGeneratedKeys="true">
INSERT into question (...) VALUES (...);
<selectKey keyColumn="questionId" keyProperty="questionId" resultType="int">
SELECT MAX(questionId) FROM question;
</selectKey>
INSERT INTO option (context, questionId)
VALUES
<foreach collection="options" item="option" open="(" separator="),(" close=")">
#{option.context}, #{questionId}
</foreach>
</insert>
...
如您所见,我想使用刚刚自动生成的
questionId
作为选项的FK信息,但是它无法正常工作,我该如何让它工作?或者我不能将它们放入单个范围内吗?