I have the following JPA repo with insert query method:
public interface CMRepository extends JpaRepository <CMark, String> {
@Transactional
@Modifying
@Query(value = "INSERT INTO c_mark(class_fk, s_con_fk, dis_con_fk, rto_fk, version, classification) "
"values(:class_fk, :s_con_fk, :dis_con_fk, :rto_fk, :version, :classification)", nativeQuery = true)
int insertCMark(@Param("class_fk") String class_fk, @Param("s_con_fk") String s_con_fk, @Param("dis_con_fk") String dis_con_fk,
@Param("rto_fk") String rto_fk, @Param("version") int version, @Param("classification") String classification);
}
When executed, I get the following error:
PSQLException: ERROR: column "classification" is of type classification but expression is of type character varying
The database has that field defined as USER-DEFINED. I believe from similar posts I am supposed to cast the classification field? If so, I am not sure how to do it. Thanks for any help!
CodePudding user response:
ok, figured this out. I had to cast like this:
public interface CMRepository extends JpaRepository <CMark, String> {
@Transactional
@Modifying
@Query(value = "INSERT INTO c_mark(class_fk, s_con_fk, dis_con_fk, rto_fk, version, classification) "
"values(:class_fk, :s_con_fk, :dis_con_fk, :rto_fk, :version, :classification\\:\\:classification)", nativeQuery = true)
int insertCMark(@Param("class_fk") String class_fk, @Param("s_con_fk") String s_con_fk, @Param("dis_con_fk") String dis_con_fk,
@Param("rto_fk") String rto_fk, @Param("version") int version, @Param("classification") String classification);
}
(notice the required escapes)