我是 hibernate 新手。我正在使用 SessionFactory 来获取交易的 session ,我在搜索后发现的一种方法是使用设置参数设置几个字段,即
Query query = getCurrentSession().createSQLQuery(
"UPDATE table_name set field1=:f1 where ID=:id");
query.setParameter("f1", f1);
query.setParameter("id", id);
但我想更新整行。我已经在实体类中设置了值,但是有没有办法根据 id 将整个实体类的值传递到数据库,id 是我要更新的表的主键。
最佳答案
您已经拥有 Hibernate 实体对象中存在的所有数据吗?然后直接调用session即可:
getCurrentSession().save(myEntity);
创建一个新对象,或
getCurrentSession().update(myEntity);
更新现有行。
如果不确定,您可以使用:
getCurrentSession().saveOrUpdate(myEntity);
关于java - 如何在 hibernate 中根据 id 更新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36286036/