I'm using spring data jpa and querydsl. I don't want to update directly through JPA because I have to submit a complete entity, which introduces an unnecessary query. So, I use querydsl instead:
val qRecord = QCoupon.coupon
jpaQueryFactory.update(qRecord)
.set(qRecord.useState, Coupon.STATE_ORDERED)
.set(qRecord.useTime, useTime)
.where(qRecord.id.eq(recordId))
.execute()
But the query after that in the same transcation cannot get the latest data, I think it has something to do with the first-level cache.
Adding @Modifying
doesn't work because I didn't use @Query
, the first annotation will be ignored.
I know EntityManager.clear()
can fix that. But it looks too heavy, I'm not sure this is a good idea.
This post explained the cause of the problem very well but not my case -- I have rejected both two answers.
@Modifying
is ignored.EntityManager.clear()
looks too heavy, andfind()
also need an entity.