Search in sources :

Example 1 with QBlogPost

use of org.baeldung.querydsl.intro.entities.QBlogPost in project tutorials by eugenp.

the class QueryDSLIntegrationTest method whenGroupingByTitle_thenReturnsTuples.

@Test
public void whenGroupingByTitle_thenReturnsTuples() {
    QBlogPost blogPost = QBlogPost.blogPost;
    NumberPath<Long> count = Expressions.numberPath(Long.class, "c");
    List<Tuple> userTitleCounts = queryFactory.select(blogPost.title, blogPost.id.count().as(count)).from(blogPost).groupBy(blogPost.title).orderBy(count.desc()).fetch();
    assertEquals("Hello World!", userTitleCounts.get(0).get(blogPost.title));
    assertEquals(new Long(2), userTitleCounts.get(0).get(count));
    assertEquals("My Second Post", userTitleCounts.get(1).get(blogPost.title));
    assertEquals(new Long(1), userTitleCounts.get(1).get(count));
}
Also used : QBlogPost(org.baeldung.querydsl.intro.entities.QBlogPost) Tuple(com.querydsl.core.Tuple)

Example 2 with QBlogPost

use of org.baeldung.querydsl.intro.entities.QBlogPost in project tutorials by eugenp.

the class QueryDSLIntegrationTest method whenJoiningWithCondition_thenResultCountShouldMatch.

@Test
public void whenJoiningWithCondition_thenResultCountShouldMatch() {
    QUser user = QUser.user;
    QBlogPost blogPost = QBlogPost.blogPost;
    List<User> users = queryFactory.selectFrom(user).innerJoin(user.blogPosts, blogPost).on(blogPost.title.eq("Hello World!")).fetch();
    assertEquals(2, users.size());
}
Also used : QUser(org.baeldung.querydsl.intro.entities.QUser) QBlogPost(org.baeldung.querydsl.intro.entities.QBlogPost) QUser(org.baeldung.querydsl.intro.entities.QUser) User(org.baeldung.querydsl.intro.entities.User)

Example 3 with QBlogPost

use of org.baeldung.querydsl.intro.entities.QBlogPost in project tutorials by eugenp.

the class QueryDSLIntegrationTest method whenRefiningWithSubquery_thenResultCountShouldMatch.

@Test
public void whenRefiningWithSubquery_thenResultCountShouldMatch() {
    QUser user = QUser.user;
    QBlogPost blogPost = QBlogPost.blogPost;
    List<User> users = queryFactory.selectFrom(user).where(user.id.in(JPAExpressions.select(blogPost.user.id).from(blogPost).where(blogPost.title.eq("Hello World!")))).fetch();
    assertEquals(2, users.size());
}
Also used : QUser(org.baeldung.querydsl.intro.entities.QUser) QBlogPost(org.baeldung.querydsl.intro.entities.QBlogPost) QUser(org.baeldung.querydsl.intro.entities.QUser) User(org.baeldung.querydsl.intro.entities.User)

Aggregations

QBlogPost (org.baeldung.querydsl.intro.entities.QBlogPost)3 QUser (org.baeldung.querydsl.intro.entities.QUser)2 User (org.baeldung.querydsl.intro.entities.User)2 Tuple (com.querydsl.core.Tuple)1