[ 
https://issues.apache.org/jira/browse/TAP5-2389?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Jochen Kemnade updated TAP5-2389:
---------------------------------
    Issue Type: Improvement  (was: Bug)

> [tapestry-jpa] Better EntityPersistentFieldStrategy that can persist 
> transient entity
> -------------------------------------------------------------------------------------
>
>                 Key: TAP5-2389
>                 URL: https://issues.apache.org/jira/browse/TAP5-2389
>             Project: Tapestry 5
>          Issue Type: Improvement
>          Components: tapestry-jpa
>    Affects Versions: 5.4
>            Reporter: Charles Capon
>              Labels: jpa
>
> 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
> {code:title=EntityPersistentFieldStrategy.java|borderStyle=solid}
> 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);
>         }
>     }
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to