0

Is there a way to pass the whole sql statement or a part of it to @Query annotation?

@Modifying
@Query(value = "INSERT INTO my_table :query", nativeQuery = true)
@Transactional
void test(@Param("query") String query);
6
  • You can't execute INSERT that way. But what's your use case? Commented Aug 5, 2022 at 11:53
  • I need to make UPSERT into PostgreSQL. And some fields can be NULL in DAO, which cause error on inserting into column of some types, for instance, BIGINT and TIMESTAMP Commented Aug 5, 2022 at 11:57
  • 1
    But then you can not use JPA. Checkout the JdbcTemplate Commented Aug 5, 2022 at 12:05
  • It's a pity. Thank you for your comment Commented Aug 5, 2022 at 12:13
  • Why can't you simply use the Repository.save function? Commented Aug 5, 2022 at 12:37

1 Answer 1

1

I think a viable option for you would be to use the entity manager, instead of the repository. You can do it using NativeQuery and its executeUpdate method. Please check:

Inserting in my JPA using entity manager native query

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.