javax.persistence.Query.executeUpdate()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(464)

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

Query.executeUpdate介绍

[英]Execute an update or delete statement.
[中]执行update或delete语句。

代码示例

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

  1. protected void deleteAllPeopleUsingEntityManager(EntityManager em) {
  2. em.createQuery("delete from Person p").executeUpdate();
  3. }

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

  1. @Test
  2. public void deferredQueryWithUpdate() {
  3. EntityManagerFactory emf = mock(EntityManagerFactory.class);
  4. EntityManager targetEm = mock(EntityManager.class);
  5. Query query = mock(Query.class);
  6. given(emf.createEntityManager()).willReturn(targetEm);
  7. given(targetEm.createQuery("x")).willReturn(query);
  8. given(targetEm.isOpen()).willReturn(true);
  9. EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
  10. em.createQuery("x").executeUpdate();
  11. verify(query).executeUpdate();
  12. verify(targetEm).close();
  13. }

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

  1. @After
  2. public void after() throws Exception {
  3. EntityManagerFactory emf = (EntityManagerFactory) context.get(EnvironmentName.ENTITY_MANAGER_FACTORY);
  4. UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
  5. ut.begin();
  6. try {
  7. EntityManager em = emf.createEntityManager();
  8. em.createQuery("delete from CorrelationPropertyInfo").executeUpdate();
  9. em.createQuery("delete from CorrelationKeyInfo").executeUpdate();
  10. ut.commit();
  11. } catch (Exception ex) {
  12. ut.rollback();
  13. Assert.fail("Exception thrown while trying to cleanup correlation data.");
  14. }
  15. cleanUp(context);
  16. }

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

  1. /**
  2. * @return
  3. */
  4. private void updateNamed(boolean useSameEm)
  5. {
  6. EntityManager em = emf.createEntityManager();
  7. String updateQuery = "Update StudentLongPrimitive s SET s.name=Vivek where s.name=Amresh";
  8. Query q = em.createQuery(updateQuery);
  9. q.executeUpdate();
  10. if (!useSameEm)
  11. {
  12. em.close();
  13. em = emf.createEntityManager();
  14. }
  15. StudentLongPrimitive newStudent = em.find(StudentLongPrimitive.class, getRandomValue(long.class));
  16. Assert.assertNotNull(newStudent);
  17. Assert.assertEquals(getRandomValue(short.class), newStudent.getAge());
  18. Assert.assertEquals("Vivek", newStudent.getName());
  19. em.close();
  20. }

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

  1. /**
  2. * Test delete all.
  3. */
  4. private void testDeleteAll()
  5. {
  6. persistBooks();
  7. int result = em.createQuery("delete from Book b").executeUpdate();
  8. Assert.assertEquals(4, result);
  9. assertDeleted(T, T, T, T);
  10. }

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

  1. entityManager.createQuery( "delete from Book" ).executeUpdate();
  2. entityManager.createQuery( "delete from Person" ).executeUpdate();
  3. Person person = entityManager.find( Person.class, personId );
  4. entityManager.createQuery( "delete from Book" ).executeUpdate();
  5. entityManager.createQuery( "delete from Person" ).executeUpdate();
  6. Person person = entityManager.find( Person.class, personId );
  7. person.setName("John Doe");
  8. entityManager.flush();
  9. Person person = entityManager.find( Person.class, personId );
  10. entityManager.createQuery( "update Person set name = UPPER(name)" ).executeUpdate();

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

  1. /**
  2. * Tear down.
  3. */
  4. @After
  5. public void tearDown()
  6. {
  7. em.createQuery("DELETE FROM Person p").executeUpdate();
  8. em.close();
  9. emf.close();
  10. }

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

  1. @Test
  2. public void testOrder() {
  3. doInJPA( this::entityManagerFactory, entityManager -> {
  4. entityManager.createNativeQuery("delete from Person").executeUpdate();
  5. });
  6. doInJPA( this::entityManagerFactory, entityManager -> {
  7. Person person = new Person("John Doe");
  8. person.id = 1L;
  9. entityManager.persist(person);
  10. });
  11. doInJPA( this::entityManagerFactory, entityManager -> {
  12. log.info("testFlushSQL");
  13. //tag::flushing-order-example[]
  14. Person person = entityManager.find( Person.class, 1L);
  15. entityManager.remove(person);
  16. Person newPerson = new Person( );
  17. newPerson.setId( 2L );
  18. newPerson.setName( "John Doe" );
  19. entityManager.persist( newPerson );
  20. //end::flushing-order-example[]
  21. });
  22. }

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

  1. EntityManager em = getEmf().createEntityManager();
  2. Query query = em.createNativeQuery(
  3. "select SEQUENCE_NAME from INFORMATION_SCHEMA.COLUMNS "
  4. String seqName = (String) query.getSingleResult();
  5. query = em.createNativeQuery("alter sequence " + seqName + " increment by 1000");
  6. query.executeUpdate();

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

  1. /**
  2. * @return
  3. */
  4. private void updateNamed(boolean useSameEm)
  5. {
  6. EntityManager em = emf.createEntityManager();
  7. String updateQuery = "Update StudentLongWrapper s SET s.name='Vivek' where s.name='Amresh'";
  8. Query q = em.createQuery(updateQuery);
  9. q.executeUpdate();
  10. if (!useSameEm)
  11. {
  12. em.close();
  13. em = emf.createEntityManager();
  14. }
  15. StudentLongWrapper newStudent = em.find(StudentLongWrapper.class, getRandomValue(Long.class));
  16. Assert.assertNotNull(newStudent);
  17. Assert.assertEquals(getRandomValue(short.class), newStudent.getAge());
  18. Assert.assertEquals("Vivek", newStudent.getName());
  19. em.close();
  20. }

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

  1. final EntityManagerFactory emf = persistenceProvider.createEntityManagerFactory( "hibernate-osgi-test", null );
  2. EntityManager em = emf.createEntityManager();
  3. em.getTransaction().begin();
  4. em.persist( new DataPoint( "Brett" ) );
  5. em.getTransaction().commit();
  6. em.close();
  7. em = emf.createEntityManager();
  8. em.getTransaction().begin();
  9. dp = em.find( DataPoint.class, 1 );
  10. dp.setName( "Brett2" );
  11. em.getTransaction().commit();
  12. em.close();
  13. em = emf.createEntityManager();
  14. em.getTransaction().begin();
  15. em.createQuery( "delete from DataPoint" ).executeUpdate();
  16. em.getTransaction().commit();
  17. em.close();
  18. em = emf.createEntityManager();
  19. em.getTransaction().begin();
  20. dp = em.find( DataPoint.class, 1 );
  21. assertNull( dp );
  22. em.getTransaction().commit();

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

  1. @Transactional
  2. @Override
  3. public void deleteAllInBatch() {
  4. em.createQuery(getDeleteAllQueryString()).executeUpdate();
  5. }

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

  1. /**
  2. * Test delete simple.
  3. */
  4. private void testDeleteSimple()
  5. {
  6. persistData();
  7. int result = em.createQuery("delete from PersonEmbed p").executeUpdate();
  8. Assert.assertEquals(4, result);
  9. assertDeleted(T, T, T, T);
  10. }

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

  1. /**
  2. * @return
  3. */
  4. private void updateNamed(boolean useSameEm)
  5. {
  6. EntityManager em = emf.createEntityManager();
  7. String updateQuery = "Update StudentDoublePrimitive s SET s.name='Vivek' where s.name='Amresh'";
  8. Query q = em.createQuery(updateQuery);
  9. q.executeUpdate();
  10. if (!useSameEm)
  11. {
  12. em.close();
  13. em = emf.createEntityManager();
  14. }
  15. StudentDoublePrimitive newStudent = em.find(StudentDoublePrimitive.class, getRandomValue(Double.class));
  16. Assert.assertNotNull(newStudent);
  17. Assert.assertEquals(getRandomValue(short.class), newStudent.getAge());
  18. Assert.assertEquals("Vivek", newStudent.getName());
  19. em.close();
  20. }

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

  1. EntityManager em = emf.createEntityManager();
  2. int varsUpdated = varLogQuery.executeUpdate();
  3. report.addEntry(Type.INFO, "Variable instances updated = " + varsUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());
  4. int nodesUpdated = nodeLogQuery.executeUpdate();
  5. report.addEntry(Type.INFO, "Node instances updated = " + nodesUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());
  6. int pInstancesUpdated = pInstanceLogQuery.executeUpdate();
  7. report.addEntry(Type.INFO, "Process instances updated = " + pInstancesUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());
  8. int taskVarUpdated = taskVarLogQuery.executeUpdate();
  9. report.addEntry(Type.INFO, "Task variables updated = " + taskVarUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());
  10. } catch (Throwable e) {
  11. int auditTaskUpdated = auditTaskLogQuery.executeUpdate();
  12. report.addEntry(Type.INFO, "Task audit updated = " + auditTaskUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());
  13. int taskUpdated = taskLogQuery.executeUpdate();
  14. report.addEntry(Type.INFO, "Tasks updated = " + taskUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());
  15. int contextInfoUpdated = contextInfoQuery.executeUpdate();
  16. report.addEntry(Type.INFO, "Context info updated = " + contextInfoUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());
  17. } catch (Throwable e) {
  18. int executorRequestsUpdated = executorRequestQuery.executeUpdate();
  19. report.addEntry(Type.INFO, "Executor Jobs updated = " + executorRequestsUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());

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

  1. @Override
  2. public int executeUpdateString(String updateString) {
  3. check();
  4. Query query = this.em.createQuery(updateString);
  5. return query.executeUpdate();
  6. }

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

  1. /**
  2. * @return
  3. */
  4. private void updateNamed(boolean useSameEm)
  5. {
  6. EntityManager em = emf.createEntityManager();
  7. String updateQuery = "Update StudentDate s SET s.name=Vivek where s.name=Amresh";
  8. Query q = em.createQuery(updateQuery);
  9. q.executeUpdate();
  10. if (!useSameEm)
  11. {
  12. em.close();
  13. em = emf.createEntityManager();
  14. }
  15. StudentDate newStudent = em.find(StudentDate.class, getRandomValue(Date.class));
  16. Assert.assertNotNull(newStudent);
  17. Assert.assertEquals(getRandomValue(short.class), newStudent.getAge());
  18. Assert.assertEquals("Vivek", newStudent.getName());
  19. em.close();
  20. }

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

  1. private EntityManager getNewEM()
  2. {
  3. if (em != null && em.isOpen())
  4. {
  5. em.close();
  6. }
  7. // Delete by query.
  8. Query query = em.createQuery("Delete from PersonMTORedis p");
  9. int updateCount = query.executeUpdate();
  10. return em = emf.createEntityManager();
  11. }
  12. }

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

  1. public int executeUpdateString(String updateString) {
  2. check();
  3. Query query = this.em.createQuery(updateString);
  4. return query.executeUpdate();
  5. }

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

  1. /**
  2. * @return
  3. */
  4. private void updateNamed(boolean useSameEm)
  5. {
  6. EntityManager em = emf.createEntityManager();
  7. String updateQuery = "Update StudentInt s SET s.name=Vivek where s.name=Amresh";
  8. Query q = em.createQuery(updateQuery);
  9. q.executeUpdate();
  10. if (!useSameEm)
  11. {
  12. em.close();
  13. em = emf.createEntityManager();
  14. }
  15. StudentInt newStudent = em.find(StudentInt.class, getRandomValue(int.class));
  16. Assert.assertNotNull(newStudent);
  17. Assert.assertEquals(getRandomValue(short.class), newStudent.getAge());
  18. Assert.assertEquals("Vivek", newStudent.getName());
  19. em.close();
  20. }

相关文章