Java Code Examples for javax.persistence.criteria.CriteriaDelete#where()

The following examples show how to use javax.persistence.criteria.CriteriaDelete#where() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HibernateCriteriaIntegrationTest.java    From tutorials with MIT License 7 votes vote down vote up
@Test
public void givenTargetItemPrice_whenCriteriaDelete_thenDeleteMatched() {

    int targetPrice = 1000;

    Session session = HibernateUtil.getHibernateSession();
    CriteriaBuilder cb = session.getCriteriaBuilder();
    CriteriaDelete<Item> criteriaDelete = cb.createCriteriaDelete(Item.class);
    Root<Item> root = criteriaDelete.from(Item.class);
    criteriaDelete.where(cb.greaterThan(root.get("itemPrice"), targetPrice));

    Transaction transaction = session.beginTransaction();
    session.createQuery(criteriaDelete).executeUpdate();
    transaction.commit();

    List<Item> deletedItem = session.createQuery("FROM Item WHERE itemPrice > " + targetPrice, Item.class).list();
    assertTrue(deletedItem.isEmpty());

}
 
Example 2
Source File: MechanismEntityService.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/**
 * 根据主文档删除,fdEntityKey可为空,不触发相关事件
 */
public <E extends IEntity> void deleteByEntity(Class<E> clazz,
		String fdEntityName, String fdEntityId, String fdEntityKey) {
	IteratorQueryTemplate<E> template = new IteratorQueryTemplate<>(
			entityManager, clazz).setFetchSize(1).setAutoFlush(false);
	IteratorQueryTemplate<E>.QueryIterator iterator = template
			.iterator(buildRequest(fdEntityName, fdEntityId, fdEntityKey));
	if (iterator.isEmpty()) {
		return;
	}
	CriteriaDelete<E> query = entityManager.getCriteriaBuilder()
			.createCriteriaDelete(clazz);
	query.where(query.from(clazz).get("fdId").in(iterator.getIds()));
	entityManager.createQuery(query).executeUpdate();
}
 
Example 3
Source File: MechanismEntityService.java    From mPass with Apache License 2.0 5 votes vote down vote up
/**
 * 根据主文档删除,fdEntityKey可为空,不触发相关事件
 */
public <E extends IEntity> void deleteByEntity(Class<E> clazz,
		String fdEntityName, String fdEntityId, String fdEntityKey) {
	IteratorQueryTemplate<E> template = new IteratorQueryTemplate<>(
			entityManager, clazz).setFetchSize(1).setAutoFlush(false);
	IteratorQueryTemplate<E>.QueryIterator iterator = template
			.iterator(buildRequest(fdEntityName, fdEntityId, fdEntityKey));
	if (iterator.isEmpty()) {
		return;
	}
	CriteriaDelete<E> query = entityManager.getCriteriaBuilder()
			.createCriteriaDelete(clazz);
	query.where(query.from(clazz).get("fdId").in(iterator.getIds()));
	entityManager.createQuery(query).executeUpdate();
}
 
Example 4
Source File: SCCCachingFactory.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Clear all subscriptions from the database assigned to the
 * credential.
 * @param c the credentials
 */
public static void clearSubscriptions(Credentials c) {
    if (c == null) {
        clearSubscriptions();
        return;
    }
    CriteriaBuilder builder = getSession().getCriteriaBuilder();
    CriteriaDelete<SCCSubscription> delete = builder.createCriteriaDelete(SCCSubscription.class);
    Root<SCCSubscription> e = delete.from(SCCSubscription.class);
    delete.where(builder.equal(e.get("credentials"), c));
    getSession().createQuery(delete).executeUpdate();
}
 
Example 5
Source File: UserNotificationFactory.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Delete all notification messages that were created before a given date.
 *
 * @param before all notifications created before this date will be deleted
 * @return int number of deleted notification messages
 */
public static int deleteNotificationMessagesBefore(Date before) {
    CriteriaBuilder builder = getSession().getCriteriaBuilder();
    CriteriaDelete<NotificationMessage> delete = builder.createCriteriaDelete(NotificationMessage.class);
    Root<NotificationMessage> root = delete.from(NotificationMessage.class);
    delete.where(builder.lessThan(root.<Date>get("created"), before));
    return getSession().createQuery(delete).executeUpdate();
}
 
Example 6
Source File: AbstractQueryImpl.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
protected <E> javax.persistence.Query createDeleteQuery(EntityManager entityManager, QueryCriteria criteria, Class<E> dtoClass) {
    CriteriaDelete<E> deleteQuery = criteriaBuilder.createCriteriaDelete(dtoClass);
    Root<E> root = deleteQuery.from(dtoClass);
    deleteQuery.where(criteria.toQueryPredicates(root)
                              .toArray(new Predicate[0]));
    return entityManager.createQuery(deleteQuery);
}
 
Example 7
Source File: CurrentVariantRollbackService.java    From mojito with Apache License 2.0 5 votes vote down vote up
/**
 * Builds the query to delete {@link com.box.l10n.mojito.entity.TMTextUnitCurrentVariant}s that will be rolled back
 *
 * @param tmId            ID of the TM the {@link TMTextUnitCurrentVariant}s to be rolled back should belong to
 * @param extraParameters Extra parameters to filter what to rollback
 * @return The delete query
 */
protected Query buildDeleteQuery(Long tmId, CurrentVariantRollbackParameters extraParameters) {

    logger.trace("Building the delete tmTextUnitCurrentVariants query");

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaDelete<TMTextUnitCurrentVariant> deleteCriteria = criteriaBuilder.createCriteriaDelete(TMTextUnitCurrentVariant.class);
    Root<TMTextUnitCurrentVariant> root = deleteCriteria.from(TMTextUnitCurrentVariant.class);

    Predicate whereClause = criteriaBuilder.conjunction();

    Predicate tmPredicate = criteriaBuilder.equal(root.get(TMTextUnitCurrentVariant_.tm), tmId);
    whereClause = criteriaBuilder.and(whereClause, tmPredicate);

    List<Long> localeIdsToRollback = extraParameters.getLocaleIds();
    if (localeIdsToRollback != null && !localeIdsToRollback.isEmpty()) {
        Predicate localesPredicate = criteriaBuilder.isTrue(root.get(TMTextUnitCurrentVariant_.locale).in(localeIdsToRollback));
        whereClause = criteriaBuilder.and(whereClause, localesPredicate);
    }

    List<Long> tmTextUnitIdsToRollback = extraParameters.getTmTextUnitIds();
    if (tmTextUnitIdsToRollback != null && !tmTextUnitIdsToRollback.isEmpty()) {
        Predicate tmTextUnitPredicate = criteriaBuilder.isTrue(root.get(TMTextUnitCurrentVariant_.tmTextUnit).in(tmTextUnitIdsToRollback));
        whereClause = criteriaBuilder.and(whereClause, tmTextUnitPredicate);
    }

    deleteCriteria.where(whereClause);

    return entityManager.createQuery(deleteCriteria);
}
 
Example 8
Source File: NativeJpaQueryTranslator.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public Query createDeletionQuery(Class queryClazz, TranslationContext criteria) {
       CriteriaDelete jpaQuery = entityManager.getCriteriaBuilder().createCriteriaDelete(queryClazz);

       if (!criteria.predicates.isEmpty()) {
           jpaQuery = jpaQuery.where(criteria.getCriteriaPredicate());
       }

       return entityManager.createQuery(jpaQuery);
   }
 
