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:

  1. How can I retrieve the specific changes made to an entity in a single method call?

  2. Is there a built-in way to extract changed attributes with their old and new values?

  3. 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 application

  • I'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/JPA

  • I'm using Hibernate Envers with @Audited entities

  • Want 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?

本文标签: