Java Code Examples for javax.persistence.criteria.CriteriaBuilder#createCriteriaDelete()
The following examples show how to use
javax.persistence.criteria.CriteriaBuilder#createCriteriaDelete() .
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 |
@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: TestCriteriaDelete.java From HibernateTips with MIT License | 7 votes |
@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 3
Source File: MCRMetadataHistoryCommands.java From mycore with GNU General Public License v3.0 | 5 votes |
@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 4
Source File: PostsBean.java From ee7-sandbox with Apache License 2.0 | 5 votes |
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 5
Source File: PostsBean.java From ee7-sandbox with Apache License 2.0 | 5 votes |
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: PostsBean.java From ee7-sandbox with Apache License 2.0 | 5 votes |
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 7
Source File: PostsBean.java From ee7-sandbox with Apache License 2.0 | 5 votes |
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 8
Source File: PostsBean.java From ee7-sandbox with Apache License 2.0 | 5 votes |
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: AbstractRepository.java From batchers with Apache License 2.0 | 5 votes |
public void deleteAll() { CriteriaBuilder criteriaBuilder = entityManagerFactory.getCriteriaBuilder(); CriteriaDelete<E> criteriaDelete = criteriaBuilder.createCriteriaDelete(getEntityClass()); criteriaDelete.from(getEntityClass()); entityManager.createQuery(criteriaDelete).executeUpdate(); }
Example 10
Source File: CurrentVariantRollbackService.java From mojito with Apache License 2.0 | 5 votes |
/** * 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 11
Source File: MCRMetadataHistoryCommands.java From mycore with GNU General Public License v3.0 | 5 votes |
@MCRCommand(syntax = "clear metadata history of id {0}", help = "clears metadata history of object/derivate with id {0}") public static void clearSingleHistory(String mcrId) { 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.equal(item.get(MCRMetaHistoryItem_.id).as(String.class), mcrId))) .executeUpdate(); LogManager.getLogger().info("Deleted {} items in history of {}", rowsDeleted, mcrId); }
Example 12
Source File: ActionRowDeleteBatch.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
private <T extends JpaObject> Integer delete(Business business, Class<T> cls, List<String> ids) throws Exception { EntityManager em = business.entityManagerContainer().get(cls); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaDelete<T> cd = cb.createCriteriaDelete(cls); Root<T> root = cd.from(cls); Predicate p = cb.isMember(root.get(JpaObject.id_FIELDNAME), cb.literal(ids)); return em.createQuery(cd.where(p)).executeUpdate(); }
Example 13
Source File: ActionRowDeleteBatch.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
private <T extends JpaObject> Integer delete(Business business, Class<T> cls, List<String> ids) throws Exception { EntityManager em = business.entityManagerContainer().get(cls); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaDelete<T> cd = cb.createCriteriaDelete(cls); Root<T> root = cd.from(cls); Predicate p = cb.isMember(root.get(JpaObject.id_FIELDNAME), cb.literal(ids)); return em.createQuery(cd.where(p)).executeUpdate(); }
Example 14
Source File: SCCCachingFactory.java From uyuni with GNU General Public License v2.0 | 5 votes |
/** * 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 15
Source File: SCCCachingFactory.java From uyuni with GNU General Public License v2.0 | 5 votes |
/** * 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 16
Source File: SCCCachingFactory.java From uyuni with GNU General Public License v2.0 | 5 votes |
/** * 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 17
Source File: JpaNoRollbackTest.java From micronaut-test with Apache License 2.0 | 5 votes |
@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 18
Source File: QueryTest.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 4 votes |
@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: QueryTest.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 4 votes |
@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 20
Source File: QueryTest.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 4 votes |
@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() ) ); }