Example 9
Source File: PostsBean.java    From ee7-sandbox with Apache License 2.0 5 votes vote down vote up
public void delete() {
    final List<Long> checkedList = getCheckedList();
    for (Long id : checkedList) {
        checked.remove(id);
    }

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<Post> q = cb.createCriteriaDelete(Post.class);
    Root<Post> root = q.from(Post.class);
    q.where(root.get("id").in(checkedList));

    int result = em.createQuery(q).executeUpdate();
    log.info("delete @" + result);
    load();
}
 
Example 10
Source File: PostsBean.java    From ee7-sandbox with Apache License 2.0 5 votes vote down vote up
public void delete() {
    final List<Long> checkedList = getCheckedList();
    for (Long id : checkedList) {
        checked.remove(id);
    }

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<Post> q = cb.createCriteriaDelete(Post.class);
    Root<Post> root = q.from(Post.class);
    q.where(root.get("id").in(checkedList));

    int result = em.createQuery(q).executeUpdate();
    log.info("delete @" + result);
    load();
}
 
Example 11
Source File: PostsBean.java    From ee7-sandbox with Apache License 2.0 5 votes vote down vote up
public void delete() {
    final List<Long> checkedList = getCheckedList();
    for (Long id : checkedList) {
        checked.remove(id);
    }

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<Post> q = cb.createCriteriaDelete(Post.class);
    Root<Post> root = q.from(Post.class);
    q.where(root.get("id").in(checkedList));

    int result = em.createQuery(q).executeUpdate();
    log.info("delete @" + result);
    load();
}
 
Example 12
Source File: PostsBean.java    From ee7-sandbox with Apache License 2.0 5 votes vote down vote up
public void delete() {
    final List<Long> checkedList = getCheckedList();
    for (Long id : checkedList) {
        checked.remove(id);
    }

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<Post> q = cb.createCriteriaDelete(Post.class);
    Root<Post> root = q.from(Post.class);
    q.where(root.get("id").in(checkedList));

    int result = em.createQuery(q).executeUpdate();
    log.info("delete @" + result);
    load();
}
 
Example 13
Source File: PostsBean.java    From ee7-sandbox with Apache License 2.0 5 votes vote down vote up
public void delete() {
    final List<Long> checkedList = getCheckedList();
    for (Long id : checkedList) {
        checked.remove(id);
    }

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<Post> q = cb.createCriteriaDelete(Post.class);
    Root<Post> root = q.from(Post.class);
    q.where(root.get("id").in(checkedList));

    int result = em.createQuery(q).executeUpdate();
    log.info("delete @" + result);
    load();
}
 
Example 14
Source File: Filter.java    From activejpa with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs the delete criteria query
 * 
 * @param builder
 * @param query
 * @param root
 */
<T extends Model> void constructQuery(CriteriaBuilder builder, CriteriaDelete<?> query, Root<T> root) {
	if (!conditions.isEmpty()) {
		List<Predicate> predicates = new ArrayList<Predicate>();
		for (Condition condition : conditions) {
			predicates.add(condition.constructQuery(builder, root));
		}
		query.where(predicates.toArray(new Predicate[0]));
	}
}
 
