From 771a81d5bd3a409c00279e8acddcdf9535fde3a9 Mon Sep 17 00:00:00 2001 From: mujah Date: Sat, 29 Oct 2016 21:46:34 +0800 Subject: [PATCH 1/9] Spring Data Solr project created --- spring-data-solr/pom.xml | 78 ++++++++++++ .../spring/data/solr/config/SolrConfig.java | 25 ++++ .../spring/data/solr/model/Product.java | 62 ++++++++++ .../solr/repository/ProductRepository.java | 24 ++++ .../resources/solr-named-queries.properties | 1 + .../ProductRepositoryIntegrationTest.java | 117 ++++++++++++++++++ 6 files changed, 307 insertions(+) create mode 100644 spring-data-solr/pom.xml create mode 100644 spring-data-solr/src/main/java/com/baeldung/spring/data/solr/config/SolrConfig.java create mode 100644 spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java create mode 100644 spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java create mode 100644 spring-data-solr/src/main/resources/solr-named-queries.properties create mode 100644 spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java diff --git a/spring-data-solr/pom.xml b/spring-data-solr/pom.xml new file mode 100644 index 0000000000..9ec017e376 --- /dev/null +++ b/spring-data-solr/pom.xml @@ -0,0 +1,78 @@ + + 4.0.0 + + com.baeldung + spring-data-solr + 0.0.1-SNAPSHOT + jar + + spring-data-solr + + + + UTF-8 + 4.2.5.RELEASE + 2.19.1 + 2.0.4.RELEASE + + + + + + org.springframework + spring-core + ${spring.version} + + + + org.springframework.data + spring-data-solr + ${spring-data-solr} + + + + + org.springframework + spring-context + ${spring.version} + + + + + log4j + log4j + 1.2.16 + + + + + junit + junit + 4.12 + test + + + + org.springframework + spring-test + ${spring.version} + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + + + + + + diff --git a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/config/SolrConfig.java b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/config/SolrConfig.java new file mode 100644 index 0000000000..1fe1e5468b --- /dev/null +++ b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/config/SolrConfig.java @@ -0,0 +1,25 @@ +package com.baeldung.spring.data.solr.config; + +import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.solr.core.SolrTemplate; +import org.springframework.data.solr.repository.config.EnableSolrRepositories; + +@Configuration +@EnableSolrRepositories(basePackages = "com.baeldung.spring.data.solr.repository", namedQueriesLocation = "classpath:solr-named-queries.properties", multicoreSupport = true) +@ComponentScan +public class SolrConfig { + + @Bean + public SolrClient solrClient() { + return new HttpSolrClient("http://localhost:8983/solr"); + } + + @Bean + public SolrTemplate solrTemplate(SolrClient client) throws Exception { + return new SolrTemplate(client); + } +} diff --git a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java new file mode 100644 index 0000000000..2c16a2cdbf --- /dev/null +++ b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java @@ -0,0 +1,62 @@ +package com.baeldung.spring.data.solr.model; + +import org.apache.solr.client.solrj.beans.Field; +import org.springframework.data.annotation.Id; +import org.springframework.data.solr.core.mapping.SolrDocument; + +@SolrDocument(solrCoreName="product") +public class Product { + + @Id + @Field("id") + private String id; + + @Field("name") + private String name; + + @Field("category") + private String category; + + @Field("description") + private String description; + + + public Product(String id,String name,String category){ + this.id = id; + this.name = name; + this.category = category; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java new file mode 100644 index 0000000000..1f57816dd6 --- /dev/null +++ b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java @@ -0,0 +1,24 @@ +package com.baeldung.spring.data.solr.repository; + + +import java.util.List; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.solr.repository.Query; +import org.springframework.data.solr.repository.SolrCrudRepository; + +import com.baeldung.spring.data.solr.model.Product; + +public interface ProductRepository extends SolrCrudRepository{ + + public List findByName(String name); + + @Query("name:*?0* OR category:*?0* OR description:*?0*") + public Page findByCustomQuery(String searchTerm,Pageable pageable); + + + // @Query(name="Product.findByNamedQuery") + public Page findByNamedQuery(String searchTerm,Pageable pageable); + +} diff --git a/spring-data-solr/src/main/resources/solr-named-queries.properties b/spring-data-solr/src/main/resources/solr-named-queries.properties new file mode 100644 index 0000000000..cec59cbebd --- /dev/null +++ b/spring-data-solr/src/main/resources/solr-named-queries.properties @@ -0,0 +1 @@ +Product.findByNamedQuery=name:*?0* OR category:*?0* OR description:*?0* \ No newline at end of file diff --git a/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java b/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java new file mode 100644 index 0000000000..52a1a2837d --- /dev/null +++ b/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java @@ -0,0 +1,117 @@ +package com.baeldung.spring.data.solr.repo; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.spring.data.solr.config.SolrConfig; +import com.baeldung.spring.data.solr.model.Product; +import com.baeldung.spring.data.solr.repository.ProductRepository; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = SolrConfig.class) +public class ProductRepositoryIntegrationTest { + + @Autowired + private ProductRepository productRepository; + + @Before + public void clearSolrData(){ + productRepository.deleteAll(); + } + + @Test + public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception{ + final Product product = new Product("P00001","Desk","Furniture"); + product.setDescription("New Desk"); + productRepository.save(product); + final Product retrievedProduct = productRepository.findOne(product.getId()); + assertEquals(product.getId(),retrievedProduct.getId()); + } + + @Test + public void whenUpdatingProduct_thenChangeAvailableOnRetrieval() throws Exception { + final Product product = new Product("P0001", "T-Shirt","Kitchen"); + product.setDescription("New T-Shirt"); + productRepository.save(product); + + product.setCategory("Clothes"); + productRepository.save(product); + + final Product retrievedProduct = productRepository.findOne(product.getId()); + assertEquals(product.getCategory(), retrievedProduct.getCategory()); + } + + + + @Test + public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception { + final Product product = new Product("P0001", "Desk","Furniture"); + product.setDescription("New Desk"); + productRepository.save(product); + + productRepository.delete(product); + + Product retrievedProduct = productRepository.findOne(product.getId()); + assertNull(retrievedProduct); + + } + + @Test + public void whenFindByName_thenAvailableOnRetrieval() throws Exception{ + final Product phone = new Product("P0001", "Phone", "Electronics"); + phone.setDescription("New Phone"); + productRepository.save(phone); + + List retrievedProducts = productRepository.findByName("Phone"); + assertEquals(phone.getId(), retrievedProducts.get(0).getId()); + } + + @Test + public void whenSearchingProductsByQuery_thenAllMatchingProductsShouldAvialble() throws Exception { + final Product phone = new Product("P0001", "Smart Phone", "Electronics"); + phone.setDescription("New Item"); + productRepository.save(phone); + + final Product phoneCover = new Product("P0002", "Cover", "Phone"); + phoneCover.setDescription("New Product"); + productRepository.save(phoneCover); + + final Product wirelessCharger = new Product("P0003", "Charging Cable", "Cable"); + wirelessCharger.setDescription("Wireless Charger for Phone"); + productRepository.save(wirelessCharger); + + Page result = productRepository.findByCustomQuery("Pho", new PageRequest(0, 10)); + assertEquals(3, result.getNumberOfElements()); + } + + @Test + public void whenSearchingProductsByNamedQuery_thenAllMatchingProductsShouldAvialble() throws Exception { + final Product phone = new Product("P0001", "Smart Phone", "Electronics"); + phone.setDescription("New Item"); + productRepository.save(phone); + + final Product phoneCover = new Product("P0002", "Cover", "Phone"); + phoneCover.setDescription("New Product"); + productRepository.save(phoneCover); + + final Product wirelessCharger = new Product("P0003", "Charging Cable", "Cable"); + wirelessCharger.setDescription("Wireless Charger for Phone"); + productRepository.save(wirelessCharger); + + Page result = productRepository.findByNamedQuery("one", new PageRequest(0, 10)); + assertEquals(3, result.getNumberOfElements()); + } + +} From 74fccfb5247345dce41afe29cec3cbcb632a0061 Mon Sep 17 00:00:00 2001 From: mujah Date: Sat, 29 Oct 2016 21:51:11 +0800 Subject: [PATCH 2/9] pom.xml format changed --- spring-data-solr/pom.xml | 79 ++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/spring-data-solr/pom.xml b/spring-data-solr/pom.xml index 9ec017e376..437bc2b277 100644 --- a/spring-data-solr/pom.xml +++ b/spring-data-solr/pom.xml @@ -1,37 +1,36 @@ - 4.0.0 - - com.baeldung - spring-data-solr - 0.0.1-SNAPSHOT - jar - - spring-data-solr - - - - UTF-8 - 4.2.5.RELEASE - 2.19.1 - 2.0.4.RELEASE - - - - - - org.springframework - spring-core - ${spring.version} - - - - org.springframework.data - spring-data-solr - ${spring-data-solr} - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + 4.0.0 + com.baeldung + spring-data-solr + 0.0.1-SNAPSHOT + jar + spring-data-solr + + + + UTF-8 + 4.2.5.RELEASE + 2.19.1 + 2.0.4.RELEASE + + + + + + org.springframework + spring-core + ${spring.version} + + + + org.springframework.data + spring-data-solr + ${spring-data-solr} + + + org.springframework spring-context @@ -45,24 +44,25 @@ 1.2.16 - + junit junit 4.12 test - + org.springframework spring-test ${spring.version} test - - - - + + + + + org.apache.maven.plugins maven-surefire-plugin @@ -75,4 +75,5 @@ + From 5876c981aa16a764f9390048ef1eb5304f5c534c Mon Sep 17 00:00:00 2001 From: mujah Date: Sat, 29 Oct 2016 21:53:04 +0800 Subject: [PATCH 3/9] pom.xml format changed --- spring-data-solr/pom.xml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/spring-data-solr/pom.xml b/spring-data-solr/pom.xml index 437bc2b277..bd48a53d06 100644 --- a/spring-data-solr/pom.xml +++ b/spring-data-solr/pom.xml @@ -17,48 +17,38 @@ - org.springframework spring-core ${spring.version} - org.springframework.data spring-data-solr ${spring-data-solr} - - org.springframework spring-context ${spring.version} - - log4j log4j 1.2.16 - - junit junit 4.12 test - org.springframework spring-test ${spring.version} test - From d9eeb7c21a921a2a806fa0cd3f9fb23a956ef130 Mon Sep 17 00:00:00 2001 From: mujah Date: Sun, 30 Oct 2016 17:03:26 +0800 Subject: [PATCH 4/9] spring-data-solr module added --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 95182ab63c..182b2d5147 100644 --- a/pom.xml +++ b/pom.xml @@ -84,6 +84,7 @@ spring-data-mongodb spring-data-redis spring-data-rest + spring-data-solr spring-exceptions spring-freemarker From b3976c7fb59be85afb7ad1ff153b9b09346373e2 Mon Sep 17 00:00:00 2001 From: mujah Date: Sun, 30 Oct 2016 17:12:50 +0800 Subject: [PATCH 5/9] formatting format changes --- .../spring/data/solr/model/Product.java | 7 ++- .../solr/repository/ProductRepository.java | 18 ++++---- .../ProductRepositoryIntegrationTest.java | 45 +++++++++---------- 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java index 2c16a2cdbf..d08640123a 100644 --- a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java +++ b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java @@ -4,7 +4,7 @@ import org.apache.solr.client.solrj.beans.Field; import org.springframework.data.annotation.Id; import org.springframework.data.solr.core.mapping.SolrDocument; -@SolrDocument(solrCoreName="product") +@SolrDocument(solrCoreName = "product") public class Product { @Id @@ -20,13 +20,12 @@ public class Product { @Field("description") private String description; - - public Product(String id,String name,String category){ + public Product(String id, String name, String category) { this.id = id; this.name = name; this.category = category; } - + public String getId() { return id; } diff --git a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java index 1f57816dd6..01ec1fb909 100644 --- a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java +++ b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java @@ -1,6 +1,5 @@ package com.baeldung.spring.data.solr.repository; - import java.util.List; import org.springframework.data.domain.Page; @@ -10,15 +9,14 @@ import org.springframework.data.solr.repository.SolrCrudRepository; import com.baeldung.spring.data.solr.model.Product; -public interface ProductRepository extends SolrCrudRepository{ - +public interface ProductRepository extends SolrCrudRepository { + public List findByName(String name); - + @Query("name:*?0* OR category:*?0* OR description:*?0*") - public Page findByCustomQuery(String searchTerm,Pageable pageable); - - - // @Query(name="Product.findByNamedQuery") - public Page findByNamedQuery(String searchTerm,Pageable pageable); - + public Page findByCustomQuery(String searchTerm, Pageable pageable); + + @Query(name = "Product.findByNamedQuery") + public Page findByNamedQuery(String searchTerm, Pageable pageable); + } diff --git a/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java b/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java index 52a1a2837d..b55e65e04a 100644 --- a/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java +++ b/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java @@ -18,59 +18,56 @@ import com.baeldung.spring.data.solr.config.SolrConfig; import com.baeldung.spring.data.solr.model.Product; import com.baeldung.spring.data.solr.repository.ProductRepository; - @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SolrConfig.class) public class ProductRepositoryIntegrationTest { - + @Autowired private ProductRepository productRepository; - + @Before - public void clearSolrData(){ + public void clearSolrData() { productRepository.deleteAll(); } - + @Test - public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception{ - final Product product = new Product("P00001","Desk","Furniture"); + public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception { + final Product product = new Product("P00001", "Desk", "Furniture"); product.setDescription("New Desk"); productRepository.save(product); final Product retrievedProduct = productRepository.findOne(product.getId()); - assertEquals(product.getId(),retrievedProduct.getId()); + assertEquals(product.getId(), retrievedProduct.getId()); } - + @Test public void whenUpdatingProduct_thenChangeAvailableOnRetrieval() throws Exception { - final Product product = new Product("P0001", "T-Shirt","Kitchen"); + final Product product = new Product("P0001", "T-Shirt", "Kitchen"); product.setDescription("New T-Shirt"); productRepository.save(product); - + product.setCategory("Clothes"); productRepository.save(product); - + final Product retrievedProduct = productRepository.findOne(product.getId()); assertEquals(product.getCategory(), retrievedProduct.getCategory()); } - - - + @Test public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception { - final Product product = new Product("P0001", "Desk","Furniture"); + final Product product = new Product("P0001", "Desk", "Furniture"); product.setDescription("New Desk"); productRepository.save(product); - + productRepository.delete(product); - - Product retrievedProduct = productRepository.findOne(product.getId()); + + Product retrievedProduct = productRepository.findOne(product.getId()); assertNull(retrievedProduct); - + } - + @Test - public void whenFindByName_thenAvailableOnRetrieval() throws Exception{ - final Product phone = new Product("P0001", "Phone", "Electronics"); + public void whenFindByName_thenAvailableOnRetrieval() throws Exception { + Product phone = new Product("P0001", "Phone", "Electronics"); phone.setDescription("New Phone"); productRepository.save(phone); @@ -92,7 +89,7 @@ public class ProductRepositoryIntegrationTest { wirelessCharger.setDescription("Wireless Charger for Phone"); productRepository.save(wirelessCharger); - Page result = productRepository.findByCustomQuery("Pho", new PageRequest(0, 10)); + Page result = productRepository.findByCustomQuery("Phone", new PageRequest(0, 10)); assertEquals(3, result.getNumberOfElements()); } From c50338720f553c29fc5ceaae248474bb9e991969 Mon Sep 17 00:00:00 2001 From: mujah Date: Wed, 9 Nov 2016 18:28:01 +0800 Subject: [PATCH 6/9] test cases and model fixes --- .../spring/data/solr/model/Product.java | 22 +++-- .../ProductRepositoryIntegrationTest.java | 92 ++++++++++++------- 2 files changed, 73 insertions(+), 41 deletions(-) diff --git a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java index d08640123a..54962b7e51 100644 --- a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java +++ b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java @@ -1,31 +1,33 @@ package com.baeldung.spring.data.solr.model; -import org.apache.solr.client.solrj.beans.Field; import org.springframework.data.annotation.Id; +import org.springframework.data.solr.core.mapping.Indexed; import org.springframework.data.solr.core.mapping.SolrDocument; @SolrDocument(solrCoreName = "product") public class Product { @Id - @Field("id") + @Indexed(name="id",type = "string") private String id; - @Field("name") + + @Indexed(name="name",type = "string") private String name; - @Field("category") + + @Indexed(name="category",type = "string") private String category; - @Field("description") + + @Indexed(name="description",type = "string") private String description; - - public Product(String id, String name, String category) { - this.id = id; - this.name = name; - this.category = category; + + public Product(){ + } + public String getId() { return id; } diff --git a/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java b/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java index b55e65e04a..74d94ef91c 100644 --- a/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java +++ b/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java @@ -21,94 +21,124 @@ import com.baeldung.spring.data.solr.repository.ProductRepository; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SolrConfig.class) public class ProductRepositoryIntegrationTest { - + @Autowired private ProductRepository productRepository; - + @Before public void clearSolrData() { productRepository.deleteAll(); } - + @Test public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception { - final Product product = new Product("P00001", "Desk", "Furniture"); + final Product product = new Product(); + product.setId("P000089998"); + product.setName("Desk"); + product.setCategory("Furniture"); product.setDescription("New Desk"); productRepository.save(product); final Product retrievedProduct = productRepository.findOne(product.getId()); assertEquals(product.getId(), retrievedProduct.getId()); } - + @Test public void whenUpdatingProduct_thenChangeAvailableOnRetrieval() throws Exception { - final Product product = new Product("P0001", "T-Shirt", "Kitchen"); + final Product product = new Product(); + product.setId("P0001"); + product.setName("T-Shirt"); + product.setCategory("Kitchen"); product.setDescription("New T-Shirt"); productRepository.save(product); - + product.setCategory("Clothes"); productRepository.save(product); - + final Product retrievedProduct = productRepository.findOne(product.getId()); assertEquals(product.getCategory(), retrievedProduct.getCategory()); } - + @Test public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception { - final Product product = new Product("P0001", "Desk", "Furniture"); + final Product product = new Product(); + product.setId("P0001"); + product.setName("Desk"); + product.setCategory("Furniture"); product.setDescription("New Desk"); productRepository.save(product); - + productRepository.delete(product); - + Product retrievedProduct = productRepository.findOne(product.getId()); assertNull(retrievedProduct); - + } - + @Test public void whenFindByName_thenAvailableOnRetrieval() throws Exception { - Product phone = new Product("P0001", "Phone", "Electronics"); + Product phone = new Product(); + phone.setId("P0001"); + phone.setName("Phone"); + phone.setCategory("Electronics"); phone.setDescription("New Phone"); productRepository.save(phone); - + List retrievedProducts = productRepository.findByName("Phone"); assertEquals(phone.getId(), retrievedProducts.get(0).getId()); } - + @Test public void whenSearchingProductsByQuery_thenAllMatchingProductsShouldAvialble() throws Exception { - final Product phone = new Product("P0001", "Smart Phone", "Electronics"); + final Product phone = new Product(); + phone.setId("P0001"); + phone.setName("Smart Phone"); + phone.setCategory("Electronics"); phone.setDescription("New Item"); productRepository.save(phone); - - final Product phoneCover = new Product("P0002", "Cover", "Phone"); + + final Product phoneCover = new Product(); + phoneCover.setId("P0002"); + phoneCover.setName("Cover"); + phoneCover.setCategory("Phone"); phoneCover.setDescription("New Product"); productRepository.save(phoneCover); - - final Product wirelessCharger = new Product("P0003", "Charging Cable", "Cable"); + + final Product wirelessCharger = new Product(); + wirelessCharger.setId("P0003"); + wirelessCharger.setName("Charging Cable"); + wirelessCharger.setCategory("Cable"); wirelessCharger.setDescription("Wireless Charger for Phone"); productRepository.save(wirelessCharger); - + Page result = productRepository.findByCustomQuery("Phone", new PageRequest(0, 10)); assertEquals(3, result.getNumberOfElements()); } - + @Test public void whenSearchingProductsByNamedQuery_thenAllMatchingProductsShouldAvialble() throws Exception { - final Product phone = new Product("P0001", "Smart Phone", "Electronics"); + final Product phone = new Product(); + phone.setId("P0001"); + phone.setName("Smart Phone"); + phone.setCategory("Electronics"); phone.setDescription("New Item"); productRepository.save(phone); - - final Product phoneCover = new Product("P0002", "Cover", "Phone"); + + final Product phoneCover = new Product(); + phoneCover.setId("P0002"); + phoneCover.setName("Cover"); + phoneCover.setCategory("Phone"); phoneCover.setDescription("New Product"); productRepository.save(phoneCover); - - final Product wirelessCharger = new Product("P0003", "Charging Cable", "Cable"); + + final Product wirelessCharger = new Product(); + wirelessCharger.setId("P0003"); + wirelessCharger.setName("Charging Cable"); + wirelessCharger.setCategory("Cable"); wirelessCharger.setDescription("Wireless Charger for Phone"); productRepository.save(wirelessCharger); - + Page result = productRepository.findByNamedQuery("one", new PageRequest(0, 10)); assertEquals(3, result.getNumberOfElements()); } - + } From 2ab1eeb036e0d5344eef5751c2602927a2040c93 Mon Sep 17 00:00:00 2001 From: mujah Date: Wed, 9 Nov 2016 18:36:32 +0800 Subject: [PATCH 7/9] refactoring model --- .../spring/data/solr/model/Product.java | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java index 54962b7e51..7cd0890718 100644 --- a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java +++ b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java @@ -6,58 +6,50 @@ import org.springframework.data.solr.core.mapping.SolrDocument; @SolrDocument(solrCoreName = "product") public class Product { - + @Id - @Indexed(name="id",type = "string") + @Indexed(name = "id", type = "string") private String id; - - @Indexed(name="name",type = "string") + @Indexed(name = "name", type = "string") private String name; - - @Indexed(name="category",type = "string") + @Indexed(name = "category", type = "string") private String category; - - @Indexed(name="description",type = "string") + @Indexed(name = "description", type = "string") private String description; - public Product(){ - - } - - public String getId() { return id; } - + public void setId(String id) { this.id = id; } - + public String getName() { return name; } - + public void setName(String name) { this.name = name; } - + public String getCategory() { return category; } - + public void setCategory(String category) { this.category = category; } - + public String getDescription() { return description; } - + public void setDescription(String description) { this.description = description; } - + } From f966f277c026dd53c32aa96e738b82cdb30330aa Mon Sep 17 00:00:00 2001 From: mujah Date: Wed, 9 Nov 2016 22:10:30 +0800 Subject: [PATCH 8/9] removed solr --- pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/pom.xml b/pom.xml index 182b2d5147..95182ab63c 100644 --- a/pom.xml +++ b/pom.xml @@ -84,7 +84,6 @@ spring-data-mongodb spring-data-redis spring-data-rest - spring-data-solr spring-exceptions spring-freemarker From 1f0e30b81ca7f87c7f0674ba5bdb83bdff0ba982 Mon Sep 17 00:00:00 2001 From: mujah Date: Wed, 9 Nov 2016 22:26:18 +0800 Subject: [PATCH 9/9] pom changes + merge upstream --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 857922310f..bd626b3ad0 100644 --- a/pom.xml +++ b/pom.xml @@ -95,6 +95,7 @@ spring-data-neo4j spring-data-redis spring-data-rest + spring-data-solr spring-dispatcher-servlet spring-exceptions spring-freemarker