[英]Get Original object before update in hibernate's entity listeners
我正在嘗試在@PreUpdate
事件中實現一個比較原始和更新的 object 的實體監聽器。 我嘗試使用 Jpa 存儲庫通過 Id 從 DB 中查詢原始 object,以將其與事件中收到的 object 進行比較。 但它檢索相同的 object (我猜它返回 session 范圍內的 object)。
有沒有辦法從實體監聽器的數據庫中獲取原始的 object ?
JPA在EntityListener中不提供新舊state。
但是你可以使用 Hibernates 攔截器:
public static class LoggingInterceptor extends EmptyInterceptor {
@Override
public boolean onFlushDirty(
Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyNames,
Type[] types) {
LOGGER.debugv( "Entity {0}#{1} changed from {2} to {3}",
entity.getClass().getSimpleName(),
id,
Arrays.toString( previousState ),
Arrays.toString( currentState )
);
return super.onFlushDirty( entity, id, currentState,
previousState, propertyNames, types
);
}
}
在此示例中,您會看到當前的 state 和之前的 arrays 可以輕松進行比較。
請閱讀文檔: https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#events
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.