admin管理员组文章数量:1399297
In Python with SQLAlchemy, I can easily track changes to an entity using inspect()
:
from sqlalchemy import inspect
# After modifying an entity
changes = inspect(entity)
changes_dict = {
attr.key: {
'old': attr.history.deleted[0] if attr.history.deleted else None,
'new': attr.history.added[0] if attr.history.added else None
} for attr in changes.attrs if attr.history.has_changes()
}
In my Kotlin/Spring Boot application with JPA, Hibernate, and Envers, I want to achieve a similar functionality. Here's a concrete example:
@Entity
@Audited
data class PaymentLink(
@Id val id: Long,
@Column var amount: Int,
@Column var status: String
)
@Service
class PaymentLinkService {
@Transactional
fun updatePaymentLink(paymentLinkId: Long) {
val paymentLink = paymentLinkRepository.findById(paymentLinkId)
// Update some fields
paymentLink.amount = 200
paymentLink.status = "updated"
// Save the entity
paymentLinkRepository.save(paymentLink)
// I want to get:
// {
// "amount": { "oldValue": 100, "newValue": 200 },
// "status": { "oldValue": "active", "newValue": "updated" }
// }
}
}
Specific Requirements:
Language: Kotlin
Framework: Spring Boot, JPA, Hibernate Envers
Goal: Get a map of changed attributes with their old and new values
Already using
@Audited
entities
What I've Tried:
Manual comparison of original and updated states
Custom change tracking interceptors
Reflection-based solutions
Key Questions:
How can I retrieve the specific changes made to an entity in a single method call?
Is there a built-in way to extract changed attributes with their old and new values?
Can this be done generically across different entity types?
I'm looking for a clean, straightforward method similar to SQLAlchemy's inspect()
that works with Hibernate Envers and provides minimal boilerplate code.
I want to extract the differences automatically whenever I do
repo.save(updated_obj)
in my Kotlin/Spring Boot applicationI'm looking for a way to retrieve:
Which attributes were changed
The old values of those attributes
The new values of those attributes
I've tried one approach:
- Manual comparison of entity states before and after save
Specific Expectation:
When I save an updated entity, I want a simple mechanism to automatically capture:
{ "amount": { "oldValue": 100, "newValue": 200 }, "status": { "oldValue": "active", "newValue": "updated" } }
In Python's SQLAlchemy, this is trivial with
inspect()
, but I can't find an equivalent straightforward method in Hibernate/JPAI'm using Hibernate Envers with
@Audited
entitiesWant a generic solution that works across different entity types
Preferably with minimal boilerplate code
Is there a standard way in Hibernate/JPA to track and retrieve these changes during or immediately after the save operation?
本文标签:
版权声明:本文标题:spring boot - How to retrieve detailed entity changes in HibernateJPA with Envers, similar to SQLAlchemy's inspect()? - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744210718a2595404.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论