利用注解映射表
@Entity
public class teacher {
private int id;
private String name;
private String title;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
} 利用事务添加信息
teacher t=new teacher();
//t.setId(1);
t.setName("t1");
t.setTitle("it");
Configuration cf=new AnnotationConfiguration();
SessionFactory sf=cf.configure().buildSessionFactory();
Session ss=sf.openSession();
ss.beginTransaction(); 但出现了这样的问题( java.sql.SQLException: 不能将值 NULL 插入列 'id',表 'hibernate.dbo.teacher';列不允许有 Null 值。INSERT 失败。)
而且在后台打印出了这样的语句 (Hibernate: insert into teacher (id, name, title) values (null, ?, ?)) 问题(为什么我自动增长的id的不是int类型的呢?) 我表中已经设置了id为主键,int类型和自增长的了
@Entity
public class teacher {
private int id;
private String name;
private String title;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
} 利用事务添加信息
teacher t=new teacher();
//t.setId(1);
t.setName("t1");
t.setTitle("it");
Configuration cf=new AnnotationConfiguration();
SessionFactory sf=cf.configure().buildSessionFactory();
Session ss=sf.openSession();
ss.beginTransaction(); 但出现了这样的问题( java.sql.SQLException: 不能将值 NULL 插入列 'id',表 'hibernate.dbo.teacher';列不允许有 Null 值。INSERT 失败。)
而且在后台打印出了这样的语句 (Hibernate: insert into teacher (id, name, title) values (null, ?, ?)) 问题(为什么我自动增长的id的不是int类型的呢?) 我表中已经设置了id为主键,int类型和自增长的了
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货