org.springframework.orm.hibernate3.HibernateJdbcException: could not insert: [com.ssh.domain.Revert]
Caused by: java.sql.SQLException: Field 'id' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
...
at com.ssh.action.user.ArticleAction.revertArticle(ArticleAction.java:229)
MySQL 表 `revert` 的主鍵 `id` 列未設置 `AUTO_INCREMENT`,且 Hibernate 映射未指定主鍵生成策略(或策略為 `assigned`),導致插入時 `id` 為 `null`,資料庫拋錯。
ALTER TABLE revert MODIFY id INT PRIMARY KEY AUTO_INCREMENT;
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/> <!-- 或 identity -->
</id>
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
重新部署測試插入,觀察 `id` 自動生成無異常。
主鍵生成策略與資料庫定義不一致是導致此錯誤的核心原因,統一雙邊配置即可解決。
出處:bianceng.cn