javax.persistence.EntityManager#refresh ( )源码实例Demo

下面列出了javax.persistence.EntityManager#refresh ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: peer-os   文件: UpdateDao.java
public void persist( final UpdateEntity item )
{
    EntityManager em = emf.createEntityManager();
    try
    {
        em.getTransaction().begin();
        em.persist( item );
        em.flush();
        em.refresh( item );
        em.getTransaction().commit();
    }
    catch ( Exception e )
    {
        LOG.error( e.toString(), e );
        if ( em.getTransaction().isActive() )
        {
            em.getTransaction().rollback();
        }
    }
    finally
    {
        em.close();
    }
}
 
源代码2 项目: zstack   文件: RESTApiFacadeImpl.java
private RestAPIVO persist(APIMessage msg) {
    RestAPIVO vo = new RestAPIVO();
    vo.setUuid(msg.getId());
    vo.setApiMessageName(msg.getMessageName());
    vo.setState(RestAPIState.Processing);
    EntityManager mgr = getEntityManager();
    EntityTransaction tran = mgr.getTransaction();
    try {
        tran.begin();
        mgr.persist(vo);
        mgr.flush();
        mgr.refresh(vo);
        tran.commit();
        return vo;
    } catch (Exception e) {
        ExceptionDSL.exceptionSafe(tran::rollback);
        throw new CloudRuntimeException(e);
    } finally {
        ExceptionDSL.exceptionSafe(mgr::close);
    }
}
 
源代码3 项目: che   文件: JpaProfileDao.java
@Override
@Transactional
public ProfileImpl getById(String userId) throws NotFoundException, ServerException {
  requireNonNull(userId, "Required non-null id");
  try {
    final EntityManager manager = managerProvider.get();
    final ProfileImpl profile = manager.find(ProfileImpl.class, userId);
    if (profile == null) {
      throw new NotFoundException(format("Couldn't find profile for user with id '%s'", userId));
    }
    manager.refresh(profile);
    return profile;
  } catch (RuntimeException x) {
    throw new ServerException(x.getLocalizedMessage(), x);
  }
}
 
@Test(expected = OptimisticLockException.class)
public void givenVersionedEntitiesWithLockByRefreshMethod_whenConcurrentUpdate_thenOptimisticLockException() throws IOException {
    EntityManager em = getEntityManagerWithOpenTransaction();
    OptimisticLockingStudent student = em.find(OptimisticLockingStudent.class, 1L);
    em.refresh(student, LockModeType.OPTIMISTIC);

    EntityManager em2 = getEntityManagerWithOpenTransaction();
    OptimisticLockingStudent student2 = em2.find(OptimisticLockingStudent.class, 1L);
    em.refresh(student, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
    student2.setName("RICHARD");
    em2.persist(student2);
    em2.getTransaction()
        .commit();
    em2.close();

    student.setName("JOHN");
    em.persist(student);
    em.getTransaction()
        .commit();
    em.close();
}
 
源代码5 项目: testgrid   文件: EntityManagerHelper.java
/**
 * Returns the refreshed result list.
 *
 * @param entityManager Entity Manager object
 * @param resultList    Result List of query execution
 * @return List of refreshed results
 */
public static <T> List<T> refreshResultList(EntityManager entityManager, List<T> resultList) {
    if (!resultList.isEmpty()) {
        for (T entity : resultList) {
            entityManager.refresh(entity);
        }
    }
    return resultList;
}
 
源代码6 项目: mycore   文件: MCRCategoryDAOImpl.java
@Override
public void deleteCategory(MCRCategoryID id) {
    EntityManager entityManager = MCREntityManagerProvider.getCurrentEntityManager();
    LOGGER.debug("Will get: {}", id);
    MCRCategoryImpl category = getByNaturalID(entityManager, id);
    try {
        entityManager.refresh(category); //for MCR-1863
    } catch (EntityNotFoundException e) {
        //required since hibernate 5.3 if category is deleted within same transaction.
        //junit: testLicenses()
    }
    if (category == null) {
        throw new MCRPersistenceException("Category " + id + " was not found. Delete aborted.");
    }
    LOGGER.debug("Will delete: {}", category.getId());
    MCRCategory parent = category.parent;
    category.detachFromParent();
    entityManager.remove(category);
    if (parent != null) {
        entityManager.flush();
        LOGGER.debug("Left: {} Right: {}", category.getLeft(), category.getRight());
        // always add +1 for the currentNode
        int nodes = 1 + (category.getRight() - category.getLeft()) / 2;
        final int increment = nodes * -2;
        // decrement left and right values by nodes
        updateLeftRightValue(entityManager, category.getRootID(), category.getLeft(), increment);
    }
    updateTimeStamp();
    updateLastModified(category.getRootID());
}
 
源代码7 项目: jdal   文件: JpaUtils.java
/** 
 * Initialize entity attribute
 * @param em
 * @param entity
 * @param a
 * @param depth
 */
@SuppressWarnings("rawtypes")
private static void intialize(EntityManager em, Object entity, Object attached, Attribute a, int depth) {
	Object value = PropertyAccessorFactory.forDirectFieldAccess(attached).getPropertyValue(a.getName());
	if (!em.getEntityManagerFactory().getPersistenceUnitUtil().isLoaded(value)) {
		em.refresh(value);
	}
	
	PropertyAccessorFactory.forDirectFieldAccess(entity).setPropertyValue(a.getName(), value);
	
	initialize(em, value, depth - 1);
}
 
@Test(expected = TransactionRequiredException.class)
public void transactionRequiredExceptionOnRefresh() {
	EntityManagerFactory emf = mock(EntityManagerFactory.class);
	EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
	em.refresh(new Object());
}
 
@Test(expected = TransactionRequiredException.class)
public void transactionRequiredExceptionOnRefresh() {
	EntityManagerFactory emf = mock(EntityManagerFactory.class);
	EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
	em.refresh(new Object());
}
 
@Test(expected = TransactionRequiredException.class)
public void transactionRequiredExceptionOnRefresh() {
	EntityManagerFactory emf = mock(EntityManagerFactory.class);
	EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
	em.refresh(new Object());
}
 
源代码11 项目: testgrid   文件: EntityManagerHelper.java
/**
 * Returns the refreshed result.
 *
 * @param entityManager Entity Manager object
 * @param result        Result of query execution
 * @return Refreshed result
 */
public static <T> T refreshResult(EntityManager entityManager, T result) {
    entityManager.refresh(result);
    return result;
}