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

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

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

EntityManager.find介绍

[英]Find by primary key. Search for an entity of the specified class and primary key. If the entity instance is contained in the persistence context, it is returned from there.
[中]按主键查找。搜索指定类和主键的实体。如果实体实例包含在持久性上下文中,则从那里返回。

代码示例

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

EntityManager em = emf.createEntityManager();
em.close();    // Note: em can be closed before JTA tx committed. 
em = emf.createEntityManager();
Employee emp = em.find(...);  // load the employee
em.close();
emf.close();

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

protected <S extends Score, E extends AbstractTestJpaEntity<S>> void findAndAssert(
    Class<E> jpaEntityClass, Long id, S score) {
  try {
    transactionManager.begin();
    EntityManager em = entityManagerFactory.createEntityManager();
    E jpaEntity = em.find(jpaEntityClass, id);
    assertEquals(score, jpaEntity.getScore());
    transactionManager.commit();
  } catch (NotSupportedException | SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) {
    throw new RuntimeException("Transaction failed.", e);
  }
}

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

final EntityManagerFactory emf = persistenceProvider.createEntityManagerFactory( "hibernate-osgi-test", null );
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist( new DataPoint( "Brett" ) );
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
DataPoint dp = em.find( DataPoint.class, 1 );
assertNotNull( dp );
assertEquals( "Brett", dp.getName() );
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
dp = em.find( DataPoint.class, 1 );
dp.setName( "Brett2" );
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
dp = em.find( DataPoint.class, 1 );
assertNull( dp );
em.getTransaction().commit();

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

/**
 * @return
 */
private void updateNamed(boolean useSameEm)
{
  EntityManager em = emf.createEntityManager();
  String updateQuery = "Update StudentLongPrimitive s SET s.name=Vivek where s.name=Amresh";
  Query q = em.createQuery(updateQuery);
  q.executeUpdate();
  if (!useSameEm)
  {
    em.close();
    em = emf.createEntityManager();
  }
  StudentLongPrimitive newStudent = em.find(StudentLongPrimitive.class, getRandomValue(long.class));
  Assert.assertNotNull(newStudent);
  Assert.assertEquals(getRandomValue(short.class), newStudent.getAge());
  Assert.assertEquals("Vivek", newStudent.getName());
  em.close();
}

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

public void deleteCounter()
{
  EntityManager em = emf.createEntityManager();
  Counters counters = new Counters();
  counters = em.find(Counters.class, id3);
  Assert.assertNotNull(counters);
  Assert.assertNotNull(counters.getCounter());
  em.remove(counters);
  EntityManager em1 = emf.createEntityManager();
  counters = em1.find(Counters.class, id3);
  Assert.assertNull(counters);
  em.close();
}

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

@Test
@Priority(10)
public void initData() {
  EntityManager em = getEntityManager();
  // Revision 1 - add
  em.getTransaction().begin();
  ObjectUserTypeEntity entity = new ObjectUserTypeEntity( "builtInType1", "stringUserType1" );
  em.persist( entity );
  em.getTransaction().commit();
  id = entity.getId();
  // Revision 2 - modify
  em.getTransaction().begin();
  entity = em.find( ObjectUserTypeEntity.class, entity.getId() );
  entity.setUserType( 2 );
  entity = em.merge( entity );
  em.getTransaction().commit();
  // Revision 3 - remove
  em.getTransaction().begin();
  entity = em.find( ObjectUserTypeEntity.class, entity.getId() );
  em.remove( entity );
  em.getTransaction().commit();
  em.close();
}

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

entityManager.createQuery( "delete from Book" ).executeUpdate();
entityManager.createQuery( "delete from Person" ).executeUpdate();
Person person = entityManager.find( Person.class, personId );
entityManager.createQuery( "delete from Book" ).executeUpdate();
entityManager.createQuery( "delete from Person" ).executeUpdate();
Person person = entityManager.find( Person.class, personId );
person.setName("John Doe");
entityManager.flush();
Person person = entityManager.find( Person.class, personId );
entityManager.createQuery( "update Person set name = UPPER(name)" ).executeUpdate();
    Person person = entityManager.find( Person.class, personId );
Person person = entityManager.find( Person.class, personId );
Long personId = _personId;
Person person = entityManager.find( Person.class, personId );

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

/**
 * @return
 */
