0

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

3
  • We are going to need more information. Are you using Annotations or XML mapping files? Can we see the relevant mappings? Can we see the relevant DB schema? Commented Apr 12, 2010 at 6:58
  • i am using hbm <hibernate-mapping> <class name="com.test.User" table="user"> <id name="userName" column="user_name"> <generator class="identity" /> </id> <property name="userCode"> <column name="user_code" /> </property> </class> </hibernate-mapping> Commented Apr 12, 2010 at 7:09
  • User bean has userName and userCode and database has user_name and user_code as columns Commented Apr 12, 2010 at 7:10

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

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.