Search code examples
javapostgresqlspring-bootjpajsonb

How to use jsonb_set in jpa repository, for passing dynamic strings?


postgres command works in pgadmin4 but not in java code

    String toAdd = "case_data->'business' || '{\"l\":\"cpaz\"}'";
this.orchestrateRepo.updateColumn(toAdd, case_id);



 @Query(value = "Update onboarding.onboarding_cases set case_data = jsonb_set(case_data, '{business}', ?1 )where case_id=?2", nativeQuery = true)
        void updateColumn(String toAdd, BigInteger case_id);

I am passing a string toAdd,i want to insert the value dynamically..but it gives error

org.postgresql.util.PSQLException: ERROR: function jsonb_set(jsonb, unknown, character varying) does not exist
  Hint: No function matches the given name and argument types. You might need to add explicit type casts.

the query works fine if i write it like this

 @Query(value = "Update onboarding.onboarding_cases set case_data = jsonb_set(case_data, '{business}', case_data->'business' || '{"t":"cpaz"}' )where case_id=?2", nativeQuery = true)
        void updateColumn(BigInteger case_id);

What should i do


Solution

  • Finally solved this issue. Posting an answer for those looking.

    Only passing key value.

    String toAdd = "value";
    

    Changed the query to:

    @Query(value = "Update onboarding.onboarding_cases set case_data=jsonb_set(case_data,'{business,key)}',to_jsonb(?1)) where case_id=?2", nativeQuery = true)
        void updateColumn(String toAdd, BigInteger case_id);