From d76d39fb87892a38da04105836facefbc7face5c Mon Sep 17 00:00:00 2001 From: Chris Franklin Date: Fri, 22 Dec 2017 10:59:53 -0500 Subject: [PATCH 1/8] Christopher Franklin Different Types of Bean Injection in Spring My first article about the three different types of bean injection supported by the Spring Framework. This code walks through all three methods and has supporting tests. --- .../baeldung/beaninjectiontypes/Config.java | 16 ++++++++ .../beaninjectiontypes/ExampleService.java | 11 +++++ .../ExampleWithConstructorInjection.java | 19 +++++++++ .../ExampleWithPropertyInjection.java | 16 ++++++++ .../ExampleWithSetterInjection.java | 20 +++++++++ .../beaninjectiontypes/BeanInjectionTest.java | 41 +++++++++++++++++++ 6 files changed, 123 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleService.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithConstructorInjection.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithPropertyInjection.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithSetterInjection.java create mode 100644 spring-core/src/test/java/com/baeldung/beaninjectiontypes/BeanInjectionTest.java diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java new file mode 100644 index 0000000000..d523dc3f1f --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java @@ -0,0 +1,16 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.baeldung.beaninjectiontypes") +public class Config { + + @Bean + public ExampleService exampleService() { + return new ExampleService(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleService.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleService.java new file mode 100644 index 0000000000..9112fee577 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleService.java @@ -0,0 +1,11 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.stereotype.Component; + +@Component +public class ExampleService { + + public String getExampleText() { + return "Example"; + } +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithConstructorInjection.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithConstructorInjection.java new file mode 100644 index 0000000000..340d283dbd --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithConstructorInjection.java @@ -0,0 +1,19 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ExampleWithConstructorInjection { + + private ExampleService exampleService; + + @Autowired + public ExampleWithConstructorInjection(ExampleService exampleService) { + this.exampleService = exampleService; + } + + public String verifyInjection() { + return this.exampleService.getExampleText(); + } +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithPropertyInjection.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithPropertyInjection.java new file mode 100644 index 0000000000..d9495e5c05 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithPropertyInjection.java @@ -0,0 +1,16 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ExampleWithPropertyInjection { + + @Autowired + private ExampleService exampleService; + + public String verifyInjection() { + return this.exampleService.getExampleText(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithSetterInjection.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithSetterInjection.java new file mode 100644 index 0000000000..97f1355ec5 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithSetterInjection.java @@ -0,0 +1,20 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ExampleWithSetterInjection { + + private ExampleService exampleService; + + @Autowired + public void setExampleService(ExampleService exampleService) { + this.exampleService = exampleService; + } + + public String verifyInjection() { + return this.exampleService.getExampleText(); + } + +} diff --git a/spring-core/src/test/java/com/baeldung/beaninjectiontypes/BeanInjectionTest.java b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/BeanInjectionTest.java new file mode 100644 index 0000000000..ae609d5df5 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/BeanInjectionTest.java @@ -0,0 +1,41 @@ +package com.baeldung.beaninjectiontypes; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = Config.class) +public class BeanInjectionTest { + + @Test + public void whenConstructorInjection_ThenBeanValid() { + ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); + ExampleWithConstructorInjection exampleBean = context.getBean(ExampleWithConstructorInjection.class); + + assertEquals("Example", exampleBean.verifyInjection()); + } + + @Test + public void whenSetterInjection_ThenBeanValid() { + ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); + ExampleWithSetterInjection exampleBean = context.getBean(ExampleWithSetterInjection.class); + + assertEquals("Example", exampleBean.verifyInjection()); + } + + @Test + public void whenProperyInjection_ThenBeanValid() { + ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); + ExampleWithPropertyInjection exampleBean = context.getBean(ExampleWithPropertyInjection.class); + + assertEquals("Example", exampleBean.verifyInjection()); + } + +} From 4f7d19e86a42172c0c49eee1c9d6e9d7b75aa666 Mon Sep 17 00:00:00 2001 From: Chris Franklin Date: Tue, 26 Dec 2017 12:19:13 -0500 Subject: [PATCH 2/8] Revert "Christopher Franklin Different Types of Bean Injection in Spring" This reverts commit d76d39fb87892a38da04105836facefbc7face5c. --- .../baeldung/beaninjectiontypes/Config.java | 16 -------- .../beaninjectiontypes/ExampleService.java | 11 ----- .../ExampleWithConstructorInjection.java | 19 --------- .../ExampleWithPropertyInjection.java | 16 -------- .../ExampleWithSetterInjection.java | 20 --------- .../beaninjectiontypes/BeanInjectionTest.java | 41 ------------------- 6 files changed, 123 deletions(-) delete mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java delete mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleService.java delete mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithConstructorInjection.java delete mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithPropertyInjection.java delete mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithSetterInjection.java delete mode 100644 spring-core/src/test/java/com/baeldung/beaninjectiontypes/BeanInjectionTest.java diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java deleted file mode 100644 index d523dc3f1f..0000000000 --- a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.beaninjectiontypes; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -@Configuration -@ComponentScan("com.baeldung.beaninjectiontypes") -public class Config { - - @Bean - public ExampleService exampleService() { - return new ExampleService(); - } - -} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleService.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleService.java deleted file mode 100644 index 9112fee577..0000000000 --- a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleService.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.beaninjectiontypes; - -import org.springframework.stereotype.Component; - -@Component -public class ExampleService { - - public String getExampleText() { - return "Example"; - } -} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithConstructorInjection.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithConstructorInjection.java deleted file mode 100644 index 340d283dbd..0000000000 --- a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithConstructorInjection.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.beaninjectiontypes; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class ExampleWithConstructorInjection { - - private ExampleService exampleService; - - @Autowired - public ExampleWithConstructorInjection(ExampleService exampleService) { - this.exampleService = exampleService; - } - - public String verifyInjection() { - return this.exampleService.getExampleText(); - } -} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithPropertyInjection.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithPropertyInjection.java deleted file mode 100644 index d9495e5c05..0000000000 --- a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithPropertyInjection.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.beaninjectiontypes; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class ExampleWithPropertyInjection { - - @Autowired - private ExampleService exampleService; - - public String verifyInjection() { - return this.exampleService.getExampleText(); - } - -} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithSetterInjection.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithSetterInjection.java deleted file mode 100644 index 97f1355ec5..0000000000 --- a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithSetterInjection.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.beaninjectiontypes; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class ExampleWithSetterInjection { - - private ExampleService exampleService; - - @Autowired - public void setExampleService(ExampleService exampleService) { - this.exampleService = exampleService; - } - - public String verifyInjection() { - return this.exampleService.getExampleText(); - } - -} diff --git a/spring-core/src/test/java/com/baeldung/beaninjectiontypes/BeanInjectionTest.java b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/BeanInjectionTest.java deleted file mode 100644 index ae609d5df5..0000000000 --- a/spring-core/src/test/java/com/baeldung/beaninjectiontypes/BeanInjectionTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.baeldung.beaninjectiontypes; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = Config.class) -public class BeanInjectionTest { - - @Test - public void whenConstructorInjection_ThenBeanValid() { - ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); - ExampleWithConstructorInjection exampleBean = context.getBean(ExampleWithConstructorInjection.class); - - assertEquals("Example", exampleBean.verifyInjection()); - } - - @Test - public void whenSetterInjection_ThenBeanValid() { - ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); - ExampleWithSetterInjection exampleBean = context.getBean(ExampleWithSetterInjection.class); - - assertEquals("Example", exampleBean.verifyInjection()); - } - - @Test - public void whenProperyInjection_ThenBeanValid() { - ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); - ExampleWithPropertyInjection exampleBean = context.getBean(ExampleWithPropertyInjection.class); - - assertEquals("Example", exampleBean.verifyInjection()); - } - -} From 33b93d18f64f83f1c46ecf07f1f351fa49f27b13 Mon Sep 17 00:00:00 2001 From: Chris Franklin Date: Sat, 20 Jan 2018 02:36:26 -0500 Subject: [PATCH 3/8] Christopher Franklin A Simple Tagging Implementation with Elasticsearch Modifying the existing Spring Data Elasticsearch example to use the tags already on the model. Also added a number of tests as examples of how to use the tags. --- .../data/es/repository/ArticleRepository.java | 9 +++++++- .../data/es/service/ArticleService.java | 7 +++++- .../data/es/service/ArticleServiceImpl.java | 15 +++++++++++-- .../data/es/ElasticSearchIntegrationTest.java | 22 ++++++++++++++++++- .../es/ElasticSearchQueryIntegrationTest.java | 12 ++++++++++ 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java index 8aef865401..93812a3cea 100644 --- a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java @@ -1,12 +1,13 @@ package com.baeldung.spring.data.es.repository; -import com.baeldung.spring.data.es.model.Article; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.annotations.Query; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; +import com.baeldung.spring.data.es.model.Article; + @Repository public interface ArticleRepository extends ElasticsearchRepository { @@ -14,4 +15,10 @@ public interface ArticleRepository extends ElasticsearchRepository findByAuthorsNameUsingCustomQuery(String name, Pageable pageable); + + @Query("{\"bool\": {\"must\": {\"match_all\": {}}, \"filter\": {\"term\": {\"tags\": \"?0\" }}}}") + Page
findByFilteredTagQuery(String tag, Pageable pageable); + + @Query("{\"bool\": {\"must\": {\"match\": {\"authors.name\": \"?0\"}}, \"filter\": {\"term\": {\"tags\": \"?1\" }}}}") + Page
findByAuthorsNameAndFilteredTagQuery(String name, String tag, Pageable pageable); } diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java index b5a8fde633..63e2d91fa7 100644 --- a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java @@ -1,9 +1,10 @@ package com.baeldung.spring.data.es.service; -import com.baeldung.spring.data.es.model.Article; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import com.baeldung.spring.data.es.model.Article; + public interface ArticleService { Article save(Article article); @@ -15,6 +16,10 @@ public interface ArticleService { Page
findByAuthorNameUsingCustomQuery(String name, Pageable pageable); + Page
findByFilteredTagQuery(String tag, Pageable pageable); + + Page
findByAuthorsNameAndFilteredTagQuery(String name, String tag, Pageable pageable); + long count(); void delete(Article article); diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java index 2a31b52602..0908ffa70e 100644 --- a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java @@ -1,12 +1,13 @@ package com.baeldung.spring.data.es.service; -import com.baeldung.spring.data.es.repository.ArticleRepository; -import com.baeldung.spring.data.es.model.Article; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import com.baeldung.spring.data.es.model.Article; +import com.baeldung.spring.data.es.repository.ArticleRepository; + @Service public class ArticleServiceImpl implements ArticleService { @@ -42,6 +43,16 @@ public class ArticleServiceImpl implements ArticleService { return articleRepository.findByAuthorsNameUsingCustomQuery(name, pageable); } + @Override + public Page
findByFilteredTagQuery(String tag, Pageable pageable) { + return articleRepository.findByFilteredTagQuery(tag, pageable); + } + + @Override + public Page
findByAuthorsNameAndFilteredTagQuery(String name, String tag, Pageable pageable) { + return articleRepository.findByAuthorsNameAndFilteredTagQuery(name, tag, pageable); + } + @Override public long count() { return articleRepository.count(); diff --git a/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchIntegrationTest.java b/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchIntegrationTest.java index 1280c8e1de..4a4cadb550 100644 --- a/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchIntegrationTest.java +++ b/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchIntegrationTest.java @@ -47,14 +47,22 @@ public class ElasticSearchIntegrationTest { Article article = new Article("Spring Data Elasticsearch"); article.setAuthors(asList(johnSmith, johnDoe)); + article.setTags("elasticsearch", "spring data"); articleService.save(article); article = new Article("Search engines"); article.setAuthors(asList(johnDoe)); + article.setTags("search engines", "tutorial"); articleService.save(article); article = new Article("Second Article About Elasticsearch"); article.setAuthors(asList(johnSmith)); + article.setTags("elasticsearch", "spring data"); + articleService.save(article); + + article = new Article("Elasticsearch Tutorial"); + article.setAuthors(asList(johnDoe)); + article.setTags("elasticsearch"); articleService.save(article); } @@ -79,10 +87,22 @@ public class ElasticSearchIntegrationTest { @Test public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() { - final Page
articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("John Smith", new PageRequest(0, 10)); + final Page
articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("Smith", new PageRequest(0, 10)); + assertEquals(2L, articleByAuthorName.getTotalElements()); + } + + @Test + public void givenTagFilterQuery_whenSearchByTag_thenArticleIsFound() { + final Page
articleByAuthorName = articleService.findByFilteredTagQuery("elasticsearch", new PageRequest(0, 10)); assertEquals(3L, articleByAuthorName.getTotalElements()); } + @Test + public void givenTagFilterQuery_whenSearchByAuthorsName_thenArticleIsFound() { + final Page
articleByAuthorName = articleService.findByAuthorsNameAndFilteredTagQuery("Doe", "elasticsearch", new PageRequest(0, 10)); + assertEquals(2L, articleByAuthorName.getTotalElements()); + } + @Test public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() { diff --git a/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryIntegrationTest.java b/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryIntegrationTest.java index cc4bce0c75..af69f74c1b 100644 --- a/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryIntegrationTest.java +++ b/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryIntegrationTest.java @@ -174,4 +174,16 @@ public class ElasticSearchQueryIntegrationTest { final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(2, articles.size()); } + + @Test + public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() { + final QueryBuilder builder = boolQuery().must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")))) + .filter(termQuery("tags", "elasticsearch")); + + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder) + .build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + + assertEquals(2, articles.size()); + } } From d0ff2aa57d12e8fac11c64a8e0974ff3c46e2453 Mon Sep 17 00:00:00 2001 From: Chris Franklin Date: Fri, 9 Feb 2018 14:37:58 -0500 Subject: [PATCH 4/8] Code samples for Simple Tagging with JPA --- .../persistence/dao/StudentRepository.java | 11 +++++ .../inmemory/persistence/model/Student.java | 13 ++++++ .../repository/InMemoryDBIntegrationTest.java | 45 +++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java index bfcf6f5cdc..f856b78c52 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java @@ -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 { + + @Query("SELECT s FROM Student s JOIN s.tags t WHERE t = LOWER(:tag)") + List 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 retrieveByNameFilterByTag(@Param("name") String name, @Param("tag") String tag); + } diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java index b50fe9122e..2e4e3ea2cb 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java @@ -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 tags = new ArrayList<>(); + public Student() { } @@ -35,4 +41,11 @@ public class Student { this.name = name; } + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags.addAll(tags); + } } diff --git a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java index 8380ab5434..28d7e3772c 100644 --- a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java +++ b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java @@ -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 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()); + } + } From 42ffe6cb7c3da4c13dec442477c26615c23f4b31 Mon Sep 17 00:00:00 2001 From: Chris Franklin Date: Mon, 12 Feb 2018 15:16:59 -0500 Subject: [PATCH 5/8] Fix pom relative path for spring-jpa --- persistence-modules/spring-jpa/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/persistence-modules/spring-jpa/pom.xml b/persistence-modules/spring-jpa/pom.xml index 04c64fafc3..bc0b2381f3 100644 --- a/persistence-modules/spring-jpa/pom.xml +++ b/persistence-modules/spring-jpa/pom.xml @@ -13,7 +13,7 @@ com.baeldung parent-modules 1.0.0-SNAPSHOT - ../ + ../../ From 64dd15eaf44bd859f2e2b1ae5834ec09696f4dab Mon Sep 17 00:00:00 2001 From: Chris Franklin Date: Fri, 23 Feb 2018 17:01:23 -0500 Subject: [PATCH 6/8] [BAEL-1575] Advanced tagging implementation with JPA --- .../dao/ManyStudentRepository.java | 10 ++++ .../persistence/dao/ManyTagRepository.java | 7 +++ .../persistence/dao/StudentRepository.java | 6 +++ .../inmemory/persistence/model/KVTag.java | 33 ++++++++++++ .../persistence/model/LocationTag.java | 39 ++++++++++++++ .../persistence/model/ManyStudent.java | 34 ++++++++++++ .../inmemory/persistence/model/ManyTag.java | 39 ++++++++++++++ .../inmemory/persistence/model/SkillTag.java | 29 ++++++++++ .../inmemory/persistence/model/Student.java | 29 ++++++++-- .../repository/InMemoryDBIntegrationTest.java | 54 ++++++++++++++++++- 10 files changed, 276 insertions(+), 4 deletions(-) create mode 100644 persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyStudentRepository.java create mode 100644 persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyTagRepository.java create mode 100644 persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/KVTag.java create mode 100644 persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/LocationTag.java create mode 100644 persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyStudent.java create mode 100644 persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyTag.java create mode 100644 persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/SkillTag.java diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyStudentRepository.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyStudentRepository.java new file mode 100644 index 0000000000..a03b2950a0 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyStudentRepository.java @@ -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 { + List findByManyTags_Name(String name); +} diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyTagRepository.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyTagRepository.java new file mode 100644 index 0000000000..b7d991de32 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/ManyTagRepository.java @@ -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 { +} diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java index f856b78c52..ffe1a68558 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java @@ -15,4 +15,10 @@ public interface StudentRepository extends JpaRepository { @Query("SELECT s FROM Student s JOIN s.tags t WHERE s.name = LOWER(:name) AND t = LOWER(:tag)") List 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 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 retrieveByKeyTag(@Param("key") String key); + } diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/KVTag.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/KVTag.java new file mode 100644 index 0000000000..ba0071e37b --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/KVTag.java @@ -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; + } +} diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/LocationTag.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/LocationTag.java new file mode 100644 index 0000000000..071dc24806 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/LocationTag.java @@ -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; + } +} diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyStudent.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyStudent.java new file mode 100644 index 0000000000..98778b8f75 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyStudent.java @@ -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 manyTags = new HashSet<>(); + + public ManyStudent() {} + + public ManyStudent(String name) { + this.name = name; + } + + public Set getManyTags() { + return manyTags; + } + + public void setManyTags(Set manyTags) { + this.manyTags.addAll(manyTags); + } +} diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyTag.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyTag.java new file mode 100644 index 0000000000..96f9534d43 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/ManyTag.java @@ -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 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 getStudents() { + return students; + } + + public void setStudents(Set students) { + this.students.addAll(students); + } +} diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/SkillTag.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/SkillTag.java new file mode 100644 index 0000000000..0300d83d50 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/SkillTag.java @@ -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; + } +} diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java index 2e4e3ea2cb..4751a999a0 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java @@ -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 tags = new ArrayList<>(); + @ElementCollection + private List skillTags = new ArrayList<>(); + + @ElementCollection + private List kvTags = new ArrayList<>(); + public Student() { } @@ -48,4 +54,21 @@ public class Student { public void setTags(List tags) { this.tags.addAll(tags); } + + public List getSkillTags() { + return skillTags; + } + + public void setSkillTags(List skillTags) { + this.skillTags.addAll(skillTags); + } + + public List getKVTags() { + return this.kvTags; + } + + public void setKVTags(List kvTags) { + this.kvTags.addAll(kvTags); + } + } diff --git a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java index 28d7e3772c..fa957cc9d0 100644 --- a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java +++ b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java @@ -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 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 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 students = manyStudentRepository.findByManyTags_Name("full time"); + assertEquals("size incorrect", 1, students.size()); + } + } From bb7f7a0588eb6b72d403683d9cd46bd851ea9018 Mon Sep 17 00:00:00 2001 From: Chris Franklin Date: Mon, 26 Feb 2018 10:40:25 -0500 Subject: [PATCH 7/8] [BAEL-1575] Move tests to separate suite --- .../AdvancedTaggingIntegrationTest.java | 81 +++++++++++++++++++ .../repository/InMemoryDBIntegrationTest.java | 50 ------------ 2 files changed, 81 insertions(+), 50 deletions(-) create mode 100644 persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/AdvancedTaggingIntegrationTest.java diff --git a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/AdvancedTaggingIntegrationTest.java b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/AdvancedTaggingIntegrationTest.java new file mode 100644 index 0000000000..2e4f1a0e23 --- /dev/null +++ b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/AdvancedTaggingIntegrationTest.java @@ -0,0 +1,81 @@ +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.KVTag; +import org.baeldung.inmemory.persistence.model.ManyStudent; +import org.baeldung.inmemory.persistence.model.ManyTag; +import org.baeldung.inmemory.persistence.model.SkillTag; +import org.baeldung.inmemory.persistence.model.Student; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; +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; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { StudentJpaConfig.class }, loader = AnnotationConfigContextLoader.class) +@Transactional +public class AdvancedTaggingIntegrationTest { + @Resource + private StudentRepository studentRepository; + + @Resource + private ManyStudentRepository manyStudentRepository; + + @Resource + private ManyTagRepository manyTagRepository; + + @Test + public void givenStudentWithSkillTags_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 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 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 students = manyStudentRepository.findByManyTags_Name("full time"); + assertEquals("size incorrect", 1, students.size()); + } +} diff --git a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java index fa957cc9d0..49067c8af4 100644 --- a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java +++ b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java @@ -1,8 +1,6 @@ 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.*; import org.junit.Test; @@ -15,7 +13,6 @@ 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; @@ -27,12 +24,6 @@ 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"; @@ -88,47 +79,6 @@ 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 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 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 students = manyStudentRepository.findByManyTags_Name("full time"); - assertEquals("size incorrect", 1, students.size()); - } } From 8e0cececdb3a3823c665f4363427c7217cc03cf8 Mon Sep 17 00:00:00 2001 From: Chris Franklin Date: Mon, 26 Feb 2018 10:42:39 -0500 Subject: [PATCH 8/8] [BAEL-1575] Cleanup original test --- .../persistence/repository/InMemoryDBIntegrationTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java index 49067c8af4..28d7e3772c 100644 --- a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java +++ b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java @@ -2,7 +2,7 @@ package org.baeldung.persistence.repository; import org.baeldung.config.StudentJpaConfig; import org.baeldung.inmemory.persistence.dao.StudentRepository; -import org.baeldung.inmemory.persistence.model.*; +import org.baeldung.inmemory.persistence.model.Student; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; @@ -79,6 +79,4 @@ public class InMemoryDBIntegrationTest { assertEquals("name incorrect", NAME, student2.getName()); } - - }