Is createNativeQuery() safe for SQL injection if used as in:
@ManagedBean @ViewScoped public class UserController { @PersistenceContext private EntityManager em; public User register(User u) { Query query = em.createNativeQuery("SELECT r1_register(?,?,?,?,?,?,?)"); short i = 0; query.setParameter(++i, u.getUsername()); query.setParameter(++i, u.getPassword()); query.setParameter(++i, u.getName()); query.setParameter(++i, u.getSurname()); query.setParameter(++i, u.getEmail()); query.setParameter(++i, u.getBirthdate()); query.setParameter(++i, u.getPhoneNumber()); int id = (int) query.getSingleResult(); if (id != 0) u.setIduser(id); return u; } }
r1_register is a stored function that executes an INSERT and returns the identifier of the newly inserted user. This will be equivalent to:
public User register(User u) { em.persist(u);
u in both cases filled by the user. Finally, the default transaction?
EDIT: procedure:
CREATE DEFINER=`root`@`localhost` FUNCTION `r1_register`(username VARCHAR(45), _password VARCHAR(45), _name VARCHAR(45), surname VARCHAR(45), _email VARCHAR(45), _birthdate DATE, phone_number VARCHAR(10) ) RETURNS int(11) BEGIN
source share