休眠和 SQL 注入

Hibernate and SQL injection

我看过一些讨论通过命名参数防止 SQL 注入的主题,但是像

这样的 hibernate 语句怎么样?
currentSession().update(object);

currentSession().save(object)?

这些安全吗?或者总是使用像

这样的命名参数更安全

currentSession().createQuery("update Object set field=:field where id=:id").setParameter("field", field).setParameter("id", id).executeUpdate()?

它们是安全的,Hibernate 对实体 CRUD 语句使用绑定变量。为每个实体缓存语句,以避免每次需要时都创建它们,并且在执行时仅提供绑定变量值。

您可以enable SQL logging检查生成的SQL。