[BAEL-1575] Advanced tagging implementation with JPA

This commit is contained in:
Chris Franklin 2018-02-23 17:01:23 -05:00
parent 42ffe6cb7c
commit 64dd15eaf4
10 changed files with 276 additions and 4 deletions

View File

@ -0,0 +1,10 @@
package org.baeldung.inmemory.persistence.dao;
import org.baeldung.inmemory.persistence.model.ManyStudent;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface ManyStudentRepository extends JpaRepository<ManyStudent, Long> {
List<ManyStudent> findByManyTags_Name(String name);
}

View File

@ -0,0 +1,7 @@
package org.baeldung.inmemory.persistence.dao;
import org.baeldung.inmemory.persistence.model.ManyTag;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ManyTagRepository extends JpaRepository<ManyTag, Long> {
}

View File

@ -15,4 +15,10 @@ public interface StudentRepository extends JpaRepository<Student, Long> {
@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);
@Query("SELECT s FROM Student s JOIN s.skillTags t WHERE t.name = LOWER(:tagName) AND t.value > :tagValue")
List<Student> retrieveByNameFilterByMinimumSkillTag(@Param("tagName") String tagName, @Param("tagValue") int tagValue);
@Query("SELECT s FROM Student s JOIN s.kvTags t WHERE t.key = LOWER(:key)")
List<Student> retrieveByKeyTag(@Param("key") String key);
}

View File

@ -0,0 +1,33 @@
package org.baeldung.inmemory.persistence.model;
import javax.persistence.Embeddable;
@Embeddable
public class KVTag {
private String key;
private String value;
public KVTag(){}
public KVTag(String key, String value) {
super();
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,39 @@
package org.baeldung.inmemory.persistence.model;
import javax.persistence.Embeddable;
@Embeddable
public class LocationTag {
private String name;
private int xPos;
private int yPos;
public LocationTag(){}
public LocationTag(String name, int xPos, int yPos) {
super();
this.name = name;
this.xPos = xPos;
this.yPos = yPos;
}
public String getName() {
return name;
}
public int getxPos() {
return xPos;
}
public void setxPos(int xPos) {
this.xPos = xPos;
}
public int getyPos() {
return yPos;
}
public void setyPos(int yPos) {
this.yPos = yPos;
}
}

View File

@ -0,0 +1,34 @@
package org.baeldung.inmemory.persistence.model;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
public class ManyStudent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "manystudent_manytags",
joinColumns = @JoinColumn(name = "manystudent_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "manytag_id", referencedColumnName = "id"))
private Set<ManyTag> manyTags = new HashSet<>();
public ManyStudent() {}
public ManyStudent(String name) {
this.name = name;
}
public Set<ManyTag> getManyTags() {
return manyTags;
}
public void setManyTags(Set<ManyTag> manyTags) {
this.manyTags.addAll(manyTags);
}
}

View File

@ -0,0 +1,39 @@
package org.baeldung.inmemory.persistence.model;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
public class ManyTag {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@ManyToMany(mappedBy = "manyTags")
private Set<ManyStudent> students = new HashSet<>();
public ManyTag() {}
public ManyTag(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<ManyStudent> getStudents() {
return students;
}
public void setStudents(Set<ManyStudent> students) {
this.students.addAll(students);
}
}

View File

@ -0,0 +1,29 @@
package org.baeldung.inmemory.persistence.model;
import javax.persistence.Embeddable;
@Embeddable
public class SkillTag {
private String name;
private int value;
public SkillTag(){}
public SkillTag(String name, int value) {
super();
this.name = name;
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getName() {
return name;
}
}

View File

@ -1,10 +1,10 @@
package org.baeldung.inmemory.persistence.model;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Entity
public class Student {
@ -16,6 +16,12 @@ public class Student {
@ElementCollection
private List<String> tags = new ArrayList<>();
@ElementCollection
private List<SkillTag> skillTags = new ArrayList<>();
@ElementCollection
private List<KVTag> kvTags = new ArrayList<>();
public Student() {
}
@ -48,4 +54,21 @@ public class Student {
public void setTags(List<String> tags) {
this.tags.addAll(tags);
}
public List<SkillTag> getSkillTags() {
return skillTags;
}
public void setSkillTags(List<SkillTag> skillTags) {
this.skillTags.addAll(skillTags);
}
public List<KVTag> getKVTags() {
return this.kvTags;
}
public void setKVTags(List<KVTag> kvTags) {
this.kvTags.addAll(kvTags);
}
}

View File

@ -1,8 +1,10 @@
package org.baeldung.persistence.repository;
import org.baeldung.config.StudentJpaConfig;
import org.baeldung.inmemory.persistence.dao.ManyStudentRepository;
import org.baeldung.inmemory.persistence.dao.ManyTagRepository;
import org.baeldung.inmemory.persistence.dao.StudentRepository;
import org.baeldung.inmemory.persistence.model.Student;
import org.baeldung.inmemory.persistence.model.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
@ -13,6 +15,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertEquals;
@ -24,6 +27,12 @@ public class InMemoryDBIntegrationTest {
@Resource
private StudentRepository studentRepository;
@Resource
private ManyStudentRepository manyStudentRepository;
@Resource
private ManyTagRepository manyTagRepository;
private static final long ID = 1;
private static final String NAME="john";
@ -79,4 +88,47 @@ public class InMemoryDBIntegrationTest {
assertEquals("name incorrect", NAME, student2.getName());
}
@Test
public void givenStudenWithSkillTags_whenSave_thenGetByNameAndSkillTag() {
Student student = new Student(1, "Will");
SkillTag skill1 = new SkillTag("java", 5);
student.setSkillTags(Arrays.asList(skill1));
studentRepository.save(student);
Student student2 = new Student(2, "Joe");
SkillTag skill2 = new SkillTag("java", 1);
student2.setSkillTags(Arrays.asList(skill2));
studentRepository.save(student2);
List<Student> students = studentRepository.retrieveByNameFilterByMinimumSkillTag("java", 3);
assertEquals("size incorrect", 1, students.size());
}
@Test
public void givenStudentWithKVTags_whenSave_thenGetByTagOk(){
Student student = new Student(0, "John");
student.setKVTags(Arrays.asList(new KVTag("department", "computer science")));
studentRepository.save(student);
Student student2 = new Student(1, "James");
student2.setKVTags(Arrays.asList(new KVTag("department", "humanities")));
studentRepository.save(student2);
List<Student> students = studentRepository.retrieveByKeyTag("department");
assertEquals("size incorrect", 2, students.size());
}
@Test
public void givenStudentWithManyTags_whenSave_theyGetByTagOk() {
ManyTag tag = new ManyTag("full time");
manyTagRepository.save(tag);
ManyStudent student = new ManyStudent("John");
student.setManyTags(Collections.singleton(tag));
manyStudentRepository.save(student);
List<ManyStudent> students = manyStudentRepository.findByManyTags_Name("full time");
assertEquals("size incorrect", 1, students.size());
}
}