Fixing SQL Injection in Hibernate

Note: This post is part of our series on “How to Fix SQL Injection Vulnerabilities“. The series contains examples on how to fix SQL Injection Vulnerabilities in various programming languages.

An SQL Injection attack is a code injection attack when input from an attacker reaches one of your databases without any filteration or validation. As a result, a malicious user can execute Read / Write / Delete / Update query in your database. In addition to this he can also run system level commands. The following example shows how SQL Injection can be prevented by in Hibernate

Subscription sub = (Subscription) sessionFactory.getCurrentSession()
 .createQuery("from Subscription sub where sub.verification = :verification")
 .setString("verification", verification)
 .uniqueResult();