forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-8040: Find distinct rows using Spring data JPA (eugenp#16798)
- Loading branch information
1 parent
806d430
commit 598a5a5
Showing
8 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
.../spring-data-jpa-query-2/src/main/java/com/baeldung/distinct/DistinctDataApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...istence-modules/spring-data-jpa-query-2/src/main/java/com/baeldung/distinct/NameView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.baeldung.distinct; | ||
|
||
public interface NameView { | ||
|
||
String getName(); | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
persistence-modules/spring-data-jpa-query-2/src/main/java/com/baeldung/distinct/School.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...modules/spring-data-jpa-query-2/src/main/java/com/baeldung/distinct/SchoolRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
persistence-modules/spring-data-jpa-query-2/src/main/java/com/baeldung/distinct/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...ing-data-jpa-query-2/src/test/java/com/baeldung/distinct/DistinctDataIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
persistence-modules/spring-data-jpa-query-2/src/test/resources/school-data.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |