Hibernate / SQLException:字段没有默认值 [英] Hibernate / SQLException: field doesn't have default value

查看:165
本文介绍了Hibernate / SQLException:字段没有默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下命令生成的mySQL表:

  CREATE TABLE`actors`(
`actorID` INT(11)NOT NULL,
`actorName` VARCHAR(255)NOT NULL,
PRIMARY KEY AUTO_INCREMENT(actorID)
);

映射类:

  @Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =actorID,length = 11,unique = true,nullable = false)
private int actorID;
....

错误:

 错误:字段'actorID'没有默认值

尝试创建一个新的Actor对象并将其保存到数据库时会出现此错误。



我试过使用其他生成策略,并且删除/重建表/整个数据库,根据类似问题的答案。

感谢您的时间,
Samuel Smith



编辑:滴该表使用shippi所示的SQL语法重新创建它似乎为我解决了问题。

/ p>

  @GeneratedValue(strategy = GenerationType.AUTO)
@Id
@Column(name =actorID )
private int actorID;

这可以解决您的问题。如果保存,则保留actorsID为null。



确保数据库正常工作,尝试编写插入语句而不插入标识并查看纯插入是否正常工作。如果直接插入正在数据库中工作,您可以开始排查休眠问题。



还有一点,我用定义列的同一行来定义PH和自动增量:

actorID INT(11)NOT NULL AUTO_INCREMENT PRIMARY KEY,



但它应该可以工作。 / p>

mySQL Table generated using:

CREATE TABLE `actors` (
    `actorID` INT(11) NOT NULL,
    `actorName` VARCHAR(255) NOT NULL,
     PRIMARY KEY AUTO_INCREMENT (actorID)
);

Mapped class:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "actorID", length = 11, unique = true, nullable = false)
private int actorID;
....

Error:

ERROR: Field 'actorID' doesn't have a default value

This error is hit when trying to create a new Actor object, and saving it to the database.

I've tried using other generation strategies, and dropping/rebuilding tables / entire database, as per the answers to similar questions. No luck so far.

Thank you for your time, Samuel Smith

EDIT: Dropping the table and recreating it using the SQL syntax shown by shippi seemed to solve the problem for me.

解决方案

Just try to use

@GeneratedValue(strategy=GenerationType.AUTO)
@Id
@Column(name = "actorID")
private int actorID;

This should fix your issue. Leave the actorID null if saving.

Ensure also that the DB works fine, try to write an insert statement without inserting the ID and lets see whether the pure insert is working. If the direct insert is working to the database you could start to troubleshoot hibernate.

And a small point, I use to define the PH and Auto increment in the same line where defining the column:

actorID INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,

but it should work either way.

这篇关于Hibernate / SQLException:字段没有默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