我有三张表,一张表示用户表,属性为username(主键,varchar型)和password(varchar);第二张表为blackhole主键为blackholeid(int型),username(外键),blackholecontent(varchar),datetime(datetime);还有一张joke表和blackhole差不多。我的dao层继承了HibernateDaoSupport并使用了getHibernateTemplate().save(user),结果报了如下错误:
Hibernate operation: could not insert: [com.model.User]; uncategorized SQLException for SQL [insert into blackhole.user (password) values (?)]; SQL state [HY000]; error code [1364]; Field 'username' doesn't have a default value; nested exception is java.sql.SQLException: Field 'username' doesn't have a default value
org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: could not insert: [com.model.User]; uncategorized SQLException for SQL [insert into blackhole.user (password) values (?)]; SQL state [HY000]; error code [1364]; Field 'username' doesn't have a default value; nested exception is java.sql.SQLException: Field 'username' doesn't have a default value
java.sql.SQLException: Field 'username' doesn't have a default value
------解决方案--------------------
删除user.hbm.xml中username配置的 <generator class="native" />
------解决方案--------------------
你和下面的链接里比较下,看是不是你的数据库没有设置主键自增,http://zhidao.baidu.com/link?url=el8O2GylNuKhP-u5MovI-v6BhgYAP6iuOkeG0WcZFur77RdlMKbjjMvGmdd3Jvqb25Yc1p3_RshSD9OaJ6A2aa