Code samples for Simple Tagging with JPA

This commit is contained in:
Chris Franklin 2018-02-09 14:37:58 -05:00
parent eaf4f4c0f2
commit d0ff2aa57d
3 changed files with 69 additions and 0 deletions

View File

@ -2,6 +2,17 @@ package org.baeldung.inmemory.persistence.dao;
import org.baeldung.inmemory.persistence.model.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface StudentRepository extends JpaRepository<Student, Long> {
@Query("SELECT s FROM Student s JOIN s.tags t WHERE t = LOWER(:tag)")
List<Student> retrieveByTag(@Param("tag") String tag);
@Query("SELECT s FROM Student s JOIN s.tags t WHERE s.name = LOWER(:name) AND t = LOWER(:tag)")
List<Student> retrieveByNameFilterByTag(@Param("name") String name, @Param("tag") String tag);
}

View File

@ -1,7 +1,10 @@
package org.baeldung.inmemory.persistence.model;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Student {
@ -10,6 +13,9 @@ public class Student {
private long id;
private String name;
@ElementCollection
private List<String> tags = new ArrayList<>();
public Student() {
}
@ -35,4 +41,11 @@ public class Student {
this.name = name;
}
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags.addAll(tags);
}
}

View File

@ -12,6 +12,9 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ -34,4 +37,46 @@ public class InMemoryDBIntegrationTest {
assertEquals("name incorrect", NAME, student2.getName());
}
@Test
public void givenStudentWithTags_whenSave_thenGetByTagOk(){
Student student = new Student(ID, NAME);
student.setTags(Arrays.asList("full time", "computer science"));
studentRepository.save(student);
Student student2 = studentRepository.retrieveByTag("full time").get(0);
assertEquals("name incorrect", NAME, student2.getName());
}
@Test
public void givenMultipleStudentsWithTags_whenSave_thenGetByTagReturnsCorrectCount(){
Student student = new Student(0, "Larry");
student.setTags(Arrays.asList("full time", "computer science"));
studentRepository.save(student);
Student student2 = new Student(1, "Curly");
student2.setTags(Arrays.asList("part time", "rocket science"));
studentRepository.save(student2);
Student student3 = new Student(2, "Moe");
student3.setTags(Arrays.asList("full time", "philosophy"));
studentRepository.save(student3);
Student student4 = new Student(3, "Shemp");
student4.setTags(Arrays.asList("part time", "mathematics"));
studentRepository.save(student4);
List<Student> students = studentRepository.retrieveByTag("full time");
assertEquals("size incorrect", 2, students.size());
}
@Test
public void givenStudentWithTags_whenSave_thenGetByNameAndTagOk(){
Student student = new Student(ID, NAME);
student.setTags(Arrays.asList("full time", "computer science"));
studentRepository.save(student);
Student student2 = studentRepository.retrieveByNameFilterByTag("John", "full time").get(0);
assertEquals("name incorrect", NAME, student2.getName());
}
}