Hibernate中Caused by: java.sql.SQLException: Field 'address_id'doesn't have a default value

在学习Hibernate中,相信很多人都遇到过:

Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement

Caused by: java.sql.SQLException: Field ‘address_id‘ doesn‘t have a default value

很是纳闷,明明我的address_id是主键,为毛还提示我不存在默认值?主键不是应该自动生成值的吗?

解决是一方面,知道为什么报错才是最重要的。

错误原因:

以下拿我这个错误来讲(address_id)如图:

import java.util.HashSet;
import java.util.Set;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
@Entity
@Table(name="personSet_inf")
public class PersonSet {
	@Id
	@Column(name="person_id")
/*	@GeneratedValue(strategy=GenerationType.IDENTITY)*/
	private Integer id;
	private String name;
	private String age;

在开始我虽然指定了主键和主键名,但是没有将address_id作为主键设置为@GeneratedValue(strategy=GenerationType.IDENTITY)自增类型(上面代码以注解),这时运行程序控制台就会报错:

Exception in thread "main" org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.mao.PersonSet

虽然报这个错误,但是hibernate已经创建了一张表(personSet_inf),注意这时表的主键不是自增长模式,

而且你想要插入的数据并没有插入,这时你发现了错误,然后在程序中给主键添加了自增长类型,重新运行项目,就会报错:

Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement

Caused by: java.sql.SQLException: Field 'address_id' doesn't have a default value

原因就是:你虽然在代码里修改了主键类型,但是你数据库表已经建好了,它里面的主键依然不是自增长类型,所id不会自动赋值,然后报错:id没有默认值。

解决方案:

先确认程序已经标明主键模式,然后将数据库表删除,重新运行程序,创建带有主键模式的数据表,这样就可以了,而且数据也成功的插入进去。

Hibernate中Caused by: java.sql.SQLException: Field 'address_id'doesn't have a default value

时间: 2024-07-28 21:29:36

Hibernate中Caused by: java.sql.SQLException: Field 'address_id'doesn't have a default value的相关文章

Caused by: java.sql.SQLException: Field 'id' doesn't have a default value

1.错误描述 org.hibernate.exception.GenericJDBCException: error executing work at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(Sql

Caused by: java.sql.SQLException: Field 'category_id' doesn't have a default value

### The error may involve com.qingmu.core.dao.ad.ContentDao.insertSelective-Inline ### The error occurred while setting parameters ### SQL: insert into tb_content ( title, url, pic, sort_order ) values ( ?, ?, ?, ? ) ### Cause: java.sql.SQLException:

hibernate 出现Caused by: java.sql.SQLException: Column 'id' not found.异常

用hibernate进行映射查询时,出现Caused by: java.sql.SQLException: Column 'id' not found 异常,检查数据库表及映射都有id且已经正确映射,google后发现原因为: Your query doesn't return a field named id即查询sql中没有查询出主键id列,但返回结果集中用到,故出现异常特此记录. 详细解答见下: http://stackoverflow.com/questions/34164411/cau

java.sql.SQLException: Field 'id' doesn't have a default value(用eclipse操作数据库时报了这种奇怪的错误)的原因与解决方法

1.错误原因 由于id在数据库表中是作为主键,但是在插入的过程中,没有给予数值,并且没有让其自增 2.解决办法 修改数据库表中的id,让其自增(在插入的过程中,不插入id数据时) java.sql.SQLException: Field 'id' doesn't have a default value(用eclipse操作数据库时报了这种奇怪的错误)的原因与解决方法 原文地址:https://www.cnblogs.com/zyt-bg/p/8385972.html

Caused by: java.sql.SQLException: Operand should contain 1 column(s)

1.错误描述 [ERROR:]2015-05-05 15:48:55,847 [异常拦截] org.hibernate.exception.DataException: error executing work at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:135) at org.hibernate.exception.internal.

Caused by: java.sql.SQLException: ResultSet is from UPDATE. No Data.

1.错误描述 org.hibernate.exception.GenericJDBCException: error executing work at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(Sql

Caused by: java.sql.SQLException: Incorrect integer value: '' for column 'clientId' at row 41

1.错误描述 [ERROR:]2015-06-10 13:48:26,253 [异常拦截] oa.exception.ExceptionHandler org.hibernate.exception.GenericJDBCException: error executing work at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.jav

Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0

1.错误描述 org.hibernate.exception.GenericJDBCException: error executing work at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(Sql

Caused by: java.sql.SQLException: Couldn't perform the operation commit

1.错误描述 Caused by: java.sql.SQLException: Couldn't perform the operation commit: You can't perform any operations on this connection. It has been automatically closed by Proxool for some reason (see logs). at org.logicalcobwebs.proxool.WrappedConnecti