JPA@Id注释
我想用指定的值插入到一个表中,但它就是不起作用, 这是我的密码:JPA@Id注释,jpa,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);
@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;
}
}根据发布的有限信息,我猜您正在使用SQL Server,如果要使用为标识列定义的显式值将记录插入到SQL Server表中,则需要为该表启用标识插入 因此,如果对表运行上述操作,则应该能够使用特定的值进行持久化
因此,这与JPA没有任何关系。你能提供一段概述你的学生对象的代码,以便我们可以在上面看到你的JPA注释吗?“行不通”有点模糊,你不觉得吗?如果不是太麻烦的话,您能提供您得到的错误吗?表中的“s_id”列是主键吗?如果是这样的话,您应该会得到一些错误,因为数据库将不允许主键列为null。可能会有一百万个错误。数据库可能有限制,可能没有连接到数据库,硬盘中可能没有足够的空间,等等。。如果不显示错误日志,没有人可以帮助您。也不应该有人来帮你。