我看过一些讨论通过命名参数防止 SQL 注入(inject)的主题,但是像这样的 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 语句使用绑定(bind)变量。为每个实体缓存这些语句,以避免每次需要时都创建它们,并且在执行它们时只提供绑定(bind)变量值。
您可以 enable SQL logging检查生成的 SQL。
关于java - Hibernate 和 SQL 注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42625452/