Skip to content

Commit

Permalink
BAEL-8040: Find distinct rows using Spring data JPA (eugenp#16798)
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred106 authored Jun 11, 2024
1 parent 806d430 commit 598a5a5
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 0 deletions.
23 changes: 23 additions & 0 deletions persistence-modules/spring-data-jpa-query-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
<relativePath>../../parent-boot-3</relativePath>
</parent>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -74,11 +86,22 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung.distinct;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DistinctDataApplication {

public static void main(String[] args) {
SpringApplication.run(DistinctDataApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.baeldung.distinct;

public interface NameView {

String getName();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.baeldung.distinct;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.List;

@Entity
@Table(name = "school")
@Data
@ToString(exclude = "students")
@EqualsAndHashCode(of = "id")
public class School {

@Id
@Column(name = "school_id")
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int id;

@Column(name = "name", length = 100)
private String name;

@OneToMany(mappedBy = "school")
private List<Student> students;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.baeldung.distinct;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SchoolRepository extends JpaRepository<School, Integer> {

List<School> findDistinctByStudentsBirthYear(int birthYear);

Long countDistinctByStudentsBirthYear(int birthYear);

@Query("SELECT DISTINCT sch.name FROM School sch JOIN sch.students stu WHERE stu.birthYear = :birthYear")
List<String> findDistinctSchoolNameByStudentsBirthYear(@Param("birthYear") int birthYear);

List<NameView> findDistinctNameByStudentsBirthYear(int birthYear);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.baeldung.distinct;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

@Entity
@Table(name = "student")
@Data
@ToString(exclude = "school")
@EqualsAndHashCode(of = "id")
public class Student {

@Id
@Column(name = "student_id")
private int id;

@Column(name = "name", length = 100)
private String name;

@Column(name = "birth_year")
private int birthYear;

@ManyToOne
@JoinColumn(name = "school_id", referencedColumnName = "school_id")
private School school;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.baeldung.distinct;

import jakarta.inject.Inject;

import java.util.List;

import org.springframework.boot.test.context.SpringBootTest;

import org.junit.jupiter.api.Test;

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

@SpringBootTest(classes = DistinctDataApplication.class, properties = {
"spring.jpa.show-sql=true",
"spring.jpa.properties.hibernate.format_sql=true",
"spring.jpa.generate-ddl=true",
"spring.jpa.defer-datasource-initialization=true",
"spring.sql.init.data-locations=classpath:school-data.sql"
})
class DistinctDataIntegrationTest {

@Inject
private SchoolRepository schoolRepository;

@Test
void whenFindDistinctByStudentsBirthYear_thenReturnOneSchoolEntity() {
List<School> schoolList = schoolRepository.findDistinctByStudentsBirthYear(2011);
assertThat(schoolList.size()).isEqualTo(1);
assertThat(schoolList.get(0).getId()).isEqualTo(2);
}

@Test
void whenCountDistinctByStudentsBirthYear_thenReturnOne() {
assertThat(schoolRepository.countDistinctByStudentsBirthYear(2011)).isEqualTo(1);
}

@Test
void whenFindDistinctSchoolNameByStudentsBirthYear_thenReturnOneSchoolName() {
List<String> schoolNameList = schoolRepository.findDistinctSchoolNameByStudentsBirthYear(2011);
assertThat(schoolNameList.size()).isEqualTo(1);
assertThat(schoolNameList.get(0)).isEqualTo("St Joesph's College");
}

@Test
void whenFindDistinctNameByStudentsBirthYear_thenReturnOneSchoolName() {
List<NameView> schoolNameList = schoolRepository.findDistinctNameByStudentsBirthYear(2011);
assertThat(schoolNameList.size()).isEqualTo(1);
assertThat(schoolNameList.get(0).getName()).isEqualTo("St Joesph's College");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
INSERT INTO school(name) VALUES ('Queen''s College');
INSERT INTO school(name) VALUES ('St Joesph''s College');
INSERT INTO school(name) VALUES ('Maryknoll Convent School');

INSERT INTO student (student_id, name, birth_year, school_id) VALUES (24058789, 'Chan Tai Man', 2010, 1);
INSERT INTO student (student_id, name, birth_year, school_id) VALUES (24058790, 'Wong Siu Ming', 2009, 1);
INSERT INTO student (student_id, name, birth_year, school_id) VALUES (24058791, 'Leung Kwok Wai', 2008, 1);
INSERT INTO student (student_id, name, birth_year, school_id) VALUES (24058792, 'Cheung Chi Kin', 2010, 1);
INSERT INTO student (student_id, name, birth_year, school_id) VALUES (24058793, 'Ho Man Keung', 2009, 1);
INSERT INTO student (student_id, name, birth_year, school_id) VALUES (24058794, 'Lee Ka Yiu', 2008, 2);
INSERT INTO student (student_id, name, birth_year, school_id) VALUES (24058795, 'Lau Ka Ho', 2010, 2);
INSERT INTO student (student_id, name, birth_year, school_id) VALUES (24058796, 'Ng Chi Ho', 2009, 2);
INSERT INTO student (student_id, name, birth_year, school_id) VALUES (24058797, 'Lam Hoi Chun', 2011, 2);
INSERT INTO student (student_id, name, birth_year, school_id) VALUES (24058798, 'Yip Chun Hei', 2011, 2);

0 comments on commit 598a5a5

Please sign in to comment.