You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by "Charles Capon (JIRA)" <ji...@apache.org> on 2014/09/18 15:31:33 UTC

[jira] [Created] (TAP5-2389) [tapestry-jpa] Better EntityPersistentFieldStrategy that can persist transient entity

Charles Capon created TAP5-2389:
-----------------------------------

             Summary: [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: Bug
          Components: tapestry-jpa
    Affects Versions: 5.4
            Reporter: Charles Capon


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)