0

I have some entity AuditEntity. I need get eventType value by entityId.

How to make the dealState field in AuditEntity always be zero when the findFirstByEntityIdOrderByCreatedTimeDesc method is executed, regardless of the value of this field in the database?

How correct would it be to use @Lazy (import org.springframework.context.annotation.Lazy) if the dealState field is not the key for another table?

@Entity
@Table(name = "audit")
class AuditEntity(
    @Id
    override var id: Long? = null,

    @Basic
    @Column(name = "entity_id")
    val entityId: Long,

    @Basic
    @Enumerated(EnumType.STRING)
    @Column(name = "event_type")
    val eventType: AuditEventType,

    @Type(type = "jsonb")
    @Column(name = "deal_state", columnDefinition = "jsonb")
    val dealState: Deal? = null
) : BaseEntity<Long>()
@Repository
interface AuditRepository : JpaRepository<AuditEntity, Long> {
    fun findFirstByEntityIdOrderByCreatedTimeDesc(entityId: Long): AuditEntity?
}

1 Answer 1

1

How to make the dealState field in AuditEntity always be zero when the findFirstByEntityIdOrderByCreatedTimeDesc method is executed, regardless of the value of this field in the database?

Since you want a specific field to be a specific value for a specific query, you need to do it yourself. I would suggest to wrap the AuditRepository in another class which will do it for you. A possible solution could look like this:

@Entity
@Table(name = "audit")
data class AuditEntity( // make it a data class
    @Id
    override var id: Long? = null,

    @Basic
    @Column(name = "entity_id")
    val entityId: Long,

    @Basic
    @Enumerated(EnumType.STRING)
    @Column(name = "event_type")
    val eventType: AuditEventType,

    @Type(type = "jsonb")
    @Column(name = "deal_state", columnDefinition = "jsonb")
    val dealState: Deal? = null
) : BaseEntity<Long>()

@Repository
interface AuditRepository : JpaRepository<AuditEntity, Long> {
    fun findFirstByEntityIdOrderByCreatedTimeDesc(entityId: Long): AuditEntity?
}

@Repository
class CustomAuditRepository(
    private val auditRepository: AuditRepository
) : JpaRepository<AuditEntity, Long> by auditRepository  { // delegate implementation and override only the things you want to be changed

    override fun findFirstByEntityIdOrderByCreatedTimeDesc(entityId: Long): AuditEntity? =
        auditRepository.findFirstByEntityIdOrderByCreatedTimeDesc(entityId)?.copy(dealState = ZERO)
}
Sign up to request clarification or add additional context in comments.

4 Comments

But first, anyway, I get the whole Entity, put the value of deal_state in dealState, and only then change it to null? It is fundamentally important for me that I do not record deal_state in dealState at any stage.
@NeverSleeps I don't understand what you want. What you you mean by "do not record deal_state in dealState at any stage"?
I need the json that is contained in the deal_state column not to be parsed into the Deal object. With this parsing in some cases there will be errors. There is no way to fix Deal now, so I'm looking for ways to get eventType but exclude reading from deal_state column.
I apologize if this moment was not clear in my question.

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.