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

前端 未结 6 1385
猫巷女王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 07:04

    try this:

    String hql="update table set id=? where id=? ";
    Query query=HibernateSessionFactory.getSession().createQuery(hql);
    query.setInteger(0,1);
    query.setInteger(1,2);
    query.executeUpdate();
    HibernateSessionFactory.getSession().beginTransaction().commit();
    

    or just use sql:

    String sql = "update table set id = ? where id= ?"
    Session session = HibernateSessionFactory.getSession();  
    SQLQuery query = session.createSQLQuery(sql);
    query.setParameter(0, 1);
    query.setParameter(1, 2);  
    

提交回复
热议问题