Java Code Examples for javax.persistence.criteria.Root#fetch()
The following examples show how to use
javax.persistence.criteria.Root#fetch() .
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: ServerFactory.java From uyuni with GNU General Public License v2.0 | 6 votes |
/** * @param fetchingVirtualGuests eagerly load virtual guests * @param fetchingGroups eagerly load server groups * @return a list of all systems */ public static List<Server> list(boolean fetchingVirtualGuests, boolean fetchingGroups) { CriteriaBuilder builder = getSession().getCriteriaBuilder(); CriteriaQuery<Server> criteria = builder.createQuery(Server.class); Root r = criteria.from(Server.class); if (fetchingVirtualGuests) { r.fetch("virtualGuests", JoinType.LEFT); } if (fetchingGroups) { r.fetch("groups", JoinType.LEFT); } criteria.distinct(true); return getSession().createQuery(criteria).getResultList().stream() .collect(Collectors.toList()); }
Example 2
Source File: MCRUserManager.java From mycore with GNU General Public License v3.0 | 6 votes |
private static Optional<MCRUser> getByNaturalID(EntityManager em, String userName, String realmId) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<MCRUser> query = cb.createQuery(MCRUser.class); Root<MCRUser> users = query.from(MCRUser.class); users.fetch(MCRUser_.owner.getName(), JoinType.LEFT); try { return Optional .of(em .createQuery(query .distinct(true) .where(getUserRealmCriterion(cb, users, userName, realmId))) .getSingleResult()); } catch (NoResultException e) { return Optional.empty(); } }
Example 3
Source File: JpaUtilsTest.java From jdal with Apache License 2.0 | 6 votes |
@Test @Transactional public void testCountFromFetchesCriteria() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Book> bookQuery = cb.createQuery(Book.class); Root<Book> root = bookQuery.from(Book.class); root.fetch("category"); Path<String> namePath = JpaUtils.getPath(root, "category.name"); bookQuery.where(cb.equal(namePath, "Java")); bookQuery.from(Author.class); bookQuery.select(root); CriteriaQuery<Long> countQuery = JpaUtils.countCriteria(em, bookQuery); Long result = Long.valueOf(em.createQuery(bookQuery).getResultList().size()); assertEquals(result, (Long) em.createQuery(countQuery).getSingleResult()); }
Example 4
Source File: AccountDAO.java From Hands-On-High-Performance-with-Spring-5 with MIT License | 5 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public Account findAccountUsingCriteriaAPI(Long accountId) { CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder(); CriteriaQuery<?> query = criteriaBuilder.createQuery(Account.class); Root root = query.from(Account.class); root.fetch("transactions", JoinType.INNER); query.select(root); query.where(criteriaBuilder.equal(root.get("accountId"), accountId)); return (Account)this.getEntityManager().createQuery(query).getSingleResult(); }
Example 5
Source File: ApplicationResource.java From bouncr with Eclipse Public License 1.0 | 5 votes |
@Decision(EXISTS) public boolean exists(Parameters params, RestContext context, EntityManager em) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Application> query = cb.createQuery(Application.class); Root<Application> applicationRoot = query.from(Application.class); query.where(cb.equal(applicationRoot.get("name"), params.get("name"))); List<ResourceField> embedEntities = some(params.get("embed"), embed -> new ResourceFilter().parse(embed)) .orElse(Collections.emptyList()); EntityGraph<Application> applicationGraph = em.createEntityGraph(Application.class); applicationGraph.addAttributeNodes("name", "description", "virtualPath", "passTo", "topPage"); if (embedEntities.stream().anyMatch(r -> r.getName().equalsIgnoreCase("realms"))) { applicationRoot.fetch("realms", JoinType.LEFT); query.distinct(true); applicationGraph.addSubgraph("realms") .addAttributeNodes("name", "description", "url"); } Application application = em.createQuery(query) .setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH) .setHint("javax.persistence.fetchgraph", applicationGraph) .getResultStream().findAny().orElse(null); if (application != null) { context.putValue(application); } return application != null; }
Example 6
Source File: SignUpResource.java From bouncr with Eclipse Public License 1.0 | 5 votes |
private Invitation findInvitation(String code, EntityManager em) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Invitation> query = cb.createQuery(Invitation.class); Root<Invitation> invitationRoot = query.from(Invitation.class); invitationRoot.fetch("groupInvitations", JoinType.LEFT); invitationRoot.fetch("oidcInvitations", JoinType.LEFT); query.where(cb.equal(invitationRoot.get("code"), code)); return em.createQuery(query) .setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH) .getResultStream() .findAny() .orElse(null); }
Example 7
Source File: OidcSignInResource.java From bouncr with Eclipse Public License 1.0 | 5 votes |
private OidcUser findOidcUser(OidcProvider oidcProvider, String sub, EntityManager em) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<OidcUser> query = cb.createQuery(OidcUser.class); Root<OidcUser> root = query.from(OidcUser.class); root.fetch("user"); query.where(cb.equal(root.get("oidcSub"), sub), cb.equal(root.get("oidcProvider"), oidcProvider)); return em.createQuery(query).getResultStream().findAny().orElse(null); }
Example 8
Source File: MCRUserManager.java From mycore with GNU General Public License v3.0 | 5 votes |
/** * Returns a list of all users the given user is owner of. * * @param owner the user that owns other users */ public static List<MCRUser> listUsers(MCRUser owner) { EntityManager em = MCREntityManagerProvider.getCurrentEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<MCRUser> query = cb.createQuery(MCRUser.class); Root<MCRUser> users = query.from(MCRUser.class); users.fetch(MCRUser_.owner); return em.createQuery( query .distinct(true) .where(cb.equal(users.get(MCRUser_.owner), owner))) .getResultList(); }
Example 9
Source File: JoinFetch.java From specification-arg-resolver with Apache License 2.0 | 5 votes |
@Override public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { query.distinct(true); if (!Number.class.isAssignableFrom(query.getResultType())) { // do not join in count queries for (String path : pathsToFetch){ root.fetch(path, joinType); } } return null; }
Example 10
Source File: TestJpaDao.java From jdal with Apache License 2.0 | 5 votes |
@Test @Transactional public void testCopy() { EntityManager em = bookDao.getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Book> criteria = cb.createQuery(Book.class); // Fetch join Root<Book> root = criteria.from(Book.class); Path<String> path = root.join("author").<String>get("name"); root.fetch("author"); criteria.select(root); // SubQuery Subquery<String> sq = criteria.subquery(String.class); Root<Author> author = sq.from(Author.class); sq.select(author.<String>get("name")); sq.where(cb.equal(author.<String>get("name"), "Rod")); criteria.where(cb.in(path).value(sq)); CriteriaQuery<Book> copy = cb.createQuery(Book.class); JpaUtils.copyCriteria(criteria, copy); List<Book> copyBooks = em.createQuery(copy).getResultList(); List<Book> books = em.createQuery(criteria).getResultList(); assertEquals(books, copyBooks); }
Example 11
Source File: BookRepositoryCustomImpl.java From spring-data-examples with Apache License 2.0 | 5 votes |
@Override public List<Book> findByAuthorNameAndTitle( boolean fetchAuthor, boolean distinct, String authorName, String title) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Book> cq = cb.createQuery(Book.class); Root<Book> book = cq.from(Book.class); Path<Object> authors; if (fetchAuthor) { book.fetch("authors"); authors = book.get("authors"); } else { authors = book.join("authors"); } List<Predicate> predicates = new ArrayList<>(); if (authorName != null) { predicates.add(cb.like(authors.get("fullName"), "%" + authorName + "%")); } if (title != null) { predicates.add(cb.like(book.get("title"), "%" + title + "%")); } cq.select(book) .distinct(distinct) .where(predicates.toArray(new Predicate[0])) .orderBy(cb.asc(book.get("publicationDate"))); return em.createQuery(cq).getResultList(); }
Example 12
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 13
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() ) ); }
Example 14
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() ) ); }