Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate - Custom insert into database

I write this post to know if someone knows how to do this:

I want to do this insert:

INSERT INTO TABLA (CAMPO1, CAMPO2) VALUES (?, crypt(?,'cdp'))

Crypt is a function stored in my database and the insert I would want to do it in my code. Actually when I want to insert something in the database I use:

getHibernateTemplate().persist(obj);

But I want to do a "custom" insert, because I need to use that function.

I am using hibernate + annotations:

@org.hibernate.annotations.SQLInsert (sql = "INSERT INTO TABLA (CAMPO1, CAMPO2) VALUES (?, crypt(?,'cdp'))")

But the key 'cdp' must be readed from a file, so this solution doesn't work for me.

I want to use a method on my code to execute a SQL query (INSERT query)

like image 298
Michel Avatar asked Sep 14 '10 08:09

Michel


1 Answers

Here's a solution:

Query query = getSession().createSQLQuery("INSERT INTO TABLA (CAMPO1, CAMPO2) VALUES (:valor1, encripta(:valor2, :key))");
query.setParameter("valor1", valor1);
query.setParameter("valor2", valor2);
query.setParameter("key", key);
query.executeUpdate();
like image 183
Michel Avatar answered Oct 20 '22 14:10

Michel