Details
-
Improvement
-
Status: Open
-
Major
-
Resolution: Unresolved
-
5.4
-
None
Description
I'm using tapestry-jpa and I want to persist an entity that can be transient or not in a component.
There is an issue with transient entities because they can not be persisted without an exception to be raised.
Here is a code of a better EntityPersistentFieldStrategy that can persist transient and managed entity
EntityPersistentFieldStrategy.java
public class EntityPersistentFieldStrategy extends AbstractSessionPersistentFieldStrategy { private final static String PREFIX = "entity:"; private final EntityManagerManager entityManagerManager; public EntityPersistentFieldStrategy(final EntityManagerManager entityManagerManager, final Request request) { super(PREFIX, request); this.entityManagerManager = entityManagerManager; } @Override protected Object convertApplicationValueToPersisted(final Object newValue) { try { return JpaInternalUtils.convertApplicationValueToPersisted(entityManagerManager, newValue); } catch (final RuntimeException ex) { return super.convertApplicationValueToPersisted(newValue); } } @Override protected Object convertPersistedToApplicationValue(final Object persistedValue) { if (persistedValue instanceof PersistedEntity) { final PersistedEntity persisted = (PersistedEntity) persistedValue; return persisted.restore(entityManagerManager); } else { return super.convertPersistedToApplicationValue(persistedValue); } } }