From 8c4585bc463bb6267852ae47c9ad2d195c2443ab Mon Sep 17 00:00:00 2001 From: Neeraj Yadav Date: Sat, 18 Aug 2018 14:28:03 +0530 Subject: [PATCH] Bael 1873 - Spring Data JPA Composable repository (#4953) * Merging into own fork (#3) * [BAEL-7621] - Fixed integration test of spring-hibernate-5 module by introducing H2 database * BAEL-1985: Added Examples showing how to Initialize HashSet when it's constructed (#4715) * Added Class for Initalizing HahsSet * Updated Class name * Delete InitializingSetTest.java * Copy list to another list examples (#4725) * Update README.MD (#4720) * [BAEL-7621] - Fixed integration test of spring-hibernate-5 module by introducing H2 database (#4728) * [BAEL-7645] - Fixed integration test of spring-jpa module through H2 inmemory DB * BAEL-1814 Guide to Spring Webflux (#4450) * BAEL-1814 Guide to Spring Webflux -Added files for Employee reactive application -Updated pom.xml for Spring Security * BAEL-1814 Guide to Spring Webflux -Added EmployeeControllerTest -Updated method name in EmployeeController and corrected secured url in EmployeeWebSecurityConfig * BAEL-1814 Guide to spring webflux -Fixed security config, now only specific url prompts for authentication and not all endpoints -Removed @WithMockUser as it is not needed now * BAEL-1814 Guide To Webflux -Feedback incorporation * BAEL-1814 Spring Webflux Guide -Formatted coded for EmployeeWebSocketHandler. * Update and rename EmployeeControllerTest.java to EmployeeControllerUnitTest.java * BAEL-1814 Guide to spring webflux -Fixed EmployeeControllerUnitTest.java * BAEL - 1916 (#4729) Code refactored * Renamed test methods to use BDD style * Fixed integration test of spring-jpa module through inmemory H2 DB (#4740) * [BAEL-7621] - Fixed integration test of spring-hibernate-5 module by introducing H2 database * [BAEL-7645] - Fixed integration test of spring-jpa module through H2 inmemory DB * guide to jmapper * [BAEL-7651] - Fixed integration tests of spring-security-mvc-custom module by adding proper authentication manager * clean only generated files * added spring-rest-hal-browser code (#4701) * Added spring-rest-template * Updated README.md file * Updated README.md file * Update Makefile * Update Makefile * moved AuthenticationFailureHandler example to spring-security-mvc-login * trying out separate modules in the integration profile * maven cleanup * fixing name of module * running group 2 * running group 1 * fixing relative path * Update Makefile * Update Makefile * Update JMapperIntegrationTest.java * Update JMapperRelationalIntegrationTest.java * Update MultipartFileUploadClient.java * BAEL-1914 refactor (#4749) * Server Sent Events example using Spring Webflux and React * spring security custom AuthenticationFailureHandler * refactor * moved SSE to branch * remove pom properties * moved AuthenticationFailureHandler example to spring-security-mvc-login * added link * trying out profile-driven build * minor maven cleanup * activating group 2 * Update README.md * BAEL-1850 (#4744) * Micronaut server * More server stuff; create client and test * Rename directory, new concerete client example * Remove hello-world directory from micronaut * Update MavenWrapperDownloader.java * running group 1, and small logging fix * jnosql * live test properly categorized * temporarily making a test live * moving the libraries module from group 1 * group 2 * enabling group 3 * * Added changes for BAEL-1922 Enable CORS in Spring Webflux (#4724) * BAEL-2018 (#4753) * BAEL-2018 * Update Animal.java * rename * running group 3 * jmeter excluded * running group 2 * properly classifying a testclear * live tests * BAEL-1838 (#4692) * #BAEL-1838 code samples. Renamed LambdaKotlinTest to have the build succeed. * #BAEL-1838 code samples w/inheritance. * #BAEL-1838 renamed logger helper function to getLogger to avoid confusion. * #BAEL-1838 renamed logger helper function to getLogger to avoid confusion. * BEAL-1985 - Removed Java 9 Example (#4734) * Added Class for Initalizing HahsSet * Updated Class name * Delete InitializingSetTest.java * Modified HashSet Initilization Example * Removed Java 9 Example * Update HashSetInitalizingUnitTest.java * Update HashSetInitalizingUnitTest.java * Update HashSetInitalizingUnitTest.java * Update HashSetInitalizingUnitTest.java * group 3.2 * enabling 3.1 * running group 3 * Update README.md Documenting the new default profile. * fixing the default profile testing config * groups 2 and 3 * minor major cleanup * BAEL-1907 Created new module spring-testing * new integration-lite profile * integration-lite work * BAEL-1862 Move the Junit 5 logic in the right module (#4747) * BAEL-1862 Move the Junit 5 logic in the right module -Moved Method Orders Tests from tutorails/junit5 project into correct project tutorials/testing-modules/junit-5 -Removed tutorials/junit5 project * Update DefaultOrderOfExecutionTest.java * Update README.md * BAEL-1862 Move the Junit 5 logic in the right module -Renamed *Test to *UnitTest * Update README.md * maven cleanup work * logging cleanup * BAEL-1907 Corrected formatting * working through modules * maven cleanup * trying problematic modules * integration heavy profile * maven work * Bael 1864 (#4727) * running project without building tests * include the DataCheck class * Update TestFail.java * guide to jmapper (#4745) * guide to jmapper * Update JMapperIntegrationTest.java * Update JMapperRelationalIntegrationTest.java * dupirefr/dupire.francois+pro@gmail.com [BAEL-1981] Query entities by dates and times with Spring Data JPA (#4737) * [BAEL-1981] Article entity and repository + tests * [BAEL-1981] Removing unnecessary fields * moving long-running module * BAEL-1992 * maven cleanup work * BAEL-1818 - A Guide to Connection Pools in Java (#4735) * Strange git issue with README.MD, wouldn't revert the file * Initial Commit * Initial Commit * Update pom.xml * Update pom.xml * Initial Commit * Update pom.xml * Update source files * Update source files * Update source files * Update source files * Update Application.java * Update pom.xml * Update HikariCPDataSourceUnitTest class * Update HikariCPDataSourceUnitTest.java * Update pom.xml * Update unit test classes * Update BasicConnectionPoolUnitTest.java * Fix indentation in DBCDDataSource class * Update DBCPDataSource.java * Update BasicConnectionPool class * Update BasicConnectionPool class * Update BasicConnectionPool.java * Update BasicConnectionPool.java * Update pom.xml * Update pom.xml * BAEL-1818 Refactored getConnection(), added shutdown(), cleaned up pom.xml * BAEL-1818 Removed getConnectionPool(), upgraded c3po version * BAEL-1818 Deleted obsolete connectionpool module * BAEL-1818 Deleted obsolete connectionpool module * move jmapper to libraries-data * [BAEL-7635] Removed test generated files : Will be gitignored * [BAEL-7635] - Commented out sortpom-maven-plugin that changes pom.xml in every build, added new entries in .gitignore * BAEL-1852 - Testing an Abstract Class with JUnit (#4773) * BAEL-1852 - Testing an Abstract Class with JUnit * Fixed test method names and class names according to naming compliances. * BAEL-1934 (#4768) * Bean Object, server side and client side example for event streaming example * BAEL-1628 Access a File from the Classpath in a Spring Application * inputstream retrieval added * Removed files related to evaluation article * + Aligning code to the article. Removed Utility methods and classes * BAEL - 1628 * PMD fixes * Code Review changes Refactored : whenResourceUtils_thenReadSuccessful * BAEL-1934 * +indentation correction in pom.xml * synced with master * indentation correction * update to spring 5 * Bael 2018 (#4774) * BAEL-2018 * Update Animal.java * rename * tests added * generic type shorten * update test * BAEL-1911 - Fixing author's review comments (#4782) * Strange git issue with README.MD, wouldn't revert the file * Fixing review comments * BAEL-1911 Refactored SQL code, fixed formatting * fix swagger parent * Update pom.xml * Update pom.xml * Overriding System time for testing (#4779) * Overriding System time for testing * Remove Joda Date Time examples * BAEL-1728: add java instrumentation * dupirefr/dupire.francois+pro@gmail.com [BAEL-1981] Spring data jpa dates (#4795) * [BAEL-1981] Article entity and repository + tests * [BAEL-1981] Removing unnecessary fields * [BAEL-1981] spring-data-jpa module creation * BAEL-1818 lamdba instead of loop; isEmpty() instead of == 0 (#4791) * BAEL-2018 Moved to core-java-collections (#4796) * BAEL-1691: comparing embedded servlet containers in spring boot * vaadin spring * format * remove reactive ex * move to reactive, extract mongodb ex * PR for http://jira.baeldung.com/browse/BAEL-1947 Spring Boot Vue (#4687) * commit first as binodpanta * revert test change * A short example of real-time event streaming using Spring WebFlux * Code for http://jira.baeldung.com/browse/BAEL-1527 * remove unrelated files * Apply feedback changes to rename test and remove link from readme file, ongoing work * Update formatting fixes to code and add pom changes, that partially fix test runnning issues in IDE but not in cmdline * Apply Eclipse formatter to test code and apply suggested pom fixes * BAEL-1527 Formatting fix in pom.xml * Use string.format to cleanup logging code * BAEL-1527 Changed logging pattern * Start the spring-boot-vue module, WIP * some small updates with comments * Add index html template page * merge pom.xml fixes * Add integration test with MockMvc to verify index.html content is rendered correctly * fix up pom merge issues * merge issues fix for pom * pom end of file newline * Update README.md * Update README.md (#4706) * add links (#4804) * Update README.md * Update README.md * Update README.md * Update README.md * Create README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.MD * Create README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * move mongodb ex * fix readme files * Bael 1832 (#4748) * @Primary annotation * @Primary annotation Employee name * Update PrimaryApplication.java * @Primary annotation with @Component * add security exc * added readme * added link * BAEL-2030 remove first element from list (#4803) * BAEL-2042 JavaFaker unit tests * Moved javafaker unit tests to testing-modules * Update PushController.java * * added examples of filtering collections using Streams, Apache CollectionUtils, Guava and Eclipse Collections * * Added examples for java-9 filtering collector * * minor fixes and cleaning duties * add elements to list (#4814) * BAEL-1960: Custom appender for log4j (#4731) * BAEL-1960: Custom appender for log4j * Changes as per suggestion to BAEL-1960 * Changes as [er review for BAEL-1960 * Changes for formatting as per suggestion. * BAEL-1960. Copied pom.xml from master and pasted my changes against it. * Chnages for spaces instead of tabs. * Changes for spaces instead of tabs. * PrincipalExtractor and AuthoritiesExtractor example * Bael 1743 improved (#4826) * compile only for firefox * added parent module * changed artifact id * commenting out problematic modules in the integration-lite build * integratio-lite profile work * integration-lite work * update spring data elasticsearch * BAEL-1818 A Simple Guide to Connection Pooling in Java (#4823) * Initial Commit * Update parent pom.xml * Update BasicConnectionPool class * Update BasicConnectionPool class * BAEL-1818 removed code from core-java module, cleaned up a little pom files * BAEL-1818 moved the code from connectionpool.connectionpools package to connectionpool * added link * integration-lite trying out a few modules * moved PrincipalExtractor and AuthoritiesExtractor example to spring-5-security module * removed comment on pom * [refs#BAEL-1992] Minor refactoring * BAEL-1983 Intialize a HashMap in Java (#4819) * move mqtt project * added link * Add items to list in core-java-collections (#4841) * [BAEL-7608] - Fixed spring-5-reactive integration tests * [BAEL-7608] - Reverted NettyContext to Embedded Tomcat example with Async = true * [BAEL-7608] - Removed unused imports * Update pom.xml * [BAEL-7609] - Fixed spring-boot integration tests * Added PR files for BAEL-2031 (#4844) * Added source files for BAEL-2031 * Added test files for BAEL-2031 * upgrade sockets to spring5 * BAEL-1979 Added examples for SnakeYAML Library (#4802) * BAEL-1979 Added examples for SnakeYAML Library * BAEL-1979 Moved the snakeyaml related code to libraries module * BAEL-1979 Removed the System.out.println() statements and converted the assertTrue to assertEquals wherever possible. * BAEL-1979 Removed println statements, small formatting fix in pom.xml * [BAEL-7608] - Fixed SecurityIntegrationTest with redirecting to login * Bael 1555 - Improve Example (#4852) * BAEL-1555 * Corrected indents and spacing * RequestMapping to GetMapping * Improved Performance For Concurrent Users * BAEL-1958 Log using SLF4J (#4790) * Log using SLF4J Jira Ticket: BAEL-1958 * Incorporate first review comments * Bael 2023 (#4851) * bael-2023: removing all occurrences of a value from a list * adjusting examples to match the article * [BAEL-7437] - Added spring tx dependency to fix spring-mvc-simple junit 5 TCs * add libraries server project * remove extra files * BAEL-1865 - Java Objects Sizes (#4584) * BAEL-1865 - Java Objects Sizes * BAEL-1865 - PR fix * OAuth2 Principal and Authorities example - refactor and added example using custom authorization server * Server-Sent Evensts * BAEL-1936 Use of FilenameFilter (#4520) * Added tests for FilenameFilter demo -added a test to show FilenameFilter implementation -added another test to show similar functionality using Predicate * refactored code to get directory at a single location * fixing formatting * changed test class name to conform to custom rule UnitTestNamingConventionRule lists the allowed test class names. Added ManualTest at the end to conform to the rule. * add new module * Update pom.xml (#4843) * BAEL-1861 - Running JUnit tests from a Java application (#4526) * BAEL-1562 - Thymeleaf sample working * BAEL-1562 Code added for Fragments sample * BAEL-1562 - Last correction for the test * BAEL-1562 - Thymeleaf sample working * BAEL-1562 Code added for Fragments sample * BAEL-1562 - Last correction for the test * Updates Thymeleaf version to 3.0.9.RELEASE * Added msf4j projects * updated msf4j project folder * fixed issue with spring-thymeleaf/pom.xml * Removed depedency-reduced-pom.xml * Whitespacing fix * Strange git issue with README.MD, wouldn't revert the file * Added jupiter api * Corrected junit test * Added test engine to plugin * Removed extra tag * Little fixes to junit4 and junit4 run from java * Removed scope from pom.xml * Removed bin file from testing * Slight changes for PMD * Slight changes for PMD * ok, moved code to another folder * Renamed and fixed runjunitfromjava * moved test classes to test folder * moved main to src/java * BAEL-1861 Moved test running classes to src/test/java * Added changes to runjunitfromjava * Added changes to runjunitfromjava * BAEL-1861 Changed test execution code examples * BAEL-1861 Changed test execution code examples; formatting * Bael 1852 - Test case code is aligned to support Junit5 (#4847) * add prototype bean ex with function * remove extra classes * remove extra import * separate configs * separate configs * Update AppConfig.java * Code update to support Junit5 * BAEL-1979 Added examples for SnakeYAML Library (#4802) * BAEL-1979 Added examples for SnakeYAML Library * BAEL-1979 Moved the snakeyaml related code to libraries module * BAEL-1979 Removed the System.out.println() statements and converted the assertTrue to assertEquals wherever possible. * BAEL-1979 Removed println statements, small formatting fix in pom.xml * BAEL-1852 Renamed one test method, fixed formatting * Bael 1273 Spring RSS Feed View (#4707) * Added example for BAEL-1273 - rss feed with Spring. * Fixed javadoc * Removed useless SpringBootServletInitializer in RSS app's launcher * Explicitely added Spring Boot starting class in pom.xml to prevent errors in package phase. * Adding files for Exception Handling article (#4507) * Adding files for Exception Handling article * Updating files * Test folder * testing renaming * Formatting and Naming Conventions This commit reworks the code for the Intro to Exception Handling article, ensuring that packages and classes are formatted and named according to site standards. * Update SseEmitterController.java (#4864) * fixing package hierarchy (*.list.list.listoflist -> *.list.listoflist) (#4879) * moved examples for 'removing all occurrences of an element from a list' to core-java-collections (#4878) * BAEL-1958 Moved the example to logging-modules (#4886) * Example for removing first element of array (BAEL-2029) (#4836) * Example for removing first element of array (BAEL-2029) * Use AssertJ assertions * BAEL-1986 List initialization in one line (#4696) * list initializations in one line * Enhance after review * formatting and naming * Formatting and renaming 2 * Unit tests and DequeBasedSynchronizedStack added up * Unit tests method names and class names modified as per the guidelines * BAEL-1840 Builder Pattern in Kotlin (#4730) * builder pattern in kotlin * builder pattern in kotlin new-line * deleted Sandbox, added unit test * add other tests * named and default parameters builder * Make FoodOrderNamed a data class * BAEL-1861 Replaced real tests with demo test "placeholders" (#4887) * Spring Boot and Angular E-Commerce Application (#4874) * Spring Boot and Angular E-Commerce Application * Spring Boot and Angular E-Commerce Application pom.xml updated * Spring Boot and Angular E-Commerce Application tests added * BAEL-7965 JMH module fails when build (#4888) * BAEL-7965 JMH module fails when build -Added jmh module in default profile in parent pom * Update pom.xml moved jmh module to integration profile * Create pom.xml * Create FunctionTestSuite.java BAEL-1857 * BAEL-1857 Running Parallel JUnit Tests with Maven * BAEL-1901 and BAEL-1555 add links (#4892) * BAEL-1766: Update README * BAEL-1853: add link to article * BAEL-1801: add link to article * Added links back to articles * Add links back to articles * BAEL-1795: Update README * BAEL-1901 and BAEL-1555 add links back to article * [BAEL-1967] - Custom validation MessageSource in Spring Boot * POM file updated for BAEL-1967 * BAEL-1704: Non-Trivial Work in Kotlin vs Java (#4861) * BAEL-1704: Non-Trivial Work in Kotlin vs Java * BAEL-1704: Non-Trivial Work in Kotlin vs Java Renaming one test class * Bael 1964 (#4881) * Added initial code for BAEL-1964, in-memory authentication application * Switched to default security encoder instead of a specific one * Fix typo (#4897) * add new module * fix typo * [BAEL-7670] Added logback.xml in missing modules in src/main/resources * Spring Data JPA Composable repository --- .../main/java/com/baeldung/domain/Item.java | 81 ++++++++++++++++ .../java/com/baeldung/domain/ItemType.java | 46 +++++++++ .../java/com/baeldung/domain/Location.java | 55 +++++++++++ .../main/java/com/baeldung/domain/Store.java | 76 +++++++++++++++ .../repository/CustomItemRepository.java | 15 +++ .../repository/CustomItemTypeRepository.java | 13 +++ .../repository/ItemTypeRepository.java | 10 ++ .../repository/LocationRepository.java | 10 ++ .../ReadOnlyLocationRepository.java | 15 +++ .../baeldung/repository/StoreRepository.java | 13 +++ .../impl/CustomItemRepositoryImpl.java | 32 +++++++ .../impl/CustomItemTypeRepositoryImpl.java | 31 +++++++ .../JpaRepositoriesIntegrationTest.java | 93 +++++++++++++++++++ .../src/test/resources/application.properties | 2 +- .../src/test/resources/import_articles.sql | 3 - .../src/test/resources/import_entities.sql | 21 +++++ 16 files changed, 512 insertions(+), 4 deletions(-) create mode 100644 spring-data-jpa/src/main/java/com/baeldung/domain/Item.java create mode 100644 spring-data-jpa/src/main/java/com/baeldung/domain/ItemType.java create mode 100644 spring-data-jpa/src/main/java/com/baeldung/domain/Location.java create mode 100644 spring-data-jpa/src/main/java/com/baeldung/domain/Store.java create mode 100644 spring-data-jpa/src/main/java/com/baeldung/repository/CustomItemRepository.java create mode 100644 spring-data-jpa/src/main/java/com/baeldung/repository/CustomItemTypeRepository.java create mode 100644 spring-data-jpa/src/main/java/com/baeldung/repository/ItemTypeRepository.java create mode 100644 spring-data-jpa/src/main/java/com/baeldung/repository/LocationRepository.java create mode 100644 spring-data-jpa/src/main/java/com/baeldung/repository/ReadOnlyLocationRepository.java create mode 100644 spring-data-jpa/src/main/java/com/baeldung/repository/StoreRepository.java create mode 100644 spring-data-jpa/src/main/java/com/baeldung/repository/impl/CustomItemRepositoryImpl.java create mode 100644 spring-data-jpa/src/main/java/com/baeldung/repository/impl/CustomItemTypeRepositoryImpl.java create mode 100644 spring-data-jpa/src/test/java/com/baeldung/repository/JpaRepositoriesIntegrationTest.java delete mode 100644 spring-data-jpa/src/test/resources/import_articles.sql create mode 100644 spring-data-jpa/src/test/resources/import_entities.sql diff --git a/spring-data-jpa/src/main/java/com/baeldung/domain/Item.java b/spring-data-jpa/src/main/java/com/baeldung/domain/Item.java new file mode 100644 index 0000000000..97ce14d92d --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/domain/Item.java @@ -0,0 +1,81 @@ +package com.baeldung.domain; + +import java.math.BigDecimal; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +@Entity +public class Item { + + private String color; + private String grade; + + @Id + private Long id; + + @ManyToOne + private ItemType itemType; + + private String name; + private BigDecimal price; + @ManyToOne + private Store store; + + public String getColor() { + return color; + } + + public String getGrade() { + return grade; + } + + public Long getId() { + return id; + } + + public ItemType getItemType() { + return itemType; + } + + public String getName() { + return name; + } + + public BigDecimal getPrice() { + return price; + } + + public Store getStore() { + return store; + } + + public void setColor(String color) { + this.color = color; + } + + public void setGrade(String grade) { + this.grade = grade; + } + + public void setId(Long id) { + this.id = id; + } + + public void setItemType(ItemType itemType) { + this.itemType = itemType; + } + + public void setName(String name) { + this.name = name; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public void setStore(Store store) { + this.store = store; + } +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/domain/ItemType.java b/spring-data-jpa/src/main/java/com/baeldung/domain/ItemType.java new file mode 100644 index 0000000000..412079c2ae --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/domain/ItemType.java @@ -0,0 +1,46 @@ +package com.baeldung.domain; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; + +@Entity +public class ItemType { + + @Id + private Long id; + @OneToMany(cascade = CascadeType.ALL) + @JoinColumn(name = "ITEM_TYPE_ID") + private List items = new ArrayList<>(); + + private String name; + + public Long getId() { + return id; + } + + public List getItems() { + return items; + } + + public String getName() { + return name; + } + + public void setId(Long id) { + this.id = id; + } + + public void setItems(List items) { + this.items = items; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/domain/Location.java b/spring-data-jpa/src/main/java/com/baeldung/domain/Location.java new file mode 100644 index 0000000000..2178d378eb --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/domain/Location.java @@ -0,0 +1,55 @@ +package com.baeldung.domain; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; + +@Entity +public class Location { + + private String city; + private String country; + @Id + private Long id; + + @OneToMany(cascade = CascadeType.ALL) + @JoinColumn(name = "LOCATION_ID") + private List stores = new ArrayList<>(); + + public String getCity() { + return city; + } + + public String getCountry() { + return country; + } + + public Long getId() { + return id; + } + + public List getStores() { + return stores; + } + + public void setCity(String city) { + this.city = city; + } + + public void setCountry(String country) { + this.country = country; + } + + public void setId(Long id) { + this.id = id; + } + + public void setStores(List stores) { + this.stores = stores; + } +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/domain/Store.java b/spring-data-jpa/src/main/java/com/baeldung/domain/Store.java new file mode 100644 index 0000000000..e04684c479 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/domain/Store.java @@ -0,0 +1,76 @@ +package com.baeldung.domain; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; + +@Entity +public class Store { + + private Boolean active; + @Id + private Long id; + @OneToMany(cascade = CascadeType.ALL) + @JoinColumn(name = "STORE_ID") + private List items = new ArrayList<>(); + private Long itemsSold; + + @ManyToOne + private Location location; + + private String name; + + public Boolean getActive() { + return active; + } + + public Long getId() { + return id; + } + + public List getItems() { + return items; + } + + public Long getItemsSold() { + return itemsSold; + } + + public Location getLocation() { + return location; + } + + public String getName() { + return name; + } + + public void setActive(Boolean active) { + this.active = active; + } + + public void setId(Long id) { + this.id = id; + } + + public void setItems(List items) { + this.items = items; + } + + public void setItemsSold(Long itemsSold) { + this.itemsSold = itemsSold; + } + + public void setLocation(Location location) { + this.location = location; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/repository/CustomItemRepository.java b/spring-data-jpa/src/main/java/com/baeldung/repository/CustomItemRepository.java new file mode 100644 index 0000000000..91eddb800a --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/repository/CustomItemRepository.java @@ -0,0 +1,15 @@ +package com.baeldung.repository; + +import org.springframework.stereotype.Repository; + +import com.baeldung.domain.Item; + +@Repository +public interface CustomItemRepository { + + void deleteCustom(Item entity); + + Item findItemById(Long id); + + void findThenDelete(Long id); +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/repository/CustomItemTypeRepository.java b/spring-data-jpa/src/main/java/com/baeldung/repository/CustomItemTypeRepository.java new file mode 100644 index 0000000000..77bbf294a0 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/repository/CustomItemTypeRepository.java @@ -0,0 +1,13 @@ +package com.baeldung.repository; + +import org.springframework.stereotype.Repository; + +import com.baeldung.domain.ItemType; + +@Repository +public interface CustomItemTypeRepository { + + void deleteCustom(ItemType entity); + + void findThenDelete(Long id); +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/repository/ItemTypeRepository.java b/spring-data-jpa/src/main/java/com/baeldung/repository/ItemTypeRepository.java new file mode 100644 index 0000000000..c3146aa297 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/repository/ItemTypeRepository.java @@ -0,0 +1,10 @@ +package com.baeldung.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.domain.ItemType; + +@Repository +public interface ItemTypeRepository extends JpaRepository, CustomItemTypeRepository, CustomItemRepository { +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/repository/LocationRepository.java b/spring-data-jpa/src/main/java/com/baeldung/repository/LocationRepository.java new file mode 100644 index 0000000000..f119ff916b --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/repository/LocationRepository.java @@ -0,0 +1,10 @@ +package com.baeldung.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.domain.Location; + +@Repository +public interface LocationRepository extends JpaRepository { +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/repository/ReadOnlyLocationRepository.java b/spring-data-jpa/src/main/java/com/baeldung/repository/ReadOnlyLocationRepository.java new file mode 100644 index 0000000000..2107712484 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/repository/ReadOnlyLocationRepository.java @@ -0,0 +1,15 @@ +package com.baeldung.repository; + +import java.util.Optional; + +import org.springframework.data.repository.Repository; + +import com.baeldung.domain.Location; + +@org.springframework.stereotype.Repository +public interface ReadOnlyLocationRepository extends Repository { + + Optional findById(Long id); + + Location save(Location location); +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/repository/StoreRepository.java b/spring-data-jpa/src/main/java/com/baeldung/repository/StoreRepository.java new file mode 100644 index 0000000000..939ca1dacb --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/repository/StoreRepository.java @@ -0,0 +1,13 @@ +package com.baeldung.repository; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.domain.Store; + +@Repository +public interface StoreRepository extends JpaRepository { + List findStoreByLocationId(Long locationId); +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/repository/impl/CustomItemRepositoryImpl.java b/spring-data-jpa/src/main/java/com/baeldung/repository/impl/CustomItemRepositoryImpl.java new file mode 100644 index 0000000000..44492a8f35 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/repository/impl/CustomItemRepositoryImpl.java @@ -0,0 +1,32 @@ +package com.baeldung.repository.impl; + +import javax.persistence.EntityManager; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +import com.baeldung.domain.Item; +import com.baeldung.repository.CustomItemRepository; + +@Repository +public class CustomItemRepositoryImpl implements CustomItemRepository { + + @Autowired + private EntityManager entityManager; + + @Override + public void deleteCustom(Item item) { + entityManager.remove(item); + } + + @Override + public Item findItemById(Long id) { + return entityManager.find(Item.class, id); + } + + @Override + public void findThenDelete(Long id) { + final Item item = entityManager.find(Item.class, id); + entityManager.remove(item); + } +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/repository/impl/CustomItemTypeRepositoryImpl.java b/spring-data-jpa/src/main/java/com/baeldung/repository/impl/CustomItemTypeRepositoryImpl.java new file mode 100644 index 0000000000..594fd24ea9 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/repository/impl/CustomItemTypeRepositoryImpl.java @@ -0,0 +1,31 @@ +package com.baeldung.repository.impl; + +import javax.persistence.EntityManager; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +import com.baeldung.domain.ItemType; +import com.baeldung.repository.CustomItemTypeRepository; + +@Repository +public class CustomItemTypeRepositoryImpl implements CustomItemTypeRepository { + + private static final Logger LOGGER = LoggerFactory.getLogger(CustomItemTypeRepositoryImpl.class); + + @Autowired + private EntityManager entityManager; + + @Override + public void deleteCustom(ItemType itemType) { + entityManager.remove(itemType); + } + + @Override + public void findThenDelete(Long id) { + ItemType itemTypeToDelete = entityManager.find(ItemType.class, id); + entityManager.remove(itemTypeToDelete); + } +} diff --git a/spring-data-jpa/src/test/java/com/baeldung/repository/JpaRepositoriesIntegrationTest.java b/spring-data-jpa/src/test/java/com/baeldung/repository/JpaRepositoriesIntegrationTest.java new file mode 100644 index 0000000000..d8b7bc4a88 --- /dev/null +++ b/spring-data-jpa/src/test/java/com/baeldung/repository/JpaRepositoriesIntegrationTest.java @@ -0,0 +1,93 @@ +package com.baeldung.repository; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertNotNull; +import static junit.framework.TestCase.assertNull; +import static junit.framework.TestCase.assertTrue; + +import java.util.List; +import java.util.Optional; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.domain.Item; +import com.baeldung.domain.ItemType; +import com.baeldung.domain.Location; +import com.baeldung.domain.Store; + +@RunWith(SpringRunner.class) +@DataJpaTest +public class JpaRepositoriesIntegrationTest { + @Autowired + private LocationRepository locationRepository; + @Autowired + private StoreRepository storeRepository; + @Autowired + private ItemTypeRepository compositeRepository; + @Autowired + private ReadOnlyLocationRepository readOnlyRepository; + + @Test + public void whenSaveLocation_ThenGetSameLocation() { + Location location = new Location(); + location.setId(100L); + location.setCountry("Country H"); + location.setCity("City Hundred"); + location = locationRepository.saveAndFlush(location); + + Location otherLocation = locationRepository.getOne(location.getId()); + assertEquals("Country H", otherLocation.getCountry()); + assertEquals("City Hundred", otherLocation.getCity()); + + locationRepository.delete(otherLocation); + } + + @Test + public void givenLocationId_whenFindStores_thenGetStores() { + List stores = storeRepository.findStoreByLocationId(1L); + assertEquals(1, stores.size()); + } + + @Test + public void givenItemTypeId_whenDeleted_ThenItemTypeDeleted() { + Optional itemType = compositeRepository.findById(1L); + assertTrue(itemType.isPresent()); + compositeRepository.deleteCustom(itemType.get()); + itemType = compositeRepository.findById(1L); + assertFalse(itemType.isPresent()); + } + + @Test + public void givenItemId_whenUsingCustomRepo_ThenDeleteAppropriateEntity() { + Item item = compositeRepository.findItemById(1L); + assertNotNull(item); + compositeRepository.deleteCustom(item); + item = compositeRepository.findItemById(1L); + assertNull(item); + } + + @Test + public void givenItemAndItemType_WhenAmbiguousDeleteCalled_ThenItemTypeDeletedAndNotItem() { + Optional itemType = compositeRepository.findById(1L); + assertTrue(itemType.isPresent()); + Item item = compositeRepository.findItemById(2L); + assertNotNull(item); + + compositeRepository.findThenDelete(1L); + Optional sameItemType = compositeRepository.findById(1L); + assertFalse(sameItemType.isPresent()); + Item sameItem = compositeRepository.findItemById(2L); + assertNotNull(sameItem); + } + + @Test + public void whenCreatingReadOnlyRepo_thenHaveOnlyReadOnlyOperationsAvailable() { + Optional location = readOnlyRepository.findById(1L); + assertNotNull(location); + } +} diff --git a/spring-data-jpa/src/test/resources/application.properties b/spring-data-jpa/src/test/resources/application.properties index de6ee2e6b5..73d72bc7d6 100644 --- a/spring-data-jpa/src/test/resources/application.properties +++ b/spring-data-jpa/src/test/resources/application.properties @@ -12,4 +12,4 @@ hibernate.cache.use_second_level_cache=true hibernate.cache.use_query_cache=true hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory -spring.datasource.data=import_articles.sql \ No newline at end of file +spring.datasource.data=import_entities.sql \ No newline at end of file diff --git a/spring-data-jpa/src/test/resources/import_articles.sql b/spring-data-jpa/src/test/resources/import_articles.sql deleted file mode 100644 index 4fe18bf4aa..0000000000 --- a/spring-data-jpa/src/test/resources/import_articles.sql +++ /dev/null @@ -1,3 +0,0 @@ -insert into Article(id, publication_date, publication_time, creation_date_time) values(1, TO_DATE('01/01/2018', 'DD/MM/YYYY'), TO_DATE('15:00', 'HH24:MI'), TO_DATE('31/12/2017 07:30', 'DD/MM/YYYY HH24:MI')); -insert into Article(id, publication_date, publication_time, creation_date_time) values(2, TO_DATE('01/01/2018', 'DD/MM/YYYY'), TO_DATE('15:30', 'HH24:MI'), TO_DATE('15/12/2017 08:00', 'DD/MM/YYYY HH24:MI')); -insert into Article(id, publication_date, publication_time, creation_date_time) values(3, TO_DATE('15/12/2017', 'DD/MM/YYYY'), TO_DATE('16:00', 'HH24:MI'), TO_DATE('01/12/2017 13:45', 'DD/MM/YYYY HH24:MI')); \ No newline at end of file diff --git a/spring-data-jpa/src/test/resources/import_entities.sql b/spring-data-jpa/src/test/resources/import_entities.sql new file mode 100644 index 0000000000..deb9d07f05 --- /dev/null +++ b/spring-data-jpa/src/test/resources/import_entities.sql @@ -0,0 +1,21 @@ +insert into Article(id, publication_date, publication_time, creation_date_time) values(1, TO_DATE('01/01/2018', 'DD/MM/YYYY'), TO_DATE('15:00', 'HH24:MI'), TO_DATE('31/12/2017 07:30', 'DD/MM/YYYY HH24:MI')); +insert into Article(id, publication_date, publication_time, creation_date_time) values(2, TO_DATE('01/01/2018', 'DD/MM/YYYY'), TO_DATE('15:30', 'HH24:MI'), TO_DATE('15/12/2017 08:00', 'DD/MM/YYYY HH24:MI')); +insert into Article(id, publication_date, publication_time, creation_date_time) values(3, TO_DATE('15/12/2017', 'DD/MM/YYYY'), TO_DATE('16:00', 'HH24:MI'), TO_DATE('01/12/2017 13:45', 'DD/MM/YYYY HH24:MI')); + +insert into location (id, country, city) values (1, 'Country X', 'City One'); +insert into location (id, country, city) values (2, 'Country X', 'City Two'); +insert into location (id, country, city) values (3, 'Country X', 'City Three'); + +insert into store (id, name, location_id, items_sold, active) values (1, 'Store One', 3, 130000, true); +insert into store (id, name, location_id, items_sold, active) values (2, 'Store Two', 1, 170000, false); + +insert into item_type (id, name) values (1, 'Food'); +insert into item_type (id, name) values (2, 'Furniture'); +insert into item_type (id, name) values (3, 'Electronics'); + +insert into item (id, name, store_id, item_type_id, price, grade, color) values (1, 'Food Item One', 1, 1, 100, 'A', 'Color x'); +insert into item (id, name, store_id, item_type_id, price, grade, color) values (2, 'Furniture Item One', 1, 2, 2500, 'B', 'Color y'); +insert into item (id, name, store_id, item_type_id, price, grade, color) values (3, 'Food Item Two', 1, 1, 35, 'A', 'Color z'); +insert into item (id, name, store_id, item_type_id, price, grade, color) values (5, 'Furniture Item Two', 2, 2, 1600, 'A', 'Color w'); +insert into item (id, name, store_id, item_type_id, price, grade, color) values (6, 'Food Item Three', 2, 1, 5, 'B', 'Color a'); +insert into item (id, name, store_id, item_type_id, price, grade, color) values (7, 'Electronics Item One', 2, 3, 999, 'B', 'Color b');