Skip to content

Commit 355dda9

Browse files
committed
[DURACOM-474] Fix
1 parent 83fe513 commit 355dda9

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

dspace-api/src/main/java/org/dspace/app/customurl/consumer/CustomUrlConsumer.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,18 @@ private String extractMetadataValue(Item item, String metadataFieldName) {
288288
*/
289289
private void assignCustomUrlToItem(Context context, Item item, String customUrl) {
290290
log.info("Assigning custom URL '{}' to item {}", customUrl, item.getID());
291-
customUrlService.replaceCustomUrl(context, item, customUrl);
291+
// Evict and re-fetch the item to ensure a clean managed entity.
292+
// During event dispatch the item may have detached associations
293+
// that cause "detached entity passed to persist" errors.
294+
try {
295+
context.uncacheEntity(item);
296+
Item managedItem = itemService.find(context, item.getID());
297+
if (managedItem != null) {
298+
customUrlService.replaceCustomUrl(context, managedItem, customUrl);
299+
}
300+
} catch (SQLException e) {
301+
throw new RuntimeException("Failed to reload item " + item.getID(), e);
302+
}
292303
}
293304

294305
/**

dspace-api/src/main/java/org/dspace/app/customurl/service/CustomUrlServiceImpl.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,8 @@ public List<String> getAllCustomUrls(Item item) {
7777
@Override
7878
public void replaceCustomUrl(Context context, Item item, String newUrl) {
7979
try {
80-
// Ensure the item is fully managed by the current Hibernate session.
81-
// The item received from event consumers may reference a detached entity
82-
// (e.g., after a context.uncacheEntity/reloadEntity cycle in the caller),
83-
// which would cause a "detached entity passed to persist" error
84-
// when creating new MetadataValue objects. Evicting and re-fetching
85-
// guarantees a clean, fully-managed entity.
86-
context.uncacheEntity(item);
87-
Item managedItem = itemService.find(context, item.getID());
88-
if (managedItem == null) {
89-
return;
90-
}
91-
itemService.clearMetadata(context, managedItem, "dspace", "customurl", null, ANY);
92-
itemService.addMetadata(context, managedItem, "dspace", "customurl", null, null, newUrl);
80+
itemService.clearMetadata(context, item, "dspace", "customurl", null, ANY);
81+
itemService.addMetadata(context, item, "dspace", "customurl", null, null, newUrl);
9382
} catch (SQLException e) {
9483
throw new SQLRuntimeException(e);
9584
}

0 commit comments

Comments
 (0)