Example 15
Source File: QueryTest.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void testCriteriaEntityQueryWithParam(TestContext context) {
	Author author1 = new Author("Iain M. Banks");
	Author author2 = new Author("Neal Stephenson");
	Book book1 = new Book("1-85723-235-6", "Feersum Endjinn", author1);
	Book book2 = new Book("0-380-97346-4", "Cryptonomicon", author2);
	Book book3 = new Book("0-553-08853-X", "Snow Crash", author2);
	author1.books.add(book1);
	author2.books.add(book2);
	author2.books.add(book3);

	CriteriaBuilder builder = getSessionFactory().getCriteriaBuilder();
	CriteriaQuery<Book> query = builder.createQuery(Book.class);
	Root<Book> b = query.from(Book.class);
	b.fetch("author");
	ParameterExpression<String> t = builder.parameter(String.class);
	query.where( builder.equal( b.get("title"), t ) );
	query.orderBy( builder.asc( b.get("isbn") ) );

	CriteriaUpdate<Book> update = builder.createCriteriaUpdate(Book.class);
	b = update.from(Book.class);
	update.where( builder.equal( b.get("title"), t ) );
	update.set( b.get("title"), "XXX" );

	CriteriaDelete<Book> delete = builder.createCriteriaDelete(Book.class);
	b = delete.from(Book.class);
	delete.where( builder.equal( b.get("title"), t ) );

	test(context,
			openSession()
					.thenCompose( session -> session.persist(author1, author2) )
					.thenCompose( session -> session.flush() )
					.whenComplete( (session,err) -> session.close() )
					.thenCompose( v -> openSession() )
					.thenCompose( session -> session.createQuery(query)
							.setParameter( t, "Snow Crash")
							.getResultList() )
					.thenAccept( books -> {
						context.assertEquals( 1, books.size() );
						books.forEach( book -> {
							context.assertNotNull( book.id );
							context.assertNotNull( book.title );
							context.assertNotNull( book.isbn );
							context.assertEquals( "Snow Crash", book.title );
						} );
					} )

					.thenCompose( v -> openSession() )
					.thenCompose( session -> session.createQuery(update)
							.setParameter( t, "Snow Crash")
							.executeUpdate() )
					.thenCompose( v -> openSession() )
					.thenCompose( session -> session.createQuery(delete)
							.setParameter( t, "Snow Crash")
							.executeUpdate() )
	);
}
 
Example 16
Source File: QueryTest.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void testCriteriaEntityQueryWithNamedParam(TestContext context) {
	Author author1 = new Author("Iain M. Banks");
	Author author2 = new Author("Neal Stephenson");
	Book book1 = new Book("1-85723-235-6", "Feersum Endjinn", author1);
	Book book2 = new Book("0-380-97346-4", "Cryptonomicon", author2);
	Book book3 = new Book("0-553-08853-X", "Snow Crash", author2);
	author1.books.add(book1);
	author2.books.add(book2);
	author2.books.add(book3);

	CriteriaBuilder builder = getSessionFactory().getCriteriaBuilder();
	CriteriaQuery<Book> query = builder.createQuery(Book.class);
	Root<Book> b = query.from(Book.class);
	b.fetch("author");
	ParameterExpression<String> t = builder.parameter(String.class, "title");
	query.where( builder.equal( b.get("title"), t ) );
	query.orderBy( builder.asc( b.get("isbn") ) );

	CriteriaUpdate<Book> update = builder.createCriteriaUpdate(Book.class);
	b = update.from(Book.class);
	update.where( builder.equal( b.get("title"), t ) );
	update.set( b.get("title"), "XXX" );

	CriteriaDelete<Book> delete = builder.createCriteriaDelete(Book.class);
	b = delete.from(Book.class);
	delete.where( builder.equal( b.get("title"), t ) );

	test(context,
			openSession()
					.thenCompose( session -> session.persist(author1, author2) )
					.thenCompose( session -> session.flush() )
					.whenComplete( (session,err) -> session.close() )
					.thenCompose( v -> openSession() )
					.thenCompose( session -> session.createQuery(query)
							.setParameter("title", "Snow Crash")
							.getResultList() )
					.thenAccept( books -> {
						context.assertEquals( 1, books.size() );
						books.forEach( book -> {
							context.assertNotNull( book.id );
							context.assertNotNull( book.title );
							context.assertNotNull( book.isbn );
							context.assertEquals( "Snow Crash", book.title );
						} );
					} )

					.thenCompose( v -> openSession() )
					.thenCompose( session -> session.createQuery(update)
							.setParameter("title", "Snow Crash")
							.executeUpdate() )
					.thenCompose( v -> openSession() )
					.thenCompose( session -> session.createQuery(delete)
							.setParameter("title", "Snow Crash")
							.executeUpdate() )
	);
}