3

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.

2
  • Can you insert into the table via mysql workbench without specifying actorID? Commented Jan 20, 2014 at 21:10
  • Errrhh... I bumped into this problem a while ago. It was really hard to find. What happens if you blow away the @Column annotation? It is not needed IMHO. Commented Jan 20, 2014 at 21:15

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Minor detail, but int cannot be null. It wil be initialized to zero.
Okay, so I changed the variable mapping to what you described above, and it had no effect. However, I dropped the entire database, and rebuilt all my tables, this time using SQL syntax similar like you've shown here, and it works perfectly. I don't think it's the syntax change itself, but because it was fairly different from before, it forced a refresh somewhere that fixed the problem (AUTO_INCREMENT was added later originally, so maybe the new database just hadn't been cached or something... I'm not really sure)
I had the same issue, Solved it empty the entire database to empty and re run the application. Everything is no working fine.
3

Your error is in your table definition.

Must be:

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

You missed AUTO_INCREMENT in actorID.

2 Comments

This will still cause the same error since actorName also doesn't has a default value defined.
When you create a new record in a database, you usually don't set manually the ID. But you must set all the other columns, specially the no null columns. Makes no sense to have a default value for the column actorName.
1

I had the same issue, Solved it empty the entire database to empty and re run the application. Everything is no working fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.