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

The following examples show how to use javax.persistence.criteria.CriteriaDelete#from() . 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: TestCriteriaDelete.java    From HibernateTips with MIT License 7 votes vote down vote up
@Test
public void deleteBooks() {
	log.info("... deleteBooks ...");

	EntityManager em = emf.createEntityManager();
	em.getTransaction().begin();

	logBooks(em);
	
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaDelete<Book> delete = cb.createCriteriaDelete(Book.class);
	delete.from(Book.class);
	
	Query query = em.createQuery(delete);
	query.executeUpdate();

	logBooks(em);
	
	em.getTransaction().commit();
	em.close();
}
 
Example 2
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 3
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 4
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 5
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 6
Source File: MCRMetadataHistoryCommands.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@MCRCommand(syntax = "clear metadata history of base {0}",
    help = "clears metadata history of all objects with base id {0}")
public static void clearHistory(String baseId) {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaDelete<MCRMetaHistoryItem> delete = cb.createCriteriaDelete(MCRMetaHistoryItem.class);
    Root<MCRMetaHistoryItem> item = delete.from(MCRMetaHistoryItem.class);
    int rowsDeleted = em.createQuery(
        delete.where(
            cb.like(item.get(MCRMetaHistoryItem_.id).as(String.class), baseId + "_".replace("_", "$_") + '%', '$')))
        .executeUpdate();
    LogManager.getLogger().info("Deleted {} items in history of {}", rowsDeleted, baseId);
}
 
Example 7
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 8
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 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: AbstractRepository.java    From batchers with Apache License 2.0 5 votes vote down vote up
public void deleteAll() {
    CriteriaBuilder criteriaBuilder = entityManagerFactory.getCriteriaBuilder();
    CriteriaDelete<E> criteriaDelete = criteriaBuilder.createCriteriaDelete(getEntityClass());

    criteriaDelete.from(getEntityClass());

    entityManager.createQuery(criteriaDelete).executeUpdate();
}
 
Example 11
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 12
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.
 */
public static void clearSubscriptions() {
    //getSession().getNamedQuery("SCCSubscription.deleteAll").executeUpdate();
    CriteriaBuilder builder = getSession().getCriteriaBuilder();
    CriteriaDelete<SCCSubscription> delete = builder.createCriteriaDelete(SCCSubscription.class);
    Root d = delete.from(SCCSubscription.class);
    getSession().createQuery(delete).executeUpdate();

}
 
Example 13
Source File: SCCCachingFactory.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Clear all repositories from the database.
 */
public static void clearRepositories() {
    //getSession().getNamedQuery("SCCRepository.deleteAll").executeUpdate();
    CriteriaBuilder builder = getSession().getCriteriaBuilder();
    CriteriaDelete<SCCRepository> delete = builder.createCriteriaDelete(SCCRepository.class);
    CriteriaDelete<SCCRepositoryAuth> deleteAuth = builder.createCriteriaDelete(SCCRepositoryAuth.class);
    delete.from(SCCRepository.class);
    deleteAuth.from(SCCRepositoryAuth.class);
    getSession().createQuery(deleteAuth).executeUpdate();
    getSession().createQuery(delete).executeUpdate();
}
 
Example 14
Source File: JpaNoRollbackTest.java    From micronaut-test with Apache License 2.0 5 votes vote down vote up
@AfterAll
void cleanup() {
    final TransactionStatus tx = transactionManager.getTransaction(new DefaultTransactionDefinition());
    final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    final CriteriaDelete<Book> delete = criteriaBuilder.createCriteriaDelete(Book.class);
    delete.from(Book.class);
    entityManager.createQuery(delete).executeUpdate();
    transactionManager.commit(tx);
}
 
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 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() )
	);
}
 
Example 16
Source File: AbstractDao.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 4 votes vote down vote up
public void deleteAll() {
    final CriteriaDelete<T> criteriaDelete = em.getCriteriaBuilder().createCriteriaDelete(clazz);
    criteriaDelete.from(clazz);
    em.createQuery(criteriaDelete).executeUpdate();
}
 
Example 17
Source File: AbstractDao.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 4 votes vote down vote up
public void deleteAll() {
    final CriteriaDelete<T> criteriaDelete = em.getCriteriaBuilder().createCriteriaDelete(clazz);
    criteriaDelete.from(clazz);
    em.createQuery(criteriaDelete).executeUpdate();
}
 
Example 18
Source File: QueryTest.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void testCriteriaEntityQuery(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");
	query.orderBy( builder.asc( b.get("isbn") ) );

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

	CriteriaDelete<Book> delete = builder.createCriteriaDelete(Book.class);
	b = delete.from(Book.class);

	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).getResultList() )
					.thenAccept( books -> {
						context.assertEquals( 3, books.size() );
						books.forEach( book -> {
							context.assertNotNull( book.id );
							context.assertNotNull( book.title );
							context.assertNotNull( book.isbn );
						} );
					} )
					.thenCompose( v -> openSession() )
					.thenCompose( session -> session.createQuery(update).executeUpdate() )
					.thenCompose( v -> openSession() )
					.thenCompose( session -> session.createQuery(delete).executeUpdate() )
	);
}
 
Example 19
Source File: AbstractDao.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 4 votes vote down vote up
public void deleteAll() {
    final CriteriaDelete<T> criteriaDelete = em.getCriteriaBuilder().createCriteriaDelete(clazz);
    criteriaDelete.from(clazz);
    em.createQuery(criteriaDelete).executeUpdate();
}
 
Example 20
Source File: AbstractDao.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 4 votes vote down vote up
public void deleteAll() {
    final CriteriaDelete<T> criteriaDelete = em.getCriteriaBuilder().createCriteriaDelete(clazz);
    criteriaDelete.from(clazz);
    em.createQuery(criteriaDelete).executeUpdate();
}