org.hibernate.Query.executeUpdate()方法的使用及代码示例

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

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

Query.executeUpdate介绍

[英]Execute the update or delete statement.

The semantics are compliant with the ejb3 Query.executeUpdate() method.
[中]执行update或delete语句。
语义符合ejb3查询。executeUpdate()方法。

代码示例

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

@Override
  protected void doInTransactionWithoutResult(TransactionStatus status) {
    sessionFactory.getCurrentSession().createQuery("DELETE FROM " + Plugin.class.getSimpleName()).executeUpdate();
  }
});

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

@Override
  protected void doInTransactionWithoutResult(TransactionStatus status) {
    sessionFactory.getCurrentSession().createQuery("DELETE FROM VersionInfo").executeUpdate();
  }
});

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

@Deprecated
@Override
@SuppressWarnings({"rawtypes", "deprecation"})
public int bulkUpdate(final String queryString, @Nullable final Object... values) throws DataAccessException {
  Integer result = executeWithNativeSession(session -> {
    org.hibernate.Query queryObject = queryObject(
        ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
    prepareQuery(queryObject);
    if (values != null) {
      for (int i = 0; i < values.length; i++) {
        queryObject.setParameter(i, values[i]);
      }
    }
    return queryObject.executeUpdate();
  });
  Assert.state(result != null, "No update count");
  return result;
}

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

public void deleteAll() {
    getHibernateTemplate().execute((HibernateCallback) session -> session.createQuery(String.format("DELETE FROM %s", ServerBackup.class.getName())).executeUpdate());
  }
}

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

@Override
  protected void doInTransactionWithoutResult(TransactionStatus status) {
    transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
      @Override
      public void afterCommit() {
        clearEnabledUserCountFromCache();
      }
    });
    sessionFactory.getCurrentSession().createQuery("DELETE FROM User").executeUpdate();
  }
});

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

@Override
  protected void doInTransactionWithoutResult(TransactionStatus status) {
    transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
      @Override
      public void afterCommit() {
        clearEnabledUserCountFromCache();
      }
    });
    String queryString = String.format("update %s set enabled = :enabled where name in (:userNames)", User.class.getName());
    Query query = sessionFactory.getCurrentSession().createQuery(queryString);
    query.setParameter("enabled", enabled);
    query.setParameterList("userNames", usernames);
    query.executeUpdate();
  }
});

代码示例来源:origin: kaaproject/kaa

@Override
public void removeByName(String tenantName) {
 Query query = getQuery(DELETE_BY_NAME_HQL);
 int number = query.setString(NAME_PROPERTY, tenantName).executeUpdate();
 LOG.debug("Removed [{}] tenant by name [{}]", number, tenantName);
}

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

@Override
public boolean deleteUsers(List<String> userNames) {
  return (Boolean) transactionTemplate.execute((TransactionCallback) status -> {
    String queryString = "delete from User where name in (:userNames)";
    Query query = sessionFactory.getCurrentSession().createQuery(queryString);
    query.setParameterList("userNames", userNames);
    query.executeUpdate();
    return Boolean.TRUE;
  });
}

代码示例来源:origin: org.springframework/spring-orm