private void updateNamed(boolean useSameEm)
{
  EntityManager em = emf.createEntityManager();
  String updateQuery = "Update StudentLongWrapper s SET s.name='Vivek' where s.name='Amresh'";
  Query q = em.createQuery(updateQuery);
  q.executeUpdate();
  if (!useSameEm)
  {
    em.close();
    em = emf.createEntityManager();
  }
  StudentLongWrapper newStudent = em.find(StudentLongWrapper.class, getRandomValue(Long.class));
  Assert.assertNotNull(newStudent);
  Assert.assertEquals(getRandomValue(short.class), newStudent.getAge());
  Assert.assertEquals("Vivek", newStudent.getName());
  em.close();
}

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

public void testDelete(boolean useSameEm)
{
  EntityManager em = emf.createEntityManager();
  StudentCharacter studentMax = em.find(StudentCharacter.class, maxValue);
  Assert.assertNotNull(studentMax);
  Assert.assertEquals(getMaxValue(short.class), studentMax.getAge());
  Assert.assertEquals("Kuldeep", studentMax.getName());
  em.remove(studentMax);
  if (!useSameEm)
  {
    em.close();
    em = emf.createEntityManager();
  }
  studentMax = em.find(StudentCharacter.class, maxValue);
  Assert.assertNull(studentMax);
  em.close();
}

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

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

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

private void deleteNamedQueryOnCounter()
  {
    EntityManager em = emf.createEntityManager();
    String deleteQuery = "Delete From Counters c where c.id <= " + id2;

    Query q = em.createQuery(deleteQuery);
    q.executeUpdate();

    Counters counter2 = new Counters();        
    counter2 = em.find(Counters.class, id1);
    Assert.assertNull(counter2);

    Counters counter3 = new Counters();
    counter3 = em.find(Counters.class, id2);
    Assert.assertNull(counter3);

    em.close();
  }
}

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

@Test
public void testJpaEnvers() throws Exception {
  final ServiceReference serviceReference = bundleContext.getServiceReference( PersistenceProvider.class.getName() );
  final PersistenceProvider persistenceProvider = (PersistenceProvider) bundleContext.getService( serviceReference );
  final EntityManagerFactory emf = persistenceProvider.createEntityManagerFactory( "hibernate-osgi-test", null );
  final Integer adpId;
  EntityManager em = emf.createEntityManager();
  em.getTransaction().begin();
  AuditedDataPoint adp = new AuditedDataPoint( "Chris" );
  em.persist( adp );
  em.getTransaction().commit();
  adpId = adp.getId();
  em.close();
  em = emf.createEntityManager();
  em.getTransaction().begin();
  adp = em.find( AuditedDataPoint.class, adpId );
  adp.setName( "Chris2" );
  em.getTransaction().commit();
  em.close();
  em = emf.createEntityManager();
  AuditReader ar = AuditReaderFactory.get( em );
  assertEquals( 2, ar.getRevisions( AuditedDataPoint.class, adpId ).size() );
  AuditedDataPoint rev1 = ar.find( AuditedDataPoint.class, adpId, 1 );
  AuditedDataPoint rev2 = ar.find( AuditedDataPoint.class, adpId, 2 );
  assertEquals( new AuditedDataPoint( adpId, "Chris" ), rev1 );
  assertEquals( new AuditedDataPoint( adpId, "Chris2" ), rev2 );
  em.close();
}

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

protected <S extends Score, E extends AbstractTestJpaEntity<S>> void findAssertAndChangeScore(
    Class<E> jpaEntityClass, Long id, S oldScore, S newScore) {
  try {
    transactionManager.begin();
    EntityManager em = entityManagerFactory.createEntityManager();
    E jpaEntity = em.find(jpaEntityClass, id);
    em.persist(jpaEntity);
    assertEquals(oldScore, jpaEntity.getScore());
    jpaEntity.setScore(newScore);
    jpaEntity = em.merge(jpaEntity);
    transactionManager.commit();
  } catch (NotSupportedException | SystemException | RollbackException | HeuristicRollbackException | HeuristicMixedException e) {
    throw new RuntimeException("Transaction failed.", e);
  }
}

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

/**
 * @return
 */
private void updateNamed(boolean useSameEm)
{
  EntityManager em = emf.createEntityManager();
  String updateQuery = "Update StudentDoublePrimitive s SET s.name='Vivek' where s.name='Amresh'";
  Query q = em.createQuery(updateQuery);
  q.executeUpdate();
  if (!useSameEm)
  {
    em.close();
    em = emf.createEntityManager();
  }
  StudentDoublePrimitive newStudent = em.find(StudentDoublePrimitive.class, getRandomValue(Double.class));
  Assert.assertNotNull(newStudent);
  Assert.assertEquals(getRandomValue(short.class), newStudent.getAge());
  Assert.assertEquals("Vivek", newStudent.getName());
  em.close();
}

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

public void testDelete(boolean useSameEm)
{
  EntityManager em = emf.createEntityManager();
  StudentMongoBigInteger studentMax = em.find(StudentMongoBigInteger.class, getMaxValue(BigInteger.class));
  Assert.assertNotNull(studentMax);
  Assert.assertEquals(getMaxValue(short.class), studentMax.getAge());
  Assert.assertEquals("Kuldeep", studentMax.getName());
  em.remove(studentMax);
  if (!useSameEm)
  {
    em.close();
    em = emf.createEntityManager();
  }
  studentMax = em.find(StudentMongoBigInteger.class, getMaxValue(BigInteger.class));
  Assert.assertNull(studentMax);
  em.close();
}

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

@Test
// tests that Author has 3 books.
public void testAuthorState() {
  EntityManager entityManager = getEntityManager();
  try {
    final Author author = entityManager.find( Author.class, authorId );
    assertNotNull( author );
    assertEquals( 3, author.getBooks().size() );
  }
  catch ( Exception e ) {
    if ( entityManager.getTransaction().isActive() ) {
      entityManager.getTransaction().rollback();
    }
  }
  finally {
    entityManager.close();
  }
}

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

/**
 * @return
 */
private void updateNamed(boolean useSameEm)
{
  EntityManager em = emf.createEntityManager();
  String updateQuery = "Update StudentDate s SET s.name=Vivek where s.name=Amresh";
  Query q = em.createQuery(updateQuery);
  q.executeUpdate();
  if (!useSameEm)
  {
    em.close();
    em = emf.createEntityManager();
  }
  StudentDate newStudent = em.find(StudentDate.class, getRandomValue(Date.class));
  Assert.assertNotNull(newStudent);
  Assert.assertEquals(getRandomValue(short.class), newStudent.getAge());
  Assert.assertEquals("Vivek", newStudent.getName());
  em.close();
}

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

public void testDelete(boolean useSameEm)
{
  EntityManager em = emf.createEntityManager();
  StudentLongPrimitive studentMax = em.find(StudentLongPrimitive.class, getMaxValue(long.class));
  Assert.assertNotNull(studentMax);
  Assert.assertEquals(getMaxValue(short.class), studentMax.getAge());
  Assert.assertEquals("Kuldeep", studentMax.getName());
  em.remove(studentMax);
  if (!useSameEm)
  {
    em.close();
    em = emf.createEntityManager();
  }
  studentMax = em.find(StudentLongPrimitive.class, getMaxValue(long.class));
  Assert.assertNull(studentMax);
  em.close();
}

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

@Test
// tests that Author has 3 books.
public void testAuthorState() {
  EntityManager entityManager = getEntityManager();
  try {
    final Author author = entityManager.find( Author.class, authorId );
    assertNotNull( author );
    assertEquals( 3, author.getBooks().size() );
  }
  catch ( Exception e ) {
    if ( entityManager.getTransaction().isActive() ) {
      entityManager.getTransaction().rollback();
    }
  }
  finally {
    entityManager.close();
  }
}

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

/**
 * @return
 */
private void updateNamed(boolean useSameEm)
{
  EntityManager em = emf.createEntityManager();
  String updateQuery = "Update StudentInt s SET s.name=Vivek where s.name=Amresh";
  Query q = em.createQuery(updateQuery);
  q.executeUpdate();
  if (!useSameEm)
  {
    em.close();
    em = emf.createEntityManager();
  }
  StudentInt newStudent = em.find(StudentInt.class, getRandomValue(int.class));
  Assert.assertNotNull(newStudent);
  Assert.assertEquals(getRandomValue(short.class), newStudent.getAge());
  Assert.assertEquals("Vivek", newStudent.getName());
  em.close();
}

相关文章