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