Skip to content

Commit

Permalink
Bael 2532 hibernate aggregate functions (eugenp#6266)
Browse files Browse the repository at this point in the history
* BAEL-2532 Hibernate Aggregate Functions

* BAEL-2532 Deleting created Student POJO
  • Loading branch information
shubhi22 authored and maibin committed Feb 3, 2019
1 parent d6618b1 commit 52db7dd
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,43 @@
public class Student {

@Id
@GeneratedValue (strategy = GenerationType.SEQUENCE)
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long studentId;

private String name;

private int age;

public Student() {
}

public Student(String name, int age) {
this.name = name;
this.age = age;
}

public long getStudentId() {
return studentId;
}

public void setStudent_id(long studentId) {
public void setStudentId(long studentId) {
this.studentId = studentId;
}


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.baeldung.hibernate.aggregatefunctions;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;

import org.hibernate.HibernateException;
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 AggregateFunctionsIntegrationTest {

private static Session session;
private static Transaction transaction;

@BeforeClass
public static final void setup() throws HibernateException, IOException {
session = HibernateUtil.getSessionFactory()
.openSession();
transaction = session.beginTransaction();

Student jonas = new Student("Jonas", 22);
session.save(jonas);

Student sally = new Student("Sally", 20);
session.save(sally);

Student simon = new Student("Simon", 25);
session.save(simon);

Student raven = new Student("Raven", 21);
session.save(raven);

Student sam = new Student("Sam", 23);
session.save(sam);

}

@AfterClass
public static final void teardown() {
if (session != null) {
transaction.rollback();
session.close();
}
}

@Test
public void whenMaxAge_ThenReturnValue() {
int maxAge = (int) session.createQuery("SELECT MAX(age) from Student")
.getSingleResult();
assertThat(maxAge).isEqualTo(25);
}

@Test
public void whenMinAge_ThenReturnValue() {
int minAge = (int) session.createQuery("SELECT MIN(age) from Student")
.getSingleResult();
assertThat(minAge).isEqualTo(20);
}

@Test
public void whenAverageAge_ThenReturnValue() {
Double avgAge = (Double) session.createQuery("SELECT AVG(age) from Student")
.getSingleResult();
assertThat(avgAge).isEqualTo(22.2);
}

@Test
public void whenCountAll_ThenReturnValue() {
Long totalStudents = (Long) session.createQuery("SELECT COUNT(*) from Student")
.getSingleResult();
assertThat(totalStudents).isEqualTo(5);
}

@Test
public void whenSumOfAllAges_ThenReturnValue() {
Long sumOfAllAges = (Long) session.createQuery("SELECT SUM(age) from Student")
.getSingleResult();
assertThat(sumOfAllAges).isEqualTo(111);
}
}

0 comments on commit 52db7dd

Please sign in to comment.