Hibernate - Custom insert into database

后端 未结 2 849
盖世英雄少女心
盖世英雄少女心 2021-02-08 00:17

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\'))


        
相关标签:
2条回答
  • 2021-02-08 00:39

    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();
    
    0 讨论(0)
  • 2021-02-08 00:43

    Like Nathan Feger mentioned, named parameters are much cleaner and safer. In this case, the statement can be executed with the following code:

    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();
    
    0 讨论(0)
提交回复
热议问题