Search in sources :

Example 6 with EntityManagerManager

use of org.apache.tapestry5.jpa.EntityManagerManager in project tapestry-5 by apache.

the class EntityManagerObjectProvider method getOrCreateProxy.

@SuppressWarnings({ "unchecked", "rawtypes" })
private EntityManager getOrCreateProxy(final AnnotationProvider annotationProvider, final ObjectLocator objectLocator) {
    final PersistenceContext annotation = annotationProvider.getAnnotation(PersistenceContext.class);
    final String unitName = annotation == null ? null : annotation.unitName();
    EntityManager proxy = emProxyByName.get(unitName);
    if (proxy == null)
        synchronized (this) {
            final PlasticProxyFactory proxyFactory = objectLocator.getService("PlasticProxyFactory", PlasticProxyFactory.class);
            proxy = proxyFactory.createProxy(EntityManager.class, new ObjectCreator() {

                @Override
                public Object createObject() {
                    final EntityManagerManager entityManagerManager = objectLocator.getService(EntityManagerManager.class);
                    return JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
                }
            }, "<EntityManagerProxy>");
            emProxyByName.put(unitName, proxy);
        }
    return proxy;
}
Also used : EntityManager(javax.persistence.EntityManager) EntityManagerManager(org.apache.tapestry5.jpa.EntityManagerManager) PersistenceContext(javax.persistence.PersistenceContext) PlasticProxyFactory(org.apache.tapestry5.commons.services.PlasticProxyFactory) ObjectCreator(org.apache.tapestry5.commons.ObjectCreator)

Example 7 with EntityManagerManager

use of org.apache.tapestry5.jpa.EntityManagerManager in project tapestry-5 by apache.

the class PersistenceContextWorker method transform.

@Override
public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
    for (final PlasticField field : plasticClass.getFieldsWithAnnotation(PersistenceContext.class)) {
        final PersistenceContext annotation = field.getAnnotation(PersistenceContext.class);
        field.claim(annotation);
        field.setConduit(new ReadOnlyComponentFieldConduit(plasticClass.getClassName(), field.getName()) {

            @Override
            public Object get(Object instance, InstanceContext context) {
                return JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
            }
        });
    }
}
Also used : ReadOnlyComponentFieldConduit(org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit) InstanceContext(org.apache.tapestry5.plastic.InstanceContext) PlasticField(org.apache.tapestry5.plastic.PlasticField) PersistenceContext(javax.persistence.PersistenceContext)

Example 8 with EntityManagerManager

use of org.apache.tapestry5.jpa.EntityManagerManager in project tapestry-5 by apache.

the class JpaTest method trySomething.

@Test
public void trySomething() {
    ThingOne thingOne = new ThingOne();
    thingOne.setId(1);
    PersistedEntity entity = JpaInternalUtils.convertApplicationValueToPersisted(entityManagerManager, thingOne);
}
Also used : PersistedEntity(org.apache.tapestry5.internal.jpa.PersistedEntity) ThingOne(org.apache.tapestry5.jpa.test.entities.ThingOne) Test(org.testng.annotations.Test)

Example 9 with EntityManagerManager

use of org.apache.tapestry5.jpa.EntityManagerManager in project tapestry-5 by apache.

the class JpaModule method buildEntityManagerManager.

@Scope(ScopeConstants.PERTHREAD)
public static EntityManagerManager buildEntityManagerManager(final EntityManagerSource entityManagerSource, final PerthreadManager perthreadManager, final Logger logger) {
    final EntityManagerManagerImpl service = new EntityManagerManagerImpl(entityManagerSource, logger);
    perthreadManager.addThreadCleanupListener(service);
    return service;
}
Also used : EntityManagerManagerImpl(org.apache.tapestry5.internal.jpa.EntityManagerManagerImpl) Scope(org.apache.tapestry5.ioc.annotations.Scope)

Example 10 with EntityManagerManager

use of org.apache.tapestry5.jpa.EntityManagerManager in project tapestry-5 by apache.

the class JpaModule method provideValueEncoders.

@Contribute(ValueEncoderSource.class)
public static void provideValueEncoders(final MappedConfiguration<Class, ValueEncoderFactory> configuration, @Symbol(JpaSymbols.PROVIDE_ENTITY_VALUE_ENCODERS) final boolean provideEncoders, final EntityManagerSource entityManagerSource, final EntityManagerManager entityManagerManager, final TypeCoercer typeCoercer, final PropertyAccess propertyAccess, final LoggerSource loggerSource) {
    if (!provideEncoders)
        return;
    for (final PersistenceUnitInfo info : entityManagerSource.getPersistenceUnitInfos()) {
        final EntityManagerFactory emf = entityManagerSource.getEntityManagerFactory(info.getPersistenceUnitName());
        final Metamodel metamodel = emf.getMetamodel();
        for (final EntityType<?> entity : metamodel.getEntities()) {
            final Class<?> javaType = entity.getJavaType();
            final ValueEncoderFactory factory = new ValueEncoderFactory() {

                @Override
                public ValueEncoder create(final Class type) {
                    return new JpaValueEncoder(entity, entityManagerManager, info.getPersistenceUnitName(), propertyAccess, typeCoercer, loggerSource.getLogger(javaType));
                }
            };
            configuration.add(javaType, factory);
        }
    }
}
Also used : JpaValueEncoder(org.apache.tapestry5.internal.jpa.JpaValueEncoder) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo) Metamodel(javax.persistence.metamodel.Metamodel) ValueEncoderFactory(org.apache.tapestry5.services.ValueEncoderFactory) Contribute(org.apache.tapestry5.ioc.annotations.Contribute)

Aggregations

EntityManagerManager (org.apache.tapestry5.jpa.EntityManagerManager)14 Test (org.testng.annotations.Test)13 EntityManager (javax.persistence.EntityManager)12 EntityTransactionManager (org.apache.tapestry5.jpa.EntityTransactionManager)12 JpaTransactionAdvisor (org.apache.tapestry5.jpa.JpaTransactionAdvisor)12 EntityTransaction (javax.persistence.EntityTransaction)9 SQLException (java.sql.SQLException)3 PersistenceContext (javax.persistence.PersistenceContext)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 Metamodel (javax.persistence.metamodel.Metamodel)1 PersistenceUnitInfo (javax.persistence.spi.PersistenceUnitInfo)1 ObjectCreator (org.apache.tapestry5.commons.ObjectCreator)1 PlasticProxyFactory (org.apache.tapestry5.commons.services.PlasticProxyFactory)1 ApplicationGlobals (org.apache.tapestry5.http.services.ApplicationGlobals)1 EntityManagerManagerImpl (org.apache.tapestry5.internal.jpa.EntityManagerManagerImpl)1 JpaValueEncoder (org.apache.tapestry5.internal.jpa.JpaValueEncoder)1 PersistedEntity (org.apache.tapestry5.internal.jpa.PersistedEntity)1 PageTesterContext (org.apache.tapestry5.internal.test.PageTesterContext)1 ReadOnlyComponentFieldConduit (org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit)1 RegistryBuilder (org.apache.tapestry5.ioc.RegistryBuilder)1