Home > other >  How to handle JPA Attribute converters for update query
How to handle JPA Attribute converters for update query

Time:09-06

So in my application there is an entity

@Entity
data class SomeEntity(
    val name: String,

    @Convert(converter =SpecConverter::class)
    val spec: Spec,

    @CreationTimestamp
    val createdAt: LocalDateTime? = null,

    @UpdateTimestamp
    val updatedAt: LocalDateTime? = null,

)

It works fine while saving as all I need to do is

SomeEntityRepository.save (
 SomEntity(
   name="L",
   spec=SpecConverter().convertToEntityAttribute(request.specRequest),
 )
)

but how would I manage to run update queries for updating the spec of the entity, I tried doing this but it keeps giving me error

    @Transactional
    @Modifying
    @Query("update some_entity e set e.spec = :spec where name = :name")
    fun updateReferralProgram(name: String, spec: Spec): Unit

The error I was getting was

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: some_entity is not mapped [update some_entity e set e.spec = :spec where name = :name ]

CodePudding user response:

In HQL query, you should write your Entity class name instead of Table name in your query like:

@Query("update SomeEntity e set e.spec = :spec where name = :name")
  • Related