Bael 4788: Completed Article - Hibernate addScalar() method (#11783)

* BAEL-4788: Added ScalarDemo Class for Hibernate addScalar Method article

* BAEL-4788: Added unit tests in the article

* BAEL-4788: removed redundant code

Co-authored-by: Vikas Ramsingh Rajput <vikas.rajput@crownconsult.com>
This commit is contained in:
Vikas Rajput 2022-02-19 17:06:34 +05:30 committed by GitHub
parent 13a7a1d7eb
commit d28a3e642a
2 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,47 @@
package com.baeldung.hibernate.scalarmethod;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.type.StandardBasicTypes;
public class HibernateScalarExample {
private Session session;
public HibernateScalarExample(Session session) {
this.session = session;
}
public List<Object[]> fetchColumnWithNativeQuery() {
return session.createNativeQuery("SELECT * FROM Student student")
.list();
}
public List<Object[]> fetchColumnWithScalar() {
return session.createNativeQuery("SELECT * FROM Student student")
.addScalar("studentId", StandardBasicTypes.LONG)
.addScalar("name", StandardBasicTypes.STRING)
.addScalar("age", StandardBasicTypes.INTEGER)
.list();
}
public List<String> fetchLimitedColumnWithScalar() {
return session.createNativeQuery("SELECT * FROM Student student")
.addScalar("name", StandardBasicTypes.STRING)
.list();
}
public List<Object[]> fetchColumnWithOverloadedScalar() {
return session.createNativeQuery("SELECT * FROM Student student")
.addScalar("name", StandardBasicTypes.STRING)
.addScalar("age")
.list();
}
public Integer fetchAvgAgeWithScalar() {
return (Integer) session.createNativeQuery("SELECT AVG(age) as avgAge FROM Student student")
.addScalar("avgAge")
.uniqueResult();
}
}

View File

@ -0,0 +1,83 @@
package com.baeldung.hibernate.scalarmethod;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.baeldung.hibernate.HibernateUtil;
import com.baeldung.hibernate.pojo.Student;
public class HibernateScalarExampleUnitTest {
private static Session session;
private static Transaction transaction;
private static final int DATA_SIZE = 50000;
private static HibernateScalarExample scalarExample;
@BeforeClass
public static void setUp() throws IOException {
session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
scalarExample = new HibernateScalarExample(session);
session.createNativeQuery("delete from Student").executeUpdate();
for (int i = 0; i < DATA_SIZE; i++) {
Student student = new Student("John-" + i, generateRandomAge(5, 24));
session.persist(student);
}
transaction.commit();
transaction = session.beginTransaction();
}
@AfterClass
public static void tearDown() {
transaction.rollback();
session.close();
}
@Test
public void givenNativeQuery_whenNoScalarUsed_ThenFetchAll() {
List<Object[]> list = scalarExample.fetchColumnWithNativeQuery();
assertEquals(DATA_SIZE, list.size());
}
@Test
public void givenNativeQuery_whenScalarUsed_ThenFetchAll() {
List<Object[]> list = scalarExample.fetchColumnWithScalar();
assertEquals(DATA_SIZE, list.size());
}
@Test
public void givenNativeQuery_whenScalarUsed_ThenFetchLimitedColumns() {
List<String> list = scalarExample.fetchLimitedColumnWithScalar();
for (String colValue : list) {
assertTrue(colValue.startsWith("John"));
}
}
@Test
public void givenNativeQuery_whenScalarUsedForSingleResult_ThenSingleValueReturned() {
List<Object[]> list = scalarExample.fetchColumnWithOverloadedScalar();
for (Object[] colArray : list) {
assertEquals(2, colArray.length);
}
}
@Test
public void whenScalarUsedForAvgAge_ThenSingleValueReturned() {
Integer avgAge = scalarExample.fetchAvgAgeWithScalar();
assertEquals(true, (avgAge >= 5 && avgAge <= 24));
}
private static int generateRandomAge(int min, int max) {
return new Random().nextInt(max - min + 1) + min;
}
}