我想出了一個辦法來做到這一點,所以我會張貼它的使用情況下,其他人。此代碼循環遍歷所有「舊狀態」屬性,對於任何持久集合,都會獲取以前的內容「快照」。然後它封裝此不可修改的Collection好措施在:
public void onPostUpdate(PostUpdateEvent event)
{
for (Object item: event.getOldState())
{
Object previousContents = null;
if (item != null && item instanceof PersistentCollection)
{
PersistentCollection pc = (PersistentCollection) item;
PersistenceContext context = session.getPersistenceContext();
CollectionEntry entry = context.getCollectionEntry(pc);
Object snapshot = entry.getSnapshot();
if (snapshot == null)
continue;
if (pc instanceof List)
{
previousContents = Collections.unmodifiableList((List) snapshot);
}
else if (pc instanceof Map)
{
previousContents = Collections.unmodifiableMap((Map) snapshot);
}
else if (pc instanceof Set)
{
//Set snapshot is actually stored as a Map
Map snapshotMap = (Map) snapshot;
previousContents = Collections.unmodifiableSet(new HashSet(snapshotMap.values()));
}
else
previousContents = pc;
//Do something with previousContents here
}
你怎麼比較集合?我沒有看到'significantChange`方法足夠了。 – 2012-08-14 14:55:20