tags:

views:

48

answers:

1

Hi ,

I am getting the following exception while adding data into database:

org.hibernate.HibernateException: The database returned no natively generated identity value

I am using the following code:

Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();

session.save(user);
logger.info("Successfully data insert in database");
tx.commit();
isSaved = true;

Please let me know what is wrong. Thankx

+1  A: 

It seems as if the database doesn't support the identity id generator. Based on your mapping you are probably using the userName as the ID column, which would mean that you probably want to set the generator class to assigned since the username (= id) will be picked manually (and not auto generated by the database):

<hibernate-mapping>
    <class name="com.test.User" table="user">
        <id name="userName" column="user_name">
            <generator class="assigned" />
        </id>
        <property name="userCode">
        <column name="user_code" />
        </property>
    </class>
</hibernate-mapping>
Daff
Thankx a lot it worked
sarah