use of org.baeldung.querydsl.intro.entities.User in project tutorials by eugenp.
the class QueryDSLIntegrationTest method whenUsingOrderBy_thenResultsShouldBeOrdered.
@Test
public void whenUsingOrderBy_thenResultsShouldBeOrdered() {
QUser user = QUser.user;
List<User> users = queryFactory.selectFrom(user).orderBy(user.login.asc()).fetch();
assertEquals(users.size(), 4);
assertEquals(users.get(0).getLogin(), "Ash");
assertEquals(users.get(1).getLogin(), "Bishop");
assertEquals(users.get(2).getLogin(), "Call");
assertEquals(users.get(3).getLogin(), "David");
}
use of org.baeldung.querydsl.intro.entities.User in project tutorials by eugenp.
the class QueryDSLIntegrationTest method whenFindByLogin_thenShouldReturnUser.
@Test
public void whenFindByLogin_thenShouldReturnUser() {
QUser user = QUser.user;
User aUser = queryFactory.selectFrom(user).where(user.login.eq("David")).fetchOne();
assertNotNull(aUser);
assertEquals(aUser.getLogin(), "David");
}
use of org.baeldung.querydsl.intro.entities.User in project tutorials by eugenp.
the class QueryDSLIntegrationTest method populateDatabase.
@BeforeClass
public static void populateDatabase() {
emf = Persistence.createEntityManagerFactory("org.baeldung.querydsl.intro");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
User user1 = new User();
user1.setLogin("David");
em.persist(user1);
User user2 = new User();
user2.setLogin("Ash");
em.persist(user2);
User user3 = new User();
user3.setLogin("Call");
em.persist(user3);
User user4 = new User();
user4.setLogin("Bishop");
em.persist(user4);
BlogPost blogPost1 = new BlogPost();
blogPost1.setTitle("Hello World!");
blogPost1.setUser(user1);
em.persist(blogPost1);
BlogPost blogPost2 = new BlogPost();
blogPost2.setTitle("My Second Post");
blogPost2.setUser(user1);
em.persist(blogPost2);
BlogPost blogPost3 = new BlogPost();
blogPost3.setTitle("Hello World!");
blogPost3.setUser(user3);
em.persist(blogPost3);
em.getTransaction().commit();
em.close();
}
use of org.baeldung.querydsl.intro.entities.User 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());
}
use of org.baeldung.querydsl.intro.entities.User 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());
}
Aggregations