JPA @Id annoation

时间:2015-06-17 01:37:30

标签: jpa

我想插入具有指定值的表格,但它不起作用,  这是我的代码:

@Id
@Column(insertable=true,updatable=true)
public Long getS_id() {
    return s_id;
}


@Resource(name="studentService")
private StudentService stus;

Student student = new Student();
    student.setS_id(123213L);
    student.setName("vincent");
    stus.add(student);

如果我改变:

@Id
@Column(insertable=true,updatable=true)
public Long getS_id() {
    return s_id;
}

到此:

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(insertable=true,updatable=true)
public Long getS_id() {
    return s_id;
}

并且不要将s_id设置为适当的。

这是我的学生班

@Entity()
@Table(name="stu_info")
public class Student implements Serializable{

private static final long serialVersionUID = 1L;
/**
 * 学生的学号
 */
private Long s_id;
/**
 * 学生姓名
 */
private String name;
/**
 * 学生性别
 */
private String sex;
/**
 * 学生生日
 */
private Date birthday;

/**
 * 学生电话号码
 */
private String telephone;
/**
 * 学生所在年级
 */
private String grade;
/**
 * 学生所在班级
 */
private String classes;
/**
 * 学生编号
 */
private int number;
/**
 * 学生父亲姓名
 */
private String father_name;
/**
 * 学生母亲姓名
 */
private String mother_name;
/**
 * 学生个人疾病史
 */
private String diseases_history;

@Id
@Column(insertable=true,updatable=true)
public Long getS_id() {
    return s_id;
}
public void setS_id(Long s_id) {
    this.s_id = s_id;
}
@Column(length=32)
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@Column(length=12)
public String getSex() {
    return sex;
}
public void setSex(String sex) {
    this.sex = sex;
}
@Temporal(TemporalType.DATE)
public Date getBirthday() {
    return birthday;
}
public void setBirthday(Date birthday) {
    this.birthday = birthday;
}

@Column(length=12)
public String getTelephone() {
    return telephone;
}
public void setTelephone(String telephone) {
    this.telephone = telephone;
}
@Column(length=32)
public String getGrade() {
    return grade;
}
public void setGrade(String grade) {
    this.grade = grade;
}
@Column(length=32)
public String getClasses() {
    return classes;
}
public void setClasses(String classes) {
    this.classes = classes;
}
@Column(length=32)
public int getNumber() {
    return number;
}
public void setNumber(int number) {
    this.number = number;
}
@Column(length=32)
public String getFather_name() {
    return father_name;
}
public void setFather_name(String father_name) {
    this.father_name = father_name;
}
@Column(length=32)
public String getMother_name() {
    return mother_name;
}
public void setMother_name(String mother_name) {
    this.mother_name = mother_name;
}
@Column(length=32)
public String getDiseases_history() {
    return diseases_history;
}
public void setDiseases_history(String diseases_history) {
    this.diseases_history = diseases_history;
}   

}

1 个答案:

答案 0 :(得分:0)

根据发布的有限信息,我猜您正在使用SQL Server并将记录插入到SQLServer表中,并为Identity列定义了显式值,这要求您为该表启用标识插入。

https://msdn.microsoft.com/en-gb/library/ms188059.aspx

因此,如果您对表执行上述操作,则应该能够使用特定值继续执行。

所以与JPA没什么关系。