在我的Java应用程序中,我正在使用hibernate .hbm文件访问数据库。是否可以更新表中的主键“ id”列;我的.hbm文件中的“ id”列如下所示:
<hibernate-mapping package="org.jems.user.model"> <class name="Student_Details" table="t_student"> <id name="id" type="int" column="id"> <generator class="increment"/> </id> <property name="name" column="name" type="string" unique="true" not-null="true" /> <property name="description" column="description" type="string" /> <property name="comments" column="comments" type="string" /> <property name="active" column="isActive" type="boolean" not-null="true" /> </class> </hibernate-mapping>
尝试这个:
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();
或只使用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);