我有一个如下模型:
@Entity
@Table(name="QUESTIONNAIRE")
@SequenceGenerator(name = "RA_QA_SEQ", sequenceName = "RA_QUESTIONNAIRE_SEQ")
public class Questionnaire {
@Id
@GeneratedValue(generator = "RA_QA_SEQ")
@Column(name = "QUESTID")
private long questionID;
@Column(name = "QUESTION")
private String question;
@GeneratedValue(generator = "RA_QA_SEQ")
@Column(name = "QUESTORDER")
private long questionOrder;
@Column(name = "QUESTCATEGORYID")
private long questionCatgoryID;
....... list of setters and getters.......
}
这里我想在DB中为“questionOrder”列插入“questionID”值。我想将@GeneratedValue(generator =“RA_QA_SEQ”)提供给“questionOrder”,但我仍然在DB中为“questionOrder”获得0,但是“questionID”获得正确的序列值。
请建议我如何将“questionID”的值变为“questionOrder”。
答案 0 :(得分:0)
从JPA 2.0规范:
11.1.17 GeneratedValue Annotation
GeneratedValue注释提供了规范 主键值的生成策略。该 GeneratedValue注释可以应用于主键属性或 实体的字段或映射的超类与Id一起使用 注释即可。
便携式应用不应使用GeneratedValue注释 其他持久性字段或属性。
如果您只想让questionOrder保持相同的值,我认为您可以这样做:
// 2 fields are mapped to the same column, so one of these needs to be read-only
@Column(name = "QUESTID", insertable=false, updateable=false)
private long questionOrder;
HTH。