Hibernate - update the primary key 'id' column in the table

前端 未结 6 1387
猫巷女王i
猫巷女王i 2021-01-18 06:35

In my Java applicaion, I am using hibernate .hbm file to access database; Is this possible to update the primary key \'id\' column in the table; Where the \'id\' column in m

6条回答
  •  借酒劲吻你
    2021-01-18 06:45

    Basically, Hibernate does not allow to update or change the primary key of a database entity. The reason is the entity data that you fetch from database via some query or .get or .load method goes into persistent context layer. So from hibernate perspective updating of such persistent entity object means deletion of the older one from database and creation of a new one.
    It better to make normal update query like

    Query query=HibernateSessionFactory.getSession().createQuery(update table set id=? where id=?);
    query.executeUpdate();
    HibernateSessionFactory.getSession().beginTransaction().commit();
    

提交回复
热议问题