Skip to content

Commit 52db7dd

Browse files
shubhi22maibin
authored andcommitted
Bael 2532 hibernate aggregate functions (eugenp#6266)
* BAEL-2532 Hibernate Aggregate Functions * BAEL-2532 Deleting created Student POJO
1 parent d6618b1 commit 52db7dd

File tree

2 files changed

+118
-3
lines changed

2 files changed

+118
-3
lines changed

persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Student.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,43 @@
99
public class Student {
1010

1111
@Id
12-
@GeneratedValue (strategy = GenerationType.SEQUENCE)
12+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
1313
private long studentId;
1414

15+
private String name;
16+
17+
private int age;
18+
19+
public Student() {
20+
}
21+
22+
public Student(String name, int age) {
23+
this.name = name;
24+
this.age = age;
25+
}
26+
1527
public long getStudentId() {
1628
return studentId;
1729
}
1830

19-
public void setStudent_id(long studentId) {
31+
public void setStudentId(long studentId) {
2032
this.studentId = studentId;
2133
}
22-
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
public int getAge() {
44+
return age;
45+
}
46+
47+
public void setAge(int age) {
48+
this.age = age;
49+
}
50+
2351
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.baeldung.hibernate.aggregatefunctions;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.IOException;
6+
7+
import org.hibernate.HibernateException;
8+
import org.hibernate.Session;
9+
import org.hibernate.Transaction;
10+
import org.junit.AfterClass;
11+
import org.junit.BeforeClass;
12+
import org.junit.Test;
13+
14+
import com.baeldung.hibernate.HibernateUtil;
15+
import com.baeldung.hibernate.pojo.Student;
16+
17+
public class AggregateFunctionsIntegrationTest {
18+
19+
private static Session session;
20+
private static Transaction transaction;
21+
22+
@BeforeClass
23+
public static final void setup() throws HibernateException, IOException {
24+
session = HibernateUtil.getSessionFactory()
25+
.openSession();
26+
transaction = session.beginTransaction();
27+
28+
Student jonas = new Student("Jonas", 22);
29+
session.save(jonas);
30+
31+
Student sally = new Student("Sally", 20);
32+
session.save(sally);
33+
34+
Student simon = new Student("Simon", 25);
35+
session.save(simon);
36+
37+
Student raven = new Student("Raven", 21);
38+
session.save(raven);
39+
40+
Student sam = new Student("Sam", 23);
41+
session.save(sam);
42+
43+
}
44+
45+
@AfterClass
46+
public static final void teardown() {
47+
if (session != null) {
48+
transaction.rollback();
49+
session.close();
50+
}
51+
}
52+
53+
@Test
54+
public void whenMaxAge_ThenReturnValue() {
55+
int maxAge = (int) session.createQuery("SELECT MAX(age) from Student")
56+
.getSingleResult();
57+
assertThat(maxAge).isEqualTo(25);
58+
}
59+
60+
@Test
61+
public void whenMinAge_ThenReturnValue() {
62+
int minAge = (int) session.createQuery("SELECT MIN(age) from Student")
63+
.getSingleResult();
64+
assertThat(minAge).isEqualTo(20);
65+
}
66+
67+
@Test
68+
public void whenAverageAge_ThenReturnValue() {
69+
Double avgAge = (Double) session.createQuery("SELECT AVG(age) from Student")
70+
.getSingleResult();
71+
assertThat(avgAge).isEqualTo(22.2);
72+
}
73+
74+
@Test
75+
public void whenCountAll_ThenReturnValue() {
76+
Long totalStudents = (Long) session.createQuery("SELECT COUNT(*) from Student")
77+
.getSingleResult();
78+
assertThat(totalStudents).isEqualTo(5);
79+
}
80+
81+
@Test
82+
public void whenSumOfAllAges_ThenReturnValue() {
83+
Long sumOfAllAges = (Long) session.createQuery("SELECT SUM(age) from Student")
84+
.getSingleResult();
85+
assertThat(sumOfAllAges).isEqualTo(111);
86+
}
87+
}

0 commit comments

Comments
 (0)