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

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

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

EntityManager.contains介绍

[英]Check if the instance is a managed entity instance belonging to the current persistence context.
[中]检查实例是否是属于当前持久性上下文的托管实体实例。

代码示例

代码示例来源: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: spring-projects/spring-data-jpa

@Transactional
@Override
public void delete(T entity) {
  Assert.notNull(entity, "The entity must not be null!");
  em.remove(em.contains(entity) ? entity : em.merge(entity));
}

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

public void delete(CodeType codeType) {
  if (!em.contains(codeType)) {
    codeType = (CodeType) em.find(CodeTypeImpl.class, codeType.getId());
  }
  em.remove(codeType);
}

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

public AdminUser saveAdminUser(AdminUser user) {
  if (em.contains(user) || user.getId() != null) {
    return em.merge(user);
  } else {
    em.persist(user);
    return user;
  }
}

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

@Override
public void delete(FulfillmentGroup fulfillmentGroup) {
  if (!em.contains(fulfillmentGroup)) {
    fulfillmentGroup = readFulfillmentGroupById(fulfillmentGroup.getId());
  }
  em.remove(fulfillmentGroup);
}

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

@Override
  public void delete(StaticAssetStorage assetStorage) {
    if (!em.contains(assetStorage)) {
      assetStorage = readStaticAssetStorageById(assetStorage.getId());
    }
    em.remove(assetStorage);
  }
}

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

@Override
public void delete(StructuredContent content) {
  if (! em.contains(content)) {
    content = findStructuredContentById(content.getId());
  }
  em.remove(content);
}

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

public void deleteAdminRole(AdminRole role) {
  if (!em.contains(role)) {
    role = readAdminRoleById(role.getId());
  }
  em.remove(role);
}

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

@Override
public void delete(Sku sku) {
  if (!em.contains(sku)) {
    sku = readSkuById(sku.getId());
  }
  em.remove(sku);
}

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

public <E> E update(final E entity) {
  U.notNull(getIdentifier(entity), "entity identifier");
  return transactional(() -> {
    if (em.contains(entity)) {
      em.persist(entity);
      return entity;
    } else {
      return em.merge(entity);
    }
  });
}

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

@Override
public void delete(CategoryXref categoryXref) {
  if (!em.contains(categoryXref)) {
    categoryXref = readXrefByIds(categoryXref.getCategory().getId(), categoryXref.getSubCategory().getId());
  }
  em.remove(categoryXref);        
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testApplicationManagedEntityManagerWithoutTransaction() throws Exception {
  Object testEntity = new Object();
  EntityManager mockEm = mock(EntityManager.class);
  given(mockEmf.createEntityManager()).willReturn(mockEm);
  LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
  EntityManagerFactory emf = cefb.getObject();
  assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());
  assertNotSame("EMF must be proxied", mockEmf, emf);
  EntityManager em = emf.createEntityManager();
  assertFalse(em.contains(testEntity));
  cefb.destroy();
  verify(mockEmf).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testReuseInNewTransaction() {
  EntityManager em = entityManagerFactory.createEntityManager();
  em.joinTransaction();
  doInstantiateAndSave(em);
  endTransaction();
  assertFalse(em.getTransaction().isActive());
  startNewTransaction();
  // Call any method: should cause automatic tx invocation
  assertFalse(em.contains(new Person()));
  assertFalse(em.getTransaction().isActive());
  em.joinTransaction();
  assertTrue(em.getTransaction().isActive());
  doInstantiateAndSave(em);
  setComplete();
  endTransaction();    // Should rollback
  assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));
  // Now clean up the database
  startNewTransaction();
  em.joinTransaction();
  deleteAllPeopleUsingEntityManager(em);
  assertEquals("People have been killed", 0, countRowsInTable(em, "person"));
  setComplete();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testApplicationManagedEntityManagerWithTransaction() throws Exception {
  Object testEntity = new Object();
  EntityTransaction mockTx = mock(EntityTransaction.class);
  // This one's for the tx (shared)
  EntityManager sharedEm = mock(EntityManager.class);
  given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
  // This is the application-specific one
  EntityManager mockEm = mock(EntityManager.class);
  given(mockEm.getTransaction()).willReturn(mockTx);
  given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
  LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
  JpaTransactionManager jpatm = new JpaTransactionManager();
  jpatm.setEntityManagerFactory(cefb.getObject());
  TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
  EntityManagerFactory emf = cefb.getObject();
  assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());
  assertNotSame("EMF must be proxied", mockEmf, emf);
  EntityManager em = emf.createEntityManager();
  em.joinTransaction();
  assertFalse(em.contains(testEntity));
  jpatm.commit(txStatus);
  cefb.destroy();
  verify(mockTx).begin();
  verify(mockTx).commit();
  verify(mockEm).contains(testEntity);
  verify(mockEmf).close();
}

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

@Test
public void testPersistCascadesToManyToOne() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    ReferencedEntity referencedEntity = new ReferencedEntity();
    referencedEntity.setId( 42L );
    SomeEntity someEntity = new SomeEntity();
    someEntity.setId( 23L );
    someEntity.setReferencedEntity( referencedEntity );
    entityManager.persist( someEntity );
    assertTrue( entityManager.contains( referencedEntity ) );
  } );
}

代码示例来源:origin: spring-projects/spring-framework

EntityManager em = emf.createEntityManager();
em.joinTransaction();
assertFalse(em.contains(testEntity));
verify(mockEm).contains(testEntity);
verify(mockEmf).close();

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testApplicationManagedEntityManagerWithJtaTransaction() throws Exception {
  Object testEntity = new Object();
  // This one's for the tx (shared)
  EntityManager sharedEm = mock(EntityManager.class);
  given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
  // This is the application-specific one
  EntityManager mockEm = mock(EntityManager.class);
  given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
  LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
  MutablePersistenceUnitInfo pui = ((MutablePersistenceUnitInfo) cefb.getPersistenceUnitInfo());
  pui.setTransactionType(PersistenceUnitTransactionType.JTA);
  JpaTransactionManager jpatm = new JpaTransactionManager();
  jpatm.setEntityManagerFactory(cefb.getObject());
  TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
  EntityManagerFactory emf = cefb.getObject();
  assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());
  assertNotSame("EMF must be proxied", mockEmf, emf);
  EntityManager em = emf.createEntityManager();
  em.joinTransaction();
  assertFalse(em.contains(testEntity));
  jpatm.commit(txStatus);
  cefb.destroy();
  verify(mockEm).joinTransaction();
  verify(mockEm).contains(testEntity);
  verify(mockEmf).close();
}

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

@Test
@FailureExpected( jiraKey = "HHH-12251")
public void testMergeCascadesToManyToOne() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    ReferencedEntity referencedEntity = new ReferencedEntity();
    referencedEntity.setId( 42L );
    SomeEntity someEntity = new SomeEntity();
    someEntity.setId( 23L );
    someEntity.setReferencedEntity( referencedEntity );
    entityManager.merge( someEntity );
    assertTrue( entityManager.contains( referencedEntity ) );
  } );
}

代码示例来源:origin: spring-projects/spring-framework

assertFalse(proxy.contains(o));
verify(mockEm).contains(o);
verify(mockEm).close();

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testReuseInNewTransaction() {
  EntityManager em = createContainerManagedEntityManager();
  doInstantiateAndSave(em);
  endTransaction();
  //assertFalse(em.getTransaction().isActive());
  startNewTransaction();
  // Call any method: should cause automatic tx invocation
  assertFalse(em.contains(new Person()));
  //assertTrue(em.getTransaction().isActive());
  doInstantiateAndSave(em);
  setComplete();
  endTransaction();    // Should rollback
  assertEquals("Tx must have committed back", 1, countRowsInTable(em, "person"));
  // Now clean up the database
  deleteFromTables("person");
}

相关文章