Skip to content

Commit d0ff2aa

Browse files
author
Chris Franklin
committed
Code samples for Simple Tagging with JPA
1 parent eaf4f4c commit d0ff2aa

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

‎persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
import org.baeldung.inmemory.persistence.model.Student;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.data.repository.query.Param;
7+
8+
import java.util.List;
59

610
public interface StudentRepository extends JpaRepository<Student, Long> {
11+
12+
@Query("SELECT s FROM Student s JOIN s.tags t WHERE t = LOWER(:tag)")
13+
List<Student> retrieveByTag(@Param("tag") String tag);
14+
15+
@Query("SELECT s FROM Student s JOIN s.tags t WHERE s.name = LOWER(:name) AND t = LOWER(:tag)")
16+
List<Student> retrieveByNameFilterByTag(@Param("name") String name, @Param("tag") String tag);
17+
718
}

‎persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.baeldung.inmemory.persistence.model;
22

3+
import javax.persistence.ElementCollection;
34
import javax.persistence.Entity;
45
import javax.persistence.Id;
6+
import java.util.ArrayList;
7+
import java.util.List;
58

69
@Entity
710
public class Student {
@@ -10,6 +13,9 @@ public class Student {
1013
private long id;
1114
private String name;
1215

16+
@ElementCollection
17+
private List<String> tags = new ArrayList<>();
18+
1319
public Student() {
1420
}
1521

@@ -35,4 +41,11 @@ public void setName(String name) {
3541
this.name = name;
3642
}
3743

44+
public List<String> getTags() {
45+
return tags;
46+
}
47+
48+
public void setTags(List<String> tags) {
49+
this.tags.addAll(tags);
50+
}
3851
}

‎persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
import javax.annotation.Resource;
1414

15+
import java.util.Arrays;
16+
import java.util.List;
17+
1518
import static org.junit.Assert.assertEquals;
1619

1720
@RunWith(SpringJUnit4ClassRunner.class)
@@ -34,4 +37,46 @@ public void givenStudent_whenSave_thenGetOk(){
3437
assertEquals("name incorrect", NAME, student2.getName());
3538
}
3639

40+
@Test
41+
public void givenStudentWithTags_whenSave_thenGetByTagOk(){
42+
Student student = new Student(ID, NAME);
43+
student.setTags(Arrays.asList("full time", "computer science"));
44+
studentRepository.save(student);
45+
46+
Student student2 = studentRepository.retrieveByTag("full time").get(0);
47+
assertEquals("name incorrect", NAME, student2.getName());
48+
}
49+
50+
@Test
51+
public void givenMultipleStudentsWithTags_whenSave_thenGetByTagReturnsCorrectCount(){
52+
Student student = new Student(0, "Larry");
53+
student.setTags(Arrays.asList("full time", "computer science"));
54+
studentRepository.save(student);
55+
56+
Student student2 = new Student(1, "Curly");
57+
student2.setTags(Arrays.asList("part time", "rocket science"));
58+
studentRepository.save(student2);
59+
60+
Student student3 = new Student(2, "Moe");
61+
student3.setTags(Arrays.asList("full time", "philosophy"));
62+
studentRepository.save(student3);
63+
64+
Student student4 = new Student(3, "Shemp");
65+
student4.setTags(Arrays.asList("part time", "mathematics"));
66+
studentRepository.save(student4);
67+
68+
List<Student> students = studentRepository.retrieveByTag("full time");
69+
assertEquals("size incorrect", 2, students.size());
70+
}
71+
72+
@Test
73+
public void givenStudentWithTags_whenSave_thenGetByNameAndTagOk(){
74+
Student student = new Student(ID, NAME);
75+
student.setTags(Arrays.asList("full time", "computer science"));
76+
studentRepository.save(student);
77+
78+
Student student2 = studentRepository.retrieveByNameFilterByTag("John", "full time").get(0);
79+
assertEquals("name incorrect", NAME, student2.getName());
80+
}
81+
3782
}

0 commit comments

Comments
 (0)