Uploaded image for project: 'Tapestry 5'
  1. Tapestry 5
  2. TAP5-2389

[tapestry-jpa] Better EntityPersistentFieldStrategy that can persist transient entity

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 5.4
    • Fix Version/s: None
    • Component/s: tapestry-jpa
    • Labels:

      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);
              }
          }
      }
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              Charlouze Charles Capon
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: