Search in sources :

Example 1 with KeyGenerator

use of org.apache.openejb.core.cmp.KeyGenerator in project tomee by apache.

the class Cmp2Util method getPrimaryKey.

public static Object getPrimaryKey(final BeanContext beanContext, final EntityBean entity) {
    if (entity == null) {
        return null;
    }
    // build the primary key
    final KeyGenerator kg = beanContext.getKeyGenerator();
    final Object primaryKey = kg.getPrimaryKey(entity);
    return primaryKey;
}
Also used : EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject) KeyGenerator(org.apache.openejb.core.cmp.KeyGenerator)

Example 2 with KeyGenerator

use of org.apache.openejb.core.cmp.KeyGenerator in project tomee by apache.

the class JpaCmpEngine method createBean.

public Object createBean(EntityBean bean, final ThreadContext callContext) throws CreateException {
    // TODO verify that extract primary key requires a flush followed by a merge
    final TransactionPolicy txPolicy = startTransaction("persist", callContext);
    creating.get().add(bean);
    try {
        final BeanContext beanContext = callContext.getBeanContext();
        final EntityManager entityManager = getEntityManager(beanContext);
        entityManager.persist(bean);
        entityManager.flush();
        bean = entityManager.merge(bean);
        // extract the primary key from the bean
        final KeyGenerator kg = beanContext.getKeyGenerator();
        final Object primaryKey = kg.getPrimaryKey(bean);
        return primaryKey;
    } finally {
        creating.get().remove(bean);
        commitTransaction("persist", callContext, txPolicy);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) EntityManager(javax.persistence.EntityManager) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject) SimpleKeyGenerator(org.apache.openejb.core.cmp.SimpleKeyGenerator) KeyGenerator(org.apache.openejb.core.cmp.KeyGenerator) ComplexKeyGenerator(org.apache.openejb.core.cmp.ComplexKeyGenerator) Cmp2KeyGenerator(org.apache.openejb.core.cmp.cmp2.Cmp2KeyGenerator)

Aggregations

EJBLocalObject (javax.ejb.EJBLocalObject)2 EJBObject (javax.ejb.EJBObject)2 KeyGenerator (org.apache.openejb.core.cmp.KeyGenerator)2 EntityManager (javax.persistence.EntityManager)1 BeanContext (org.apache.openejb.BeanContext)1 ComplexKeyGenerator (org.apache.openejb.core.cmp.ComplexKeyGenerator)1 SimpleKeyGenerator (org.apache.openejb.core.cmp.SimpleKeyGenerator)1 Cmp2KeyGenerator (org.apache.openejb.core.cmp.cmp2.Cmp2KeyGenerator)1 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)1 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)1