XXX.hbm.xml 配置:<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native"></generator>
</id>
数据库mysql 语句:create table headmaster(id int primary key,name varchar(20),age int );
测试语句:public class Test {
public static void main(String[] args) {
Headmaster h = new Headmaster();
h.setAge(30);
h.setName("zlw");
Session s = HibernateSessionFactory.getSession();
Transaction t = s.beginTransaction();
s.save(h);
System.out.println("OK");
t.commit();
s.close();
}
}
HibernateSessionFactory已经自动创建ok;
出错信息:Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [com.test.Headmaster】.............
Caused by: java.sql.SQLException: Field 'id' doesn't have a default value;
<column name="id" />
<generator class="native"></generator>
</id>
数据库mysql 语句:create table headmaster(id int primary key,name varchar(20),age int );
测试语句:public class Test {
public static void main(String[] args) {
Headmaster h = new Headmaster();
h.setAge(30);
h.setName("zlw");
Session s = HibernateSessionFactory.getSession();
Transaction t = s.beginTransaction();
s.save(h);
System.out.println("OK");
t.commit();
s.close();
}
}
HibernateSessionFactory已经自动创建ok;
出错信息:Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [com.test.Headmaster】.............
Caused by: java.sql.SQLException: Field 'id' doesn't have a default value;
hibernate不知道你的id是从什么开始的看异常Caused by: java.sql.SQLException: Field 'id' doesn't have a default value;id没有设置默认值