Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate problems with Auto Increment ID MYSQL 5

So, I just stood up a Spring Hibernate app and I can't seem to get my mapping file right. I am using MySql 5 and an auto incrementing key. Here is the ID portion of my mapping file.

<hibernate-mapping>
     <class name="org.XXXXXXX.Contact" table="contact">
        <id name="id" column="id" type="int" unsaved-value="null">
            <generator class="native" />
        </id>

Here is the SQL generated

insert into contact (title, first_name, middle_name, last_name, suffix, job_title, dob, passport_number, passport_expiration, employer, dietary_restrictions, secondary_contact_fname, secondary_contact_lname, secondary_contact_mname, secondary_contact_title, secondary_contact_suffix, secondary_contact_job_title, emergency_contact_name, emergency_contact_phone, emergency_contact_notes, is_company) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Here is the important part of the stack trace:

org.hibernate.AssertionFailure: null id in org.XXXXXXX.Contact entry (don't flush the Session after an exception occurs)

I have tried setting the unsaved-value to "0" and "-1" and sending them over the wire. Any ideas on what I am doing wrong?

like image 338
Ryan H Avatar asked Dec 03 '09 08:12

Ryan H


People also ask

What happens when auto increment reaches limit MySQL?

What will happen if the MySQL AUTO_INCREMENT column reaches the upper limit of the data type? When the AUTO_INCREMENT column reaches the upper limit of data type then the subsequent effort to generate the sequence number fails.

Can we insert auto increment value in MySQL?

Syntax for MySQLMySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature. By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record. VALUES ('Lars','Monsen'); The SQL statement above would insert a new record into the "Persons" table.

How do you get the last ID from a table if it's set to auto increment?

To get the next auto increment id in MySQL, we can use the function last_insert_id() from MySQL or auto_increment with SELECT. Creating a table, with “id” as auto-increment.

Why is auto increment disabled in MySQL workbench?

This was likely due to that although "INT" had been entered for the "Data Type" box, MySQL Workbench hadn't updated the available field options & was still using UI logic for a non-INT column (where auto increment isn't available).


Video Answer


1 Answers

You have to remember that Hibernate is a persistence layer and needs to be able to keep track of where an object is in the database. So when it does an insert, it actually will need to query the auto-increment counter to see what the next ID should be. It then inserts the ID into the object and inserts the object into the database. So for hibernate to do in insert, it uses needs to do a select first (unless you're using some sort of GUID generated by the application). When using mySQL auto-increment, use the "identity" generator.

Explanation of the various generators:

http://www.roseindia.net/hibernate/hibernateidgeneratorelement.shtml

A hibernate XML code snippet:

 <id name="id" type="long" unsaved-value="null" >
    <column name="uid" not-null="true"/>
    <generator class="identity"/>
 </id>
like image 198
davidemm Avatar answered Sep 21 '22 00:09

davidemm