Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

889
Views
¿Cómo usar JPA Query para insertar datos en db?

Tengo un problema con mi declaración preparada pero no puedo averiguar dónde está el error. Estoy tratando de insertar un enlace URI en la base de datos.

 @Repository public interface LoggerDao extends CrudRepository<Logger, Long> { @Query("select t from Logger t where t.user.id=?#{principal.id}") List<Logger> findAll(); @Modifying @Query(value = "insert into Logger t (t.redirect, t.user.id) VALUES (:insertLink,?#{principal.id})", nativeQuery = true) @Transactional void logURI(@Param("insertLink") String insertLink);

Error

 2017-03-11 19:52:59.157 WARN 65154 --- [nio-8080-exec-8] ohengine.jdbc.spi.SqlExceptionHelper : SQL Error: 42001, SQLState: 42001 2017-03-11 19:52:59.157 ERROR 65154 --- [nio-8080-exec-8] ohengine.jdbc.spi.SqlExceptionHelper : Syntax error in SQL statement "INSERT INTO LOGGER T[*] (T.REDIRECT, T.USER.ID) VALUES (?,?) "; expected "., (, DIRECT, SORTED, DEFAULT, VALUES, SET, (, SELECT, FROM"; SQL statement: insert into Logger t (t.redirect, t.user.id) VALUES (?,?) [42001-190] 2017-03-11 19:52:59.181 ERROR 65154 --- [nio-8080-exec-8] oaccC[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [insert into Logger t (t.redirect, t.user.id) VALUES (?,?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement] with root cause org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "INSERT INTO LOGGER T[*] (T.REDIRECT, T.USER.ID) VALUES (?,?) "; expected "., (, DIRECT, SORTED, DEFAULT, VALUES, SET, (, SELECT, FROM"; SQL statement: insert into Logger t (t.redirect, t.user.id) VALUES (?,?) [42001-190] at org.h2.engine.SessionRemote.done(SessionRemote.java:624) ~[h2-1.4.190.jar:1.4.190] at org.h2.command.CommandRemote.prepare(CommandRemote.java:68) ~[h2-1.4.190.jar:1.4.190] at org.h2.command.CommandRemote.<init>(CommandRemote.java:45) ~[h2-1.4.190.jar:1.4.190] at org.h2.engine.SessionRemote.prepareCommand(SessionRemote.java:494) ~[h2-1.4.190.jar:1.4.190] at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1188) ~[h2-1.4.190.jar:1.4.190] at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:72) ~[h2-1.4.190.jar:1.4.190] at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:276) ~[h2-1.4.190.jar:1.4.190] at org.apache.tomcat
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Logré resolver el problema. Agregué una identificación a los parámetros para que pueda pasar la identificación del usuario, usando Principal en el controlador.

 @Repository public interface LoggerDao extends CrudRepository<Logger, Long> { @Query("select t from Logger t where t.user.id=?#{principal.id}") List<Logger> findAll(); @Modifying @Query(value = "insert into Logger (redirect,user_id) VALUES (:insertLink,:id)", nativeQuery = true) @Transactional void logURI(@Param("insertLink") String insertLink, @Param("id") Long id);
over 4 years ago · Santiago Trujillo Report

0

Hay una manera de hacer inserciones usando consultas obj (no nativas) (usando @Query & @Modifying) pero depende de la base de datos que esté usando. A continuación funcionó para mí en Oracle (usando la tabla Dual):

 @Repository public interface DualRepository extends JpaRepository<Dual,Long> { @Modifying @Query("insert into Person (id,name,age) select :id,:name,:age from Dual") public int modifyingQueryInsertPerson(@Param("id")Long id, @Param("name")String name, @Param("age")Integer age); }

Aquí hay un enlace que muestra en la parte inferior qué base de datos admite stmts seleccionados sin una cláusula from: http://modern-sql.com/use-case/select-without-from

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!