开发者

Issue in Adding data into database using hibernate

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.beginTransa开发者_如何学Goction();

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

Please let me know what is wrong. Thankx


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