javax.persistence.EntityManager.flush()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(189)

本文整理了Java中javax.persistence.EntityManager.flush()方法的一些代码示例,展示了EntityManager.flush()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。EntityManager.flush()方法的具体详情如下:
包路径:javax.persistence.EntityManager
类名称:EntityManager
方法名:flush

EntityManager.flush介绍

[英]Synchronize the persistence context to the underlying database.
[中]将持久性上下文同步到基础数据库。

代码示例

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public void remove(Serializable entity) {
  standardEntityManager.remove(entity);
  standardEntityManager.flush();
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public SandBox persist(SandBox entity) {
  sandBoxEntityManager.persist(entity);
  sandBoxEntityManager.flush();
  return entity;
}

代码示例来源:origin: kiegroup/jbpm

@SuppressWarnings("unchecked")
@Override
public <T> T persist(T object) {
  check();
  this.em.persist( object );
  if( this.pessimisticLocking ) {
    this.em.flush();
    Object primaryKey = getFieldValueWithAnnotation(object, Id.class);
    return (T) this.em.find( object.getClass(), primaryKey, LockModeType.PESSIMISTIC_WRITE );
  }
  return object;
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
@Priority(10)
public void initData() {
  // Revision 1
  EntityManager em = getEntityManager();
  em.getTransaction().begin();
  StrTestEntity fe = new StrTestEntity( "x" );
  em.persist( fe );
  em.flush();
  em.remove( em.find( StrTestEntity.class, fe.getId() ) );
  em.flush();
  em.getTransaction().commit();
  //
  id = fe.getId();
}

代码示例来源:origin: Impetus/Kundera

Map<String, Client> clients = (Map<String, Client>) em.getDelegate();
Client client = clients.get(PERSISTENCE_UNIT);
PrimeUser result = em.find(PrimeUser.class, key);
Assert.assertNotNull(result);
em.remove(result);
em.flush();
em.close();
em = emf.createEntityManager();
client = clients.get(PERSISTENCE_UNIT);
((CassandraClientBase) client).setCqlVersion("3.0.0");
result = em.find(PrimeUser.class, key);
Assert.assertNull(result);

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

protected static <T> T doTransactionalOptimisticUpdate(String name, UpdateOperation<T> operation, Class<? extends T> entityClass, Object identifier, PlatformTransactionManager transactionManager, EntityManager entityManager) {
    TransactionStatus transactionStatus = TransactionUtils.createTransaction(
        name,
        TransactionDefinition.PROPAGATION_REQUIRES_NEW,
        transactionManager);
    T entity;
    try {
      entity = entityManager.find(entityClass, identifier);
      if (operation instanceof ValidatedUpdateOperation && !((ValidatedUpdateOperation<T>) operation).isValid(entity)) {
        log.debug("Entity state was found to be invalid while trying to perform " + name + " on " + entityClass.getSimpleName() + " with id: " + identifier);
        throw new OptimisticLockInvalidStateException("Unable to perform " + name + " on " + entityClass.getSimpleName() + " with id: " + identifier + ". Aborting update due to invalid state.");
      }
      operation.update(entity);
      entityManager.flush();

      TransactionUtils.finalizeTransaction(transactionStatus, transactionManager, false);

    } catch (RuntimeException e) {
      TransactionUtils.finalizeTransaction(transactionStatus, transactionManager, true);
      throw e;
    }
    return entity;
  }
}

代码示例来源:origin: kiegroup/jbpm

@Override
public Deadline persistDeadline(Deadline deadline) {
  check();
  this.em.persist( deadline );
  if( this.pessimisticLocking ) {
    this.em.flush();
    return this.em.find(DeadlineImpl.class, deadline.getId(), LockModeType.PESSIMISTIC_WRITE );
  }
  return deadline;
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public <T> T persist(T entity) {
  standardEntityManager.persist(entity);
  standardEntityManager.flush();
  return entity;
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
@Priority(10)
public void initData() {
  // Revision 1
  EntityManager em = getEntityManager();
  em.getTransaction().begin();
  StrTestEntity fe = new StrTestEntity( "x" );
  em.persist( fe );
  em.flush();
  em.getTransaction().commit();
  // Revision 2
  em.getTransaction().begin();
  fe = em.find( StrTestEntity.class, fe.getId() );
  fe.setStr( "y" );
  em.flush();
  em.remove( em.find( StrTestEntity.class, fe.getId() ) );
  em.flush();
  em.getTransaction().commit();
  //
  id = fe.getId();
}

代码示例来源:origin: Impetus/Kundera

Client client = clients.get(PERSISTENCE_UNIT);
((CassandraClientBase) client).setCqlVersion("3.0.0");
PrimeUser result = em.find(PrimeUser.class, key);
Assert.assertNotNull(result);
Assert.assertEquals("After merge", result.getTweetBody()); // assertion
em.remove(result);
em.flush();
em.close();
em = emf.createEntityManager();
clients = (Map<String, Client>) em.getDelegate();
client = clients.get(PERSISTENCE_UNIT);
result = em.find(PrimeUser.class, key);
Assert.assertNull(result);

代码示例来源:origin: AxonFramework/AxonFramework

@Override
protected void doDeleteWithLock(AnnotatedAggregate<T> aggregate) {
  EntityManager entityManager = entityManagerProvider.getEntityManager();
  entityManager.remove(aggregate.getAggregateRoot());
  if (forceFlushOnSave) {
    entityManager.flush();
  }
}

代码示例来源:origin: kiegroup/jbpm

@Override
public Content persistContent(Content content) {
  check();
  this.em.persist( content );
  if( this.pessimisticLocking ) {
    this.em.flush();
    return this.em.find(ContentImpl.class, content.getId(), LockModeType.PESSIMISTIC_WRITE );
  }
  return content;
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
public StaticAssetStorage save(StaticAssetStorage assetStorage) {
  if (em.contains(assetStorage)) {
    return em.merge(assetStorage);
  }
  em.persist(assetStorage);
  em.flush();
  return assetStorage;
}

代码示例来源:origin: hibernate/hibernate-orm

person.setName("John Doe");
entityManager.persist( person );
entityManager.remove( person );
entityManager.persist( person );
Long personId = person.getId();
Person person = entityManager.find( Person.class, personId );
book.setId( 1L );
book.setIsbn( "123-456-7890" );
entityManager.persist( book );
book.setAuthor( session.load( Person.class, personId ) );
Person person = entityManager.find( Person.class, personId );
person.setName("John Doe");
entityManager.flush();
Person person = entityManager.find( Person.class, personId );

代码示例来源:origin: Impetus/Kundera

Client client = clients.get(PERSISTENCE_UNIT);
((CassandraClientBase) client).setCqlVersion("3.0.0");
CassandraPrimeUser result = em.find(CassandraPrimeUser.class, key);
Assert.assertNotNull(result);
Assert.assertNull(result.getKey().getFullName());
em.remove(result);
em.flush();
em.close();
em = emf.createEntityManager();
client = clients.get(PERSISTENCE_UNIT);
((CassandraClientBase) client).setCqlVersion("3.0.0");
result = em.find(CassandraPrimeUser.class, key);
Assert.assertNull(result);

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Override
@Transactional("blTransactionManager")
public void delete(OrderItem orderItem) {
  if (!em.contains(orderItem)) {
    orderItem = readOrderItemById(orderItem.getId());
  }
  if (GiftWrapOrderItem.class.isAssignableFrom(orderItem.getClass())) {
    final GiftWrapOrderItem giftItem = (GiftWrapOrderItem) orderItem;
    for (OrderItem wrappedItem : giftItem.getWrappedItems()) {
      wrappedItem.setGiftWrapOrderItem(null);
      wrappedItem = save(wrappedItem);
    }
  }
  em.remove(orderItem);
  em.flush();
}

代码示例来源:origin: kiegroup/jbpm

@Override
public Attachment persistAttachment(Attachment attachment) {
  check();
  this.em.persist( attachment );
  if( this.pessimisticLocking ) {
    this.em.flush();
    return this.em.find(AttachmentImpl.class, attachment.getId(), LockModeType.PESSIMISTIC_WRITE );
  }
  return attachment;
}

代码示例来源:origin: stackoverflow.com

EntityManager em = ...;
EntityTransaction tx = em.getTransaction();
tx.begin();

for ( int i=0; i<100000; i++ ) {
  Customer customer = new Customer(.....);
  em.persist(customer);
  if ( i % 20 == 0 ) { //20, same as the JDBC batch size
    //flush a batch of inserts and release memory:
    em.flush();
    em.clear();
  }
}

tx.commit();
session.close();

代码示例来源:origin: Impetus/Kundera

@Test
public void compositeKeytest()
{
  emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT, puProperties);
  em = emf.createEntityManager();
  UUID timeLineId = UUID.randomUUID();
  CassandraCompoundKey key = new CassandraCompoundKey("shahid", 1, timeLineId);
  CassandraPrimeUser user = new CassandraPrimeUser(key);
  user.setTweetBody("my first tweet");
  user.setTweetDate(currentDate);
  em.persist(user);
  CassandraPrimeUser user2 = new CassandraPrimeUser(key);
  user2.setTweetBody("my second tweet");
  user2.setTweetDate(currentDate);
  em.flush(); // optional,just to clear persistence cache.
  em.clear();
  CassandraPrimeUser u = em.find(CassandraPrimeUser.class, key);
  Assert.assertEquals("my first tweet", u.getTweetBody());
  em.merge(user2);
  u = em.find(CassandraPrimeUser.class, key);
  Assert.assertEquals("my second tweet", u.getTweetBody());
  em.remove(user2);
}

代码示例来源:origin: Impetus/Kundera

Client client = clients.get(PERSISTENCE_UNIT);
((CassandraClientBase) client).setCqlVersion("3.0.0");
CassandraPrimeUser result = em.find(CassandraPrimeUser.class, key);
Assert.assertNotNull(result);
Assert.assertNull(result.getKey().getFullName());
em.remove(result);
em.flush();
em.close();
em = emf.createEntityManager();
client = clients.get(PERSISTENCE_UNIT);
((CassandraClientBase) client).setCqlVersion("3.0.0");
result = em.find(CassandraPrimeUser.class, key);
Assert.assertNull(result);

相关文章