|
| 1 | +package com.baeldung.jpa.projections; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.hibernate.Criteria; |
| 9 | +import org.hibernate.Session; |
| 10 | +import org.hibernate.SessionFactory; |
| 11 | +import org.hibernate.Transaction; |
| 12 | +import org.hibernate.cfg.AvailableSettings; |
| 13 | +import org.hibernate.cfg.Configuration; |
| 14 | +import org.hibernate.criterion.Order; |
| 15 | +import org.hibernate.criterion.Projections; |
| 16 | +import org.junit.After; |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.BeforeClass; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +public class HibernateProjectionsIntegrationTest { |
| 22 | + private static Session session; |
| 23 | + private static SessionFactory sessionFactory; |
| 24 | + private Transaction transaction; |
| 25 | + |
| 26 | + @BeforeClass |
| 27 | + public static void init() { |
| 28 | + Configuration configuration = getConfiguration(); |
| 29 | + configuration.addAnnotatedClass(Product.class); |
| 30 | + sessionFactory = configuration.buildSessionFactory(); |
| 31 | + } |
| 32 | + |
| 33 | + @Before |
| 34 | + public void before() { |
| 35 | + session = sessionFactory.getCurrentSession(); |
| 36 | + transaction = session.beginTransaction(); |
| 37 | + } |
| 38 | + |
| 39 | + @After |
| 40 | + public void after() { |
| 41 | + if(transaction.isActive()) { |
| 42 | + transaction.rollback(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static Configuration getConfiguration() { |
| 47 | + Configuration cfg = new Configuration(); |
| 48 | + cfg.setProperty(AvailableSettings.DIALECT, |
| 49 | + "org.hibernate.dialect.H2Dialect"); |
| 50 | + cfg.setProperty(AvailableSettings.HBM2DDL_AUTO, "none"); |
| 51 | + cfg.setProperty(AvailableSettings.DRIVER, "org.h2.Driver"); |
| 52 | + cfg.setProperty(AvailableSettings.URL, |
| 53 | + "jdbc:h2:mem:myexceptiondb2;DB_CLOSE_DELAY=-1;;INIT=RUNSCRIPT FROM 'src/test/resources/products.sql'"); |
| 54 | + cfg.setProperty(AvailableSettings.USER, "sa"); |
| 55 | + cfg.setProperty(AvailableSettings.PASS, ""); |
| 56 | + cfg.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "thread"); |
| 57 | + return cfg; |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + @SuppressWarnings("deprecation") |
| 62 | + @Test |
| 63 | + public void givenProductData_whenIdAndNameProjectionUsingCriteria_thenListOfObjectArrayReturned() { |
| 64 | + Criteria criteria = session.createCriteria(Product.class); |
| 65 | + criteria = criteria.setProjection(Projections.projectionList() |
| 66 | + .add(Projections.id()) |
| 67 | + .add(Projections.property("name"))); |
| 68 | + List<Object[]> resultList = criteria.list(); |
| 69 | + |
| 70 | + assertNotNull(resultList); |
| 71 | + assertEquals(4, resultList.size()); |
| 72 | + assertEquals(1L, resultList.get(0)[0]); |
| 73 | + assertEquals("Product Name 1", resultList.get(0)[1]); |
| 74 | + assertEquals(2L, resultList.get(1)[0]); |
| 75 | + assertEquals("Product Name 2", resultList.get(1)[1]); |
| 76 | + assertEquals(3L, resultList.get(2)[0]); |
| 77 | + assertEquals("Product Name 3", resultList.get(2)[1]); |
| 78 | + assertEquals(4L, resultList.get(3)[0]); |
| 79 | + assertEquals("Product Name 4", resultList.get(3)[1]); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + @Test |
| 84 | + public void givenProductData_whenNameProjectionUsingCriteria_thenListOfStringReturned() { |
| 85 | + Criteria criteria = session.createCriteria(Product.class); |
| 86 | + criteria = criteria.setProjection(Projections.property("name")); |
| 87 | + List resultList = criteria.list(); |
| 88 | + |
| 89 | + assertNotNull(resultList); |
| 90 | + assertEquals(4, resultList.size()); |
| 91 | + assertEquals("Product Name 1", resultList.get(0)); |
| 92 | + assertEquals("Product Name 2", resultList.get(1)); |
| 93 | + assertEquals("Product Name 3", resultList.get(2)); |
| 94 | + assertEquals("Product Name 4", resultList.get(3)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void givenProductData_whenCountByCategoryUsingCriteria_thenOK() { |
| 99 | + Criteria criteria = session.createCriteria(Product.class); |
| 100 | + criteria = criteria.setProjection(Projections.projectionList() |
| 101 | + .add(Projections.groupProperty("category")) |
| 102 | + .add(Projections.rowCount())); |
| 103 | + List<Object[]> resultList = criteria.list(); |
| 104 | + |
| 105 | + assertNotNull(resultList); |
| 106 | + assertEquals(3, resultList.size()); |
| 107 | + assertEquals("category1", resultList.get(0)[0]); |
| 108 | + assertEquals(2L, resultList.get(0)[1]); |
| 109 | + assertEquals("category2", resultList.get(1)[0]); |
| 110 | + assertEquals(1L, resultList.get(1)[1]); |
| 111 | + assertEquals("category3", resultList.get(2)[0]); |
| 112 | + assertEquals(1L, resultList.get(2)[1]); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void givenProductData_whenCountByCategoryWithAliasUsingCriteria_thenOK() { |
| 117 | + Criteria criteria = session.createCriteria(Product.class); |
| 118 | + criteria = criteria.setProjection(Projections.projectionList() |
| 119 | + .add(Projections.groupProperty("category")) |
| 120 | + .add(Projections.alias(Projections.rowCount(), "count"))); |
| 121 | + criteria.addOrder(Order.asc("count")); |
| 122 | + List<Object[]> resultList = criteria.list(); |
| 123 | + |
| 124 | + assertNotNull(resultList); |
| 125 | + assertEquals(3, resultList.size()); |
| 126 | + assertEquals("category2", resultList.get(0)[0]); |
| 127 | + assertEquals(1L, resultList.get(0)[1]); |
| 128 | + assertEquals("category3", resultList.get(1)[0]); |
| 129 | + assertEquals(1L, resultList.get(1)[1]); |
| 130 | + assertEquals("category1", resultList.get(2)[0]); |
| 131 | + assertEquals(2L, resultList.get(2)[1]); |
| 132 | + } |
| 133 | +} |
0 commit comments