@Deprecated
@Override
@SuppressWarnings({"rawtypes", "deprecation"})
public int bulkUpdate(final String queryString, @Nullable final Object... values) throws DataAccessException {
  Integer result = executeWithNativeSession(session -> {
    org.hibernate.Query queryObject = queryObject(
        ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
    prepareQuery(queryObject);
    if (values != null) {
      for (int i = 0; i < values.length; i++) {
        queryObject.setParameter(i, values[i]);
      }
    }
    return queryObject.executeUpdate();
  });
  Assert.state(result != null, "No update count");
  return result;
}

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

private void createFunctionIndex() {
  Session session = openSession();
  Transaction transaction = session.beginTransaction();
  Query query = session.createSQLQuery( "CREATE UNIQUE INDEX uk_MyEntity_name_lowercase ON MyEntity (lower(name));" );
  query.executeUpdate();
  transaction.commit();
  session.close();
}

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

private void dropTable() {
  Session session = openSession();
  Transaction transaction = session.beginTransaction();
  Query query = session.createSQLQuery( "DROP TABLE IF EXISTS MyEntity;" );
  query.executeUpdate();
  transaction.commit();
  session.close();
}

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

private void createTable() {
  Session session = openSession();
  Transaction transaction = session.beginTransaction();
  Query query = session.createSQLQuery( "CREATE TABLE MyEntity(id bigint, name varchar(255));" );
  query.executeUpdate();
  transaction.commit();
  session.close();
}

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

private void dropFunctionIndex() {
  Session session = openSession();
  Transaction transaction = session.beginTransaction();
  Query query = session.createSQLQuery( "DROP INDEX IF EXISTS uk_MyEntity_name_lowercase;" );
  query.executeUpdate();
  transaction.commit();
  session.close();
}

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

@Test
public void testSimpleInsertWithNamedParam() {
  TestData data = new TestData();
  data.prepare();
  Session s = openSession();
  Transaction t = s.beginTransaction();
  org.hibernate.Query q = s.createQuery( "insert into Pickup (id, owner, vin) select id, :owner, vin from Car" );
  q.setParameter("owner", "owner");
  q.executeUpdate();
  t.commit();
  t = s.beginTransaction();
  s.createQuery( "delete Vehicle" ).executeUpdate();
  t.commit();
  s.close();
  data.cleanup();
}

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

@Test
public void testInsertWithMultipleNamedParams() {
  TestData data = new TestData();
  data.prepare();
  Session s = openSession();
  Transaction t = s.beginTransaction();
  org.hibernate.Query q = s.createQuery( "insert into Pickup (id, owner, vin) select :id, owner, :vin from Car" );
  q.setParameter("id", 5l);
  q.setParameter("vin", "some");
  q.executeUpdate();
  t.commit();
  t = s.beginTransaction();
  s.createQuery( "delete Vehicle" ).executeUpdate();
  t.commit();
  s.close();
  data.cleanup();
}

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

private void cleanUpTest(String pckg) {
  Session session = null;
  Transaction tx = null;
  try {
    session = openSession();
    tx = session.beginTransaction();
    String hql = String.format( "delete from %s", entityName(pckg) );
    Query q = session.createQuery( hql );
    q.executeUpdate();
    tx.commit();
  }
  catch (Exception e) {
    if ( tx != null ) {
      tx.rollback();
    }
  }
  finally {
    if ( session != null ) {
      session.close();
    }
  }
}

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

@Test
public void testInsertSingleValue() {
  final String name = "Name";
  final String lastName = "LastName";
  final String fullName = name + " " + lastName;
  final FromEntity fromEntity = createFrom( name, lastName );
  final int id = 10000;// id fake
  Session session = openSession();
  session.getTransaction().begin();
  Query insert = session.getNamedQuery( "DestinationEntity.insert" );
  insert.setParameter( "generatedId", id );
  insert.setParameter( "fromId", fromEntity.id );
  insert.setParameter( "fullName", fullName );
  int executeUpdate = insert.executeUpdate();
  assertEquals( 1, executeUpdate );
  session.getTransaction().commit();
  session.close();
  session = openSession();
  DestinationEntity get = (DestinationEntity) session.get( DestinationEntity.class, id );
  session.close();
  assertEquals( fromEntity, get.from );
  assertEquals( fullName, get.fullNameFrom );
}

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

@Test
public void testDeleteMultipleValues() {
  final String name = "Name";
  final String lastName = "LastName";
  final List<Integer> ids = new ArrayList<Integer>();
  final int quantity = 10;
  final List<DestinationEntity> destinations = new ArrayList<DestinationEntity>();
  for ( int i = 0; i < quantity; i++ ) {
    FromEntity fe = createFrom( name + i, lastName + i );
    DestinationEntity destination = createDestination( fe, fe.name + fe.lastName );
    destinations.add( destination );
    ids.add( destination.id );
  }
  Session session = openSession();
  session.getTransaction().begin();
  Query delete = session.getNamedQuery( "DestinationEntity.delete" );
  delete.setParameterList( "ids", ids );
  int executeUpdate = delete.executeUpdate();
  assertEquals( quantity, executeUpdate );
  session.getTransaction().commit();
  session.close();
  List<DestinationEntity> list = findDestinationByIds( ids );
  assertTrue( list.isEmpty() );
}

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

@Test
public void testUpdateSingleValue() {
  final String name = "Name";
  final String lastName = "LastName";
  final String fullName = name + " " + lastName;
  final FromEntity fromEntity = createFrom( name, lastName );
  final DestinationEntity destinationEntity = createDestination( fromEntity, fullName );
  final String inverseFullName = lastName + " " + name;
  final FromEntity anotherFrom = createFrom( lastName, name );
  Session session = openSession();
  session.getTransaction().begin();
  Query update = session.getNamedQuery( "DestinationEntity.update" );
  update.setParameter( "idFrom", anotherFrom.id );
  update.setParameter( "fullName", inverseFullName );
  update.setParameterList( "ids", Collections.singletonList( destinationEntity.id ) );
  int executeUpdate = update.executeUpdate();
  assertEquals( 1, executeUpdate );
  session.getTransaction().commit();
  session.close();
  session = openSession();
  session.beginTransaction();
  DestinationEntity get = (DestinationEntity) session.get( DestinationEntity.class, destinationEntity.id );
  assertEquals( anotherFrom, get.from );
  assertEquals( inverseFullName, get.fullNameFrom );
  session.getTransaction().commit();
  session.close();
}

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

@Test
public void testDeleteSingleValue() {
  final String name = "Name";
  final String lastName = "LastName";
  final String fullName = name + " " + lastName;
  final FromEntity fromEntity = createFrom( name, lastName );
  final DestinationEntity destinationEntity = createDestination( fromEntity, fullName );
  Session session = openSession();
  session.getTransaction().begin();
  Query delete = session.getNamedQuery( "DestinationEntity.delete" );
  delete.setParameterList( "ids", Collections.singletonList( destinationEntity.id ) );
  int executeUpdate = delete.executeUpdate();
  assertEquals( 1, executeUpdate );
  session.getTransaction().commit();
  session.close();
  session = openSession();
  DestinationEntity get = (DestinationEntity) session.get( DestinationEntity.class, destinationEntity.id );
  session.close();
  assertNull( get );
}

相关文章