├── README.md ├── spring-boot-data-jpa-crud ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── boot │ │ └── data │ │ └── jpa │ │ └── crud │ │ ├── SpringBootDataJpaCrudApp.java │ │ ├── entity │ │ └── Website.java │ │ ├── repository │ │ └── WebsiteRepository.java │ │ ├── rest │ │ └── controller │ │ │ └── WebsiteRestController.java │ │ ├── service │ │ └── WebsiteService.java │ │ └── vo │ │ └── WebsiteVo.java │ └── resources │ ├── application.properties │ └── application.yml ├── spring-boot-h2-integration ├── pom.xml ├── pom.xml_3 ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── boot │ │ └── h2 │ │ └── integration │ │ ├── SpringH2App.java │ │ ├── SpringH2App.java_3 │ │ ├── entity │ │ ├── Employee.java │ │ └── Employee.java_3 │ │ ├── repository │ │ └── EmployeeRepository.java │ │ └── rest │ │ └── controller │ │ └── EmployeeRestController.java │ └── resources │ ├── application.properties │ └── data.sql ├── spring-boot-hikari-connection-pool ├── item.sql ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── springboot │ │ └── hikari │ │ └── connectionpool │ │ ├── App.java │ │ ├── entity │ │ └── Item.java │ │ ├── repository │ │ └── ItemRepository.java │ │ └── service │ │ └── ItemService.java │ └── resources │ └── application.properties ├── spring-boot-jndi-datasource ├── pom.xml ├── readme.rst ├── spring_boot_3_3_3 │ └── spring-boot-jndi-datasource │ │ ├── pom.xml │ │ └── src │ │ └── main │ │ ├── com │ │ └── roytuts │ │ │ └── spring │ │ │ └── boot │ │ │ └── jndi │ │ │ └── datasource │ │ │ ├── JndiDatasourceApp.java │ │ │ ├── config │ │ │ ├── AppConfig.java │ │ │ └── DatabaseProperties.java │ │ │ ├── entity │ │ │ └── Company.java │ │ │ ├── repository │ │ │ └── CompanyRepository.java │ │ │ ├── rest │ │ │ └── controller │ │ │ │ └── CompanyRestController.java │ │ │ └── service │ │ │ └── CompanyService.java │ │ └── resources │ │ └── application.properties └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── boot │ │ └── jndi │ │ └── datasource │ │ ├── JndiDatasourceApp.java │ │ ├── config │ │ ├── AppConfig.java │ │ └── DatabaseProperties.java │ │ ├── entity │ │ └── Company.java │ │ ├── repository │ │ └── CompanyRepository.java │ │ ├── rest │ │ └── controller │ │ │ └── CompanyRestController.java │ │ └── service │ │ └── CompanyService.java │ └── resources │ └── application.properties ├── spring-data-jpa-audit ├── employee.sql ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── data │ │ └── jpa │ │ └── audit │ │ ├── App.java │ │ ├── domain │ │ └── AuditorAwareImpl.java │ │ ├── entity │ │ └── Employee.java │ │ ├── repository │ │ └── EmployeeJpaRepository.java │ │ ├── rest │ │ └── controller │ │ │ └── EmployeeRestController.java │ │ ├── service │ │ └── EmployeeService.java │ │ └── vo │ │ └── EmployeeVo.java │ └── resources │ └── application.properties ├── spring-data-jpa-batch-insertion ├── build.gradle ├── employee.json ├── employee.sql ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── data │ │ └── jpa │ │ └── batch │ │ └── insertion │ │ ├── SpringDataJpabatchInsertionApp.java │ │ ├── config │ │ └── DbConfig.java │ │ ├── controller │ │ └── EmployeeRestController.java │ │ ├── entity │ │ └── Employee.java │ │ ├── repository │ │ └── EmployeeRepository.java │ │ └── service │ │ └── EmployeeService.java │ └── resources │ └── application.properties ├── spring-data-jpa-criteriadelete ├── pom.xml ├── pom.xml_3_1_3 ├── product.sql ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── data │ │ └── jpa │ │ └── criteriadelete │ │ ├── CriteriaDeleteApp.java │ │ ├── CriteriaDeleteApp.java_3_1_3 │ │ ├── entity │ │ ├── Product.java │ │ └── Product.java_3_1_3 │ │ └── service │ │ ├── DeleteService.java │ │ └── DeleteService.java_3_1_3 │ └── resources │ └── application.properties ├── spring-data-jpa-criteriaupdate ├── pom.xml ├── pom.xml_3_1_3 ├── product.sql ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── data │ │ └── jpa │ │ └── criteriaupdate │ │ ├── CriteriaUpdateApp.java │ │ ├── CriteriaUpdateApp.java_3_1_3 │ │ ├── entity │ │ ├── Product.java │ │ └── Product.java_3_1_3 │ │ └── service │ │ ├── UpdateService.java │ │ └── UpdateService.java_3_1_3 │ └── resources │ └── application.properties ├── spring-data-jpa-crud ├── build.gradle ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── data │ │ └── jpa │ │ └── crud │ │ ├── SpringDataJpaCrudApp.java │ │ ├── config │ │ └── Config.java │ │ ├── entity │ │ ├── Product.java │ │ └── ProductType.java │ │ ├── repository │ │ ├── ProductRepository.java │ │ └── ProductTypeRepository.java │ │ └── service │ │ ├── ProductService.java │ │ └── ProductTypeService.java │ └── resources │ ├── data.sql │ └── schema.sql ├── spring-data-jpa-entity-graphs ├── build.gradle ├── department-employee.sql ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── data │ │ └── jpa │ │ └── entity │ │ └── graphs │ │ ├── SpringDataJpaEntityGraphApp.java │ │ ├── config │ │ └── Config.java │ │ ├── entity │ │ ├── Department.java │ │ └── Employee.java │ │ ├── repository │ │ ├── DepartmentRepository.java │ │ └── EmployeeRepository.java │ │ └── service │ │ └── DepartmentService.java │ └── resources │ └── application.properties ├── spring-data-jpa-hibernate-usertype ├── build.gradle ├── employee.sql ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── data │ │ └── jpa │ │ └── hibernate │ │ └── usertype │ │ ├── SpringDataJpaHibernateUserTypeApp.java │ │ ├── config │ │ └── Config.java │ │ ├── entity │ │ └── Employee.java │ │ ├── repository │ │ └── EmployeeRepository.java │ │ ├── service │ │ └── EmployeeService.java │ │ └── type │ │ └── EmployeeType.java │ └── resources │ └── application.properties ├── spring-data-jpa-in-clause-where-table-multiple-rows-selection-deletion ├── build.gradle ├── pom.xml ├── product.sql ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── data │ │ └── jpa │ │ └── table │ │ └── multiple │ │ └── rows │ │ └── selection │ │ └── deletion │ │ ├── SpringJpaTableMultiRowsSelectionDeletionApp.java │ │ ├── dto │ │ └── Input.java │ │ ├── entity │ │ └── Product.java │ │ ├── repository │ │ └── ProductRepository.java │ │ ├── rest │ │ └── controller │ │ │ └── ProductRestController.java │ │ └── service │ │ └── ProductService.java │ └── resources │ ├── application.properties │ └── data.sql ├── spring-data-jpa-left-right-inner-cross-join-three-tables ├── build.gradle ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── data │ │ └── jpa │ │ └── left │ │ └── right │ │ └── inner │ │ └── cross │ │ └── join │ │ ├── SpringDataJpaLeftrightinnerCrossJoinApp.java │ │ ├── config │ │ └── JoinConfig.java │ │ ├── dto │ │ └── JoinDto.java │ │ ├── entity │ │ ├── Company.java │ │ ├── Food.java │ │ └── Sale.java │ │ ├── repository │ │ └── JoinRepository.java │ │ ├── rest │ │ └── controller │ │ │ └── JoinRestController.java │ │ └── service │ │ └── JoinService.java │ └── resources │ └── application.properties ├── spring-data-jpa-left-right-inner-cross-join ├── pom.xml ├── readme.rst ├── spring-boot-3 │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── roytuts │ │ │ └── spring │ │ │ └── data │ │ │ └── jpa │ │ │ └── left │ │ │ └── right │ │ │ └── inner │ │ │ └── cross │ │ │ └── join │ │ │ ├── SpringDataJpaLeftRightInnerCrossJoinApp.java │ │ │ ├── dto │ │ │ └── DeptEmpDto.java │ │ │ ├── entity │ │ │ ├── Department.java │ │ │ └── Employee.java │ │ │ ├── repository │ │ │ ├── DepartmentRepository.java │ │ │ └── EmployeeRepository.java │ │ │ ├── rest │ │ │ └── controller │ │ │ │ └── JoinQueryController.java │ │ │ └── service │ │ │ └── JoinQueryService.java │ │ └── resources │ │ └── application.properties ├── spring-data-join.sql └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── data │ │ └── jpa │ │ └── left │ │ └── right │ │ └── inner │ │ └── cross │ │ └── join │ │ ├── SpringDataJpaLeftRightInnerCrossJoinApp.java │ │ ├── dto │ │ └── DeptEmpDto.java │ │ ├── entity │ │ ├── Department.java │ │ └── Employee.java │ │ ├── repository │ │ ├── DepartmentRepository.java │ │ └── EmployeeRepository.java │ │ ├── rest │ │ └── controller │ │ │ └── JoinQueryController.java │ │ └── service │ │ └── JoinQueryService.java │ └── resources │ └── application.properties ├── spring-data-jpa-named-query ├── build.gradle ├── pom.xml ├── readme.rst ├── settings.gradle └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── data │ │ └── jpa │ │ └── named │ │ └── query │ │ ├── SpringDataJpaNamedQueryApp.java │ │ ├── config │ │ └── Config.java │ │ ├── entity │ │ └── Item.java │ │ ├── repository │ │ └── NameQueryRepository.java │ │ └── service │ │ └── NamedQueryService.java │ └── resources │ ├── application.properties │ └── sql │ ├── data.sql │ └── table.sql ├── spring-data-jpa-native-query-to-dto ├── build.gradle ├── event.sql ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── data │ │ └── jpa │ │ └── natve │ │ └── query │ │ ├── SpringDataJpaNativeQueryApp.java │ │ ├── dto │ │ └── EventDto.java │ │ ├── model │ │ └── Event.java │ │ ├── repository │ │ └── SpringDataJpaNativeQueryRepository.java │ │ └── service │ │ └── SpringDataJpaNativeQueryService.java │ └── resources │ └── application.properties ├── spring-data-jpa-pageable ├── pom.xml ├── readme.rst ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── roytuts │ │ │ └── spring │ │ │ └── data │ │ │ └── jpa │ │ │ └── pageable │ │ │ ├── PageableApp.java │ │ │ ├── converter │ │ │ └── EntityDtoConverter.java │ │ │ ├── dto │ │ │ └── NoteDto.java │ │ │ ├── entity │ │ │ └── Note.java │ │ │ ├── initializer │ │ │ └── NoteInitializer.java │ │ │ ├── repository │ │ │ └── NoteRepository.java │ │ │ ├── rest │ │ │ └── controller │ │ │ │ └── NoteRestController.java │ │ │ └── service │ │ │ └── NoteService.java │ │ └── resources │ │ └── application.properties └── ~$ring-data-jpa-pageable.docx ├── spring-data-jpa-specification-criteria-in-clause ├── build.gradle ├── readme.rst ├── spring-boot-3 │ ├── pom.xml │ └── src │ │ └── main │ │ └── resources │ │ ├── application.properties │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── data │ │ └── jpa │ │ └── specification │ │ └── criteria │ │ └── in │ │ └── clause │ │ ├── SpringDataJpaSpecificationCriteriaInClauseApp.java │ │ ├── criteria │ │ └── CaseSearchCriteria.java │ │ ├── entity │ │ ├── Case.java │ │ ├── Country.java │ │ └── Region.java │ │ ├── repository │ │ └── CaseRepo.java │ │ ├── rest │ │ └── controller │ │ │ └── CaseSearchController.java │ │ ├── service │ │ └── SearchCaseService.java │ │ └── vo │ │ └── CaseVo.java ├── sql.sql └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── data │ │ └── jpa │ │ └── specification │ │ └── criteria │ │ └── in │ │ └── clause │ │ ├── SpringDataJpaSpecificationCriteriaInClauseApp.java │ │ ├── config │ │ └── DatabaseConfig.java │ │ ├── criteria │ │ └── CaseSearchCriteria.java │ │ ├── entity │ │ ├── Case.java │ │ ├── Country.java │ │ └── Region.java │ │ ├── repository │ │ └── CaseRepo.java │ │ ├── rest │ │ └── controller │ │ │ └── CaseSearchController.java │ │ ├── service │ │ └── SearchCaseService.java │ │ └── vo │ │ └── CaseVo.java │ └── resources │ └── application.properties ├── spring-data-jpa-stored-procedure ├── build.gradle ├── pom.xml ├── pom.xml_3 ├── readme.rst ├── src │ └── main │ │ ├── java │ │ └── spring │ │ │ └── data │ │ │ └── jpa │ │ │ └── stored │ │ │ └── procedure │ │ │ ├── SpringDataJpaStoredProcedureApp.java │ │ │ ├── SpringDataJpaStoredProcedureApp.java_3 │ │ │ ├── entity │ │ │ ├── UserDetails.java │ │ │ └── UserDetails.java_3 │ │ │ ├── repository │ │ │ ├── UserDetailsJpaRepository.java │ │ │ └── UserDetailsRepository.java │ │ │ └── service │ │ │ └── UserService.java │ │ └── resources │ │ └── application.properties └── user_details.sql ├── spring-image-blob-image ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── image │ │ └── blob │ │ ├── App.java │ │ ├── controller │ │ └── ImageController.java │ │ ├── entity │ │ └── Image.java │ │ ├── repository │ │ └── ImageRepository.java │ │ └── service │ │ └── ImageService.java │ └── resources │ ├── application.properties │ └── templates │ ├── images.html │ └── upload.html ├── spring-jpa-bidirectional-manytomany-without-join-entity ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── jpa │ │ └── bidirectional │ │ └── manytomany │ │ └── without │ │ └── join │ │ └── entity │ │ ├── BidirectionalManyToManyWithoutJoinEntityApp.java │ │ ├── entity │ │ ├── Artist.java │ │ └── Cd.java │ │ ├── repository │ │ ├── ArtistRepository.java │ │ └── CdRepository.java │ │ └── service │ │ └── ManyToManyService.java │ └── resources │ └── application.properties ├── spring-jpa-bidirectional-manytoone-onetomany ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── jpa │ │ └── bidirectional │ │ └── manytoone │ │ └── onetomany │ │ ├── ManyToOneOneToManyBidirectionalApp.java │ │ ├── entity │ │ ├── Artist.java │ │ └── Cd.java │ │ ├── repository │ │ ├── ArtistRepository.java │ │ └── CdRepository.java │ │ └── service │ │ └── ManyToOneOneToManyService.java │ └── resources │ └── application.properties ├── spring-jpa-unidirectional-manytoone ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── jpa │ │ └── unidirectional │ │ └── manytoone │ │ ├── ManyToOneUnidirectionalApp.java │ │ ├── entity │ │ ├── Artist.java │ │ └── Cd.java │ │ ├── repository │ │ └── CdRepository.java │ │ └── service │ │ └── ManyToOneService.java │ └── resources │ └── application.properties ├── springboot-data-jpa-composite-primary-key ├── pom.xml ├── readme.rst ├── spring_340 │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── roytuts │ │ │ └── springboot │ │ │ └── datajpa │ │ │ └── composite │ │ │ └── primarykey │ │ │ ├── App.java │ │ │ ├── entity │ │ │ ├── User.java │ │ │ └── UserPKey.java │ │ │ └── repository │ │ │ └── UserRepository.java │ │ └── resources │ │ └── application.properties ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── roytuts │ │ │ └── springboot │ │ │ └── datajpa │ │ │ └── composite │ │ │ └── primarykey │ │ │ ├── App.java │ │ │ ├── entity │ │ │ ├── User.java │ │ │ └── UserPKey.java │ │ │ └── repository │ │ │ └── UserRepository.java │ │ └── resources │ │ └── application.properties └── user.sql └── springboot-mapstruct-jpa-entity-dto-mapping ├── pom.xml ├── readme.rst └── src └── main ├── java └── com │ └── roytuts │ └── springboot │ └── mapstruct │ └── jpa │ └── entity │ └── dto │ └── mapping │ ├── App.java │ ├── dto │ └── EventDto.java │ ├── entity │ └── Event.java │ ├── mapper │ └── EventMapper.java │ ├── repository │ └── EventRepository.java │ ├── restcontroller │ └── EventRestController.java │ └── service │ └── EventService.java └── resources └── application.properties /README.md: -------------------------------------------------------------------------------- 1 | Spring Data JPA related programs 2 | -------------------------------------------------------------------------------- /spring-boot-data-jpa-crud/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-boot-data-jpa-crud-example/ 2 | -------------------------------------------------------------------------------- /spring-boot-data-jpa-crud/src/main/java/com/roytuts/spring/boot/data/jpa/crud/SpringBootDataJpaCrudApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.data.jpa.crud; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.domain.EntityScan; 6 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 7 | 8 | @SpringBootApplication 9 | @EntityScan("com.roytuts.spring.boot.data.jpa.crud.entity") 10 | @EnableJpaRepositories("com.roytuts.spring.boot.data.jpa.crud.repository") 11 | public class SpringBootDataJpaCrudApp { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(SpringBootDataJpaCrudApp.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-boot-data-jpa-crud/src/main/java/com/roytuts/spring/boot/data/jpa/crud/entity/Website.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.data.jpa.crud.entity; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.GeneratedValue; 5 | import jakarta.persistence.GenerationType; 6 | import jakarta.persistence.Id; 7 | 8 | @Entity 9 | public class Website { 10 | 11 | @Id 12 | @GeneratedValue(strategy = GenerationType.IDENTITY) 13 | private Integer id; 14 | private String name; 15 | private String url; 16 | 17 | public Website() { 18 | } 19 | 20 | public Website(Integer id, String name, String url) { 21 | this.id = id; 22 | this.name = name; 23 | this.url = url; 24 | } 25 | 26 | public Integer getId() { 27 | return id; 28 | } 29 | 30 | public void setId(Integer id) { 31 | this.id = id; 32 | } 33 | 34 | public String getName() { 35 | return name; 36 | } 37 | 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | 42 | public String getUrl() { 43 | return url; 44 | } 45 | 46 | public void setUrl(String url) { 47 | this.url = url; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /spring-boot-data-jpa-crud/src/main/java/com/roytuts/spring/boot/data/jpa/crud/repository/WebsiteRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.data.jpa.crud.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.boot.data.jpa.crud.entity.Website; 6 | 7 | public interface WebsiteRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-boot-data-jpa-crud/src/main/java/com/roytuts/spring/boot/data/jpa/crud/service/WebsiteService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.data.jpa.crud.service; 2 | 3 | import java.util.List; 4 | import java.util.stream.Collectors; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import com.roytuts.spring.boot.data.jpa.crud.entity.Website; 10 | import com.roytuts.spring.boot.data.jpa.crud.repository.WebsiteRepository; 11 | import com.roytuts.spring.boot.data.jpa.crud.vo.WebsiteVo; 12 | 13 | @Service 14 | public class WebsiteService { 15 | 16 | @Autowired 17 | private WebsiteRepository repository; 18 | 19 | public List getWebsiteList() { 20 | return repository.findAll().stream().map(w -> { 21 | WebsiteVo vo = new WebsiteVo(w.getId(), w.getName(), w.getUrl()); 22 | return vo; 23 | }).collect(Collectors.toList()); 24 | } 25 | 26 | public WebsiteVo getWebsiteById(Integer id) { 27 | return repository.findById(id).map(w -> { 28 | WebsiteVo vo = new WebsiteVo(w.getId(), w.getName(), w.getUrl()); 29 | return vo; 30 | }).orElseGet(null); 31 | } 32 | 33 | public void saveWebsite(WebsiteVo vo) { 34 | Website website = new Website(); 35 | website.setName(vo.getName()); 36 | website.setUrl(vo.getUrl()); 37 | repository.save(website); 38 | } 39 | 40 | public void updateWebsite(WebsiteVo vo) { 41 | Website website = new Website(vo.getId(), vo.getName(), vo.getUrl()); 42 | repository.save(website); 43 | } 44 | 45 | public void deleteWebsite(WebsiteVo vo) { 46 | Website website = new Website(vo.getId(), vo.getName(), vo.getUrl()); 47 | repository.delete(website); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /spring-boot-data-jpa-crud/src/main/java/com/roytuts/spring/boot/data/jpa/crud/vo/WebsiteVo.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.data.jpa.crud.vo; 2 | 3 | public class WebsiteVo { 4 | 5 | private Integer id; 6 | private String name; 7 | private String url; 8 | 9 | public WebsiteVo() { 10 | } 11 | 12 | public WebsiteVo(Integer id, String name, String url) { 13 | this.id = id; 14 | this.name = name; 15 | this.url = url; 16 | } 17 | 18 | public Integer getId() { 19 | return id; 20 | } 21 | 22 | public void setId(Integer id) { 23 | this.id = id; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | 34 | public String getUrl() { 35 | return url; 36 | } 37 | 38 | public void setUrl(String url) { 39 | this.url = url; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring-boot-data-jpa-crud/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.jpa.show-sql=true 2 | spring.h2.console.enabled=true 3 | -------------------------------------------------------------------------------- /spring-boot-data-jpa-crud/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | jpa: 3 | show-sql: true 4 | h2: 5 | console: 6 | enabled: true 7 | -------------------------------------------------------------------------------- /spring-boot-h2-integration/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.roytuts 6 | spring-boot-h2-integration 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | UTF-8 12 | 8 13 | 8 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.5.4 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-jpa 31 | 32 | 33 | 34 | com.h2database 35 | h2 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-maven-plugin 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /spring-boot-h2-integration/pom.xml_3: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-boot-h2-integration 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.1.5 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-data-jpa 33 | 34 | 35 | 36 | com.h2database 37 | h2 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-boot-h2-integration/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/integrate-h2-in-memory-database-with-spring-boot/ 2 | 3 | You may find the spring boot 3 related files with _3 appended in the file name. 4 | -------------------------------------------------------------------------------- /spring-boot-h2-integration/src/main/java/com/roytuts/spring/boot/h2/integration/SpringH2App.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.h2.integration; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.domain.EntityScan; 6 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 7 | 8 | @SpringBootApplication 9 | @EntityScan(basePackages = "com.roytuts.spring.boot.h2.integration.entity") 10 | @EnableJpaRepositories(basePackages = "com.roytuts.spring.boot.h2.integration.repository") 11 | public class SpringH2App { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(SpringH2App.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-boot-h2-integration/src/main/java/com/roytuts/spring/boot/h2/integration/SpringH2App.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.h2.integration; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringH2App { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringH2App.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-h2-integration/src/main/java/com/roytuts/spring/boot/h2/integration/entity/Employee.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.h2.integration.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.Id; 7 | 8 | @Entity 9 | // @Table(name = "employee") 10 | public class Employee { 11 | 12 | @Id 13 | @GeneratedValue // (strategy = GenerationType.IDENTITY) 14 | private Integer id; 15 | 16 | private String name; 17 | 18 | @Column(name = "email_address") 19 | private String email; 20 | 21 | public Integer getId() { 22 | return id; 23 | } 24 | 25 | public void setId(Integer id) { 26 | this.id = id; 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | public String getEmail() { 38 | return email; 39 | } 40 | 41 | public void setEmail(String email) { 42 | this.email = email; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /spring-boot-h2-integration/src/main/java/com/roytuts/spring/boot/h2/integration/entity/Employee.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.h2.integration.entity; 2 | 3 | import jakarta.persistence.Column; 4 | import jakarta.persistence.Entity; 5 | import jakarta.persistence.GeneratedValue; 6 | import jakarta.persistence.GenerationType; 7 | import jakarta.persistence.Id; 8 | 9 | @Entity 10 | public class Employee { 11 | 12 | @Id 13 | @GeneratedValue(strategy = GenerationType.IDENTITY) 14 | private Integer id; 15 | 16 | private String name; 17 | 18 | @Column(name = "email_address") 19 | private String email; 20 | 21 | public Integer getId() { 22 | return id; 23 | } 24 | 25 | public void setId(Integer id) { 26 | this.id = id; 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | public String getEmail() { 38 | return email; 39 | } 40 | 41 | public void setEmail(String email) { 42 | this.email = email; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /spring-boot-h2-integration/src/main/java/com/roytuts/spring/boot/h2/integration/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.h2.integration.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.boot.h2.integration.entity.Employee; 6 | 7 | public interface EmployeeRepository extends JpaRepository { 8 | 9 | Employee findByName(String name); 10 | 11 | Employee findByEmail(String email); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-h2-integration/src/main/java/com/roytuts/spring/boot/h2/integration/rest/controller/EmployeeRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.h2.integration.rest.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.http.ResponseEntity; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import com.roytuts.spring.boot.h2.integration.entity.Employee; 11 | import com.roytuts.spring.boot.h2.integration.repository.EmployeeRepository; 12 | 13 | @RestController 14 | public class EmployeeRestController { 15 | 16 | @Autowired 17 | private EmployeeRepository employeeRepository; 18 | 19 | @GetMapping("/employee/name/{name}") 20 | public ResponseEntity getEmployeeByName(@PathVariable String name) { 21 | Employee employee = employeeRepository.findByName(name); 22 | return new ResponseEntity(employee, HttpStatus.OK); 23 | } 24 | 25 | @GetMapping("/employee/email/{email}") 26 | public ResponseEntity getEmployeeByEmail(@PathVariable String email) { 27 | Employee employee = employeeRepository.findByEmail(email); 28 | return new ResponseEntity(employee, HttpStatus.OK); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /spring-boot-h2-integration/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:roytuts 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | 6 | 7 | spring.jpa.show-sql = true 8 | 9 | spring.h2.console.enabled = true 10 | spring.h2.console.path=/h2console/ 11 | 12 | spring.jpa.hibernate.ddl-auto = create 13 | 14 | spring.jpa.defer-datasource-initialization=true -------------------------------------------------------------------------------- /spring-boot-h2-integration/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | insert into employee(id,name,email_address) 2 | values(1,'Soumitra','soumitra@email.com'); 3 | insert into employee(id,name,email_address) 4 | values(2,'Liton','liton@email.com'); 5 | insert into employee(id,name,email_address) 6 | values(3,'Suman','suman@email.com'); 7 | insert into employee(id,name,email_address) 8 | values(4,'Debabrata','debabrata@email.com'); -------------------------------------------------------------------------------- /spring-boot-hikari-connection-pool/item.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `item` ( 2 | `item_id` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL AUTO_INCREMENT, 3 | `item_name` varchar(45) COLLATE utf8mb4_unicode_ci NOT NULL, 4 | `item_desc` text COLLATE utf8mb4_unicode_ci, 5 | `item_price` double COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0', 6 | PRIMARY KEY (`item_id`) 7 | ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 8 | 9 | 10 | insert into `item`(`item_id`,`item_name`,`item_desc`,`item_price`) 11 | values (1,'CD','CD is a compact disk',100), 12 | (2,'DVD','DVD is larger than CD in size',150), 13 | (3,'ABC','ABC test description',24), 14 | (4,'XYZ','XYZ test description',25.32), 15 | (5,'CD Player','CD player is used to play CD',30.02), 16 | (6,'New Item1','New Item1 Desc',125); 17 | -------------------------------------------------------------------------------- /spring-boot-hikari-connection-pool/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-boot-hikari-connection-pool 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | org.springframework.boot 14 | spring-boot-starter-parent 15 | 3.1.3 16 | 17 | 18 | 19 | UTF-8 20 | 19 21 | 19 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-data-jpa 33 | 34 | 35 | 36 | com.mysql 37 | mysql-connector-j 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-boot-hikari-connection-pool/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-boot-hikari-connection-pool-configurations/ 2 | -------------------------------------------------------------------------------- /spring-boot-hikari-connection-pool/src/main/java/com/roytuts/springboot/hikari/connectionpool/App.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.hikari.connectionpool; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.CommandLineRunner; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | 8 | import com.roytuts.springboot.hikari.connectionpool.service.ItemService; 9 | 10 | @SpringBootApplication 11 | public class App implements CommandLineRunner { 12 | 13 | @Autowired 14 | private ItemService itemService; 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(App.class, args); 18 | } 19 | 20 | @Override 21 | public void run(String... args) throws Exception { 22 | System.out.println(itemService.getItem(2)); 23 | 24 | System.out.println(); 25 | System.out.println(); 26 | 27 | itemService.getItemList().stream().forEach(i -> System.out.println(i)); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /spring-boot-hikari-connection-pool/src/main/java/com/roytuts/springboot/hikari/connectionpool/entity/Item.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.hikari.connectionpool.entity; 2 | 3 | import jakarta.persistence.Column; 4 | import jakarta.persistence.Entity; 5 | import jakarta.persistence.GeneratedValue; 6 | import jakarta.persistence.GenerationType; 7 | import jakarta.persistence.Id; 8 | import jakarta.persistence.Table; 9 | 10 | @Table 11 | @Entity 12 | public class Item { 13 | 14 | @Id 15 | @Column 16 | @GeneratedValue(strategy = GenerationType.IDENTITY) 17 | private long itemId; 18 | 19 | @Column 20 | private String itemName; 21 | 22 | @Column 23 | private String itemDesc; 24 | 25 | @Column 26 | private double itemPrice; 27 | 28 | public long getItemId() { 29 | return itemId; 30 | } 31 | 32 | public void setItemId(long itemId) { 33 | this.itemId = itemId; 34 | } 35 | 36 | public String getItemName() { 37 | return itemName; 38 | } 39 | 40 | public void setItemName(String itemName) { 41 | this.itemName = itemName; 42 | } 43 | 44 | public String getItemDesc() { 45 | return itemDesc; 46 | } 47 | 48 | public void setItemDesc(String itemDesc) { 49 | this.itemDesc = itemDesc; 50 | } 51 | 52 | public double getItemPrice() { 53 | return itemPrice; 54 | } 55 | 56 | public void setItemPrice(double itemPrice) { 57 | this.itemPrice = itemPrice; 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "Item [itemId=" + itemId + ", itemName=" + itemName + ", itemDesc=" + itemDesc + ", itemPrice=" 63 | + itemPrice + "]"; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /spring-boot-hikari-connection-pool/src/main/java/com/roytuts/springboot/hikari/connectionpool/repository/ItemRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.hikari.connectionpool.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.springboot.hikari.connectionpool.entity.Item; 6 | 7 | public interface ItemRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-boot-hikari-connection-pool/src/main/java/com/roytuts/springboot/hikari/connectionpool/service/ItemService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.hikari.connectionpool.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.roytuts.springboot.hikari.connectionpool.entity.Item; 9 | import com.roytuts.springboot.hikari.connectionpool.repository.ItemRepository; 10 | 11 | @Service 12 | public class ItemService { 13 | 14 | @Autowired 15 | private ItemRepository itemRepository; 16 | 17 | public Item getItem(long id) { 18 | return itemRepository.findById(id).get(); 19 | } 20 | 21 | public List getItemList() { 22 | return itemRepository.findAll(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-boot-jndi-datasource/ 2 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/spring_boot_3_3_3/spring-boot-jndi-datasource/src/main/com/roytuts/spring/boot/jndi/datasource/JndiDatasourceApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.jndi.datasource; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class JndiDatasourceApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(JndiDatasourceApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/spring_boot_3_3_3/spring-boot-jndi-datasource/src/main/com/roytuts/spring/boot/jndi/datasource/config/DatabaseProperties.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.jndi.datasource.config; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | 5 | @ConfigurationProperties(prefix = "app.datasource") 6 | public class DatabaseProperties { 7 | 8 | private String driverClassName; 9 | private String url; 10 | private String username; 11 | private String password; 12 | private String jndiName; 13 | 14 | public String getDriverClassName() { 15 | return driverClassName; 16 | } 17 | 18 | public void setDriverClassName(String driverClassName) { 19 | this.driverClassName = driverClassName; 20 | } 21 | 22 | public String getUrl() { 23 | return url; 24 | } 25 | 26 | public void setUrl(String url) { 27 | this.url = url; 28 | } 29 | 30 | public String getUsername() { 31 | return username; 32 | } 33 | 34 | public void setUsername(String username) { 35 | this.username = username; 36 | } 37 | 38 | public String getPassword() { 39 | return password; 40 | } 41 | 42 | public void setPassword(String password) { 43 | this.password = password; 44 | } 45 | 46 | public String getJndiName() { 47 | return jndiName; 48 | } 49 | 50 | public void setJndiName(String jndiName) { 51 | this.jndiName = jndiName; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/spring_boot_3_3_3/spring-boot-jndi-datasource/src/main/com/roytuts/spring/boot/jndi/datasource/entity/Company.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.jndi.datasource.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | import jakarta.persistence.Column; 6 | import jakarta.persistence.Entity; 7 | import jakarta.persistence.GeneratedValue; 8 | import jakarta.persistence.GenerationType; 9 | import jakarta.persistence.Id; 10 | import jakarta.persistence.Table; 11 | 12 | @Entity 13 | @Table(name = "company") 14 | public class Company implements Serializable { 15 | 16 | private static final long serialVersionUID = 1L; 17 | 18 | @Id 19 | @Column(name = "id") 20 | @GeneratedValue(strategy = GenerationType.IDENTITY) 21 | private long id; 22 | 23 | @Column(name = "name") 24 | private String name; 25 | 26 | public long getId() { 27 | return id; 28 | } 29 | 30 | public void setId(long id) { 31 | this.id = id; 32 | } 33 | 34 | public String getName() { 35 | return name; 36 | } 37 | 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/spring_boot_3_3_3/spring-boot-jndi-datasource/src/main/com/roytuts/spring/boot/jndi/datasource/repository/CompanyRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.jndi.datasource.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.boot.jndi.datasource.entity.Company; 6 | 7 | public interface CompanyRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/spring_boot_3_3_3/spring-boot-jndi-datasource/src/main/com/roytuts/spring/boot/jndi/datasource/rest/controller/CompanyRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.jndi.datasource.rest.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import com.roytuts.spring.boot.jndi.datasource.entity.Company; 12 | import com.roytuts.spring.boot.jndi.datasource.service.CompanyService; 13 | 14 | @RestController 15 | public class CompanyRestController { 16 | 17 | @Autowired 18 | private CompanyService companyService; 19 | 20 | @GetMapping("/company") 21 | public ResponseEntity> getCompanyList() { 22 | return new ResponseEntity>(companyService.getCompanyList(), HttpStatus.OK); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/spring_boot_3_3_3/spring-boot-jndi-datasource/src/main/com/roytuts/spring/boot/jndi/datasource/service/CompanyService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.jndi.datasource.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.roytuts.spring.boot.jndi.datasource.entity.Company; 9 | import com.roytuts.spring.boot.jndi.datasource.repository.CompanyRepository; 10 | 11 | @Service 12 | public class CompanyService { 13 | 14 | @Autowired 15 | private CompanyRepository companyRepository; 16 | 17 | public List getCompanyList() { 18 | return companyRepository.findAll(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/spring_boot_3_3_3/spring-boot-jndi-datasource/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #datasource 2 | app.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | app.datasource.url=jdbc:mysql://localhost/roytuts 4 | app.datasource.username=root 5 | app.datasource.password=root 6 | app.datasource.jndiName=jdbc/myDataSource 7 | 8 | #disable schema generation from Hibernate 9 | spring.jpa.hibernate.ddl-auto=none 10 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/src/main/java/com/roytuts/spring/boot/jndi/datasource/JndiDatasourceApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.jndi.datasource; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication(scanBasePackages = "com.roytuts.spring.boot.jndi.datasource") 7 | public class JndiDatasourceApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(JndiDatasourceApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/src/main/java/com/roytuts/spring/boot/jndi/datasource/config/DatabaseProperties.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.jndi.datasource.config; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | 5 | @ConfigurationProperties(prefix = "spring.datasource") 6 | public class DatabaseProperties { 7 | 8 | String url; 9 | String username; 10 | String password; 11 | String driverClassName; 12 | String jndiName; 13 | 14 | public String getUrl() { 15 | return url; 16 | } 17 | 18 | public void setUrl(String url) { 19 | this.url = url; 20 | } 21 | 22 | public String getUsername() { 23 | return username; 24 | } 25 | 26 | public void setUsername(String username) { 27 | this.username = username; 28 | } 29 | 30 | public String getPassword() { 31 | return password; 32 | } 33 | 34 | public void setPassword(String password) { 35 | this.password = password; 36 | } 37 | 38 | public String getDriverClassName() { 39 | return driverClassName; 40 | } 41 | 42 | public void setDriverClassName(String driverClassName) { 43 | this.driverClassName = driverClassName; 44 | } 45 | 46 | public String getJndiName() { 47 | return jndiName; 48 | } 49 | 50 | public void setJndiName(String jndiName) { 51 | this.jndiName = jndiName; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/src/main/java/com/roytuts/spring/boot/jndi/datasource/entity/Company.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.jndi.datasource.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.Table; 11 | 12 | @Entity 13 | @Table(name = "company") 14 | public class Company implements Serializable { 15 | 16 | private static final long serialVersionUID = 1L; 17 | 18 | @Id 19 | @Column(name = "id") 20 | @GeneratedValue(strategy = GenerationType.IDENTITY) 21 | private long id; 22 | 23 | @Column(name = "name") 24 | private String name; 25 | 26 | public long getId() { 27 | return id; 28 | } 29 | 30 | public void setId(long id) { 31 | this.id = id; 32 | } 33 | 34 | public String getName() { 35 | return name; 36 | } 37 | 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/src/main/java/com/roytuts/spring/boot/jndi/datasource/repository/CompanyRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.jndi.datasource.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.boot.jndi.datasource.entity.Company; 6 | 7 | public interface CompanyRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/src/main/java/com/roytuts/spring/boot/jndi/datasource/rest/controller/CompanyRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.jndi.datasource.rest.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import com.roytuts.spring.boot.jndi.datasource.entity.Company; 12 | import com.roytuts.spring.boot.jndi.datasource.service.CompanyService; 13 | 14 | @RestController 15 | public class CompanyRestController { 16 | 17 | @Autowired 18 | private CompanyService companyService; 19 | 20 | @GetMapping("/company") 21 | public ResponseEntity> getCompanyList() { 22 | return new ResponseEntity>(companyService.getCompanyList(), HttpStatus.OK); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/src/main/java/com/roytuts/spring/boot/jndi/datasource/service/CompanyService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.jndi.datasource.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.roytuts.spring.boot.jndi.datasource.entity.Company; 9 | import com.roytuts.spring.boot.jndi.datasource.repository.CompanyRepository; 10 | 11 | @Service 12 | public class CompanyService { 13 | 14 | @Autowired 15 | private CompanyRepository companyRepository; 16 | 17 | public List getCompanyList() { 18 | return companyRepository.findAll(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-boot-jndi-datasource/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | spring.datasource.jndiName=jdbc/myDataSource 7 | 8 | #disable schema generation from Hibernate 9 | spring.jpa.hibernate.ddl-auto=none -------------------------------------------------------------------------------- /spring-data-jpa-audit/employee.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `employee` ( 2 | `id` int unsigned NOT NULL AUTO_INCREMENT, 3 | `name` varchar(50) NOT NULL, 4 | `email` varchar(150) NOT NULL, 5 | `address` varchar(255) NOT NULL, 6 | `created_by` varchar(50) NOT NULL, 7 | `created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 8 | `updated_by` varchar(50) DEFAULT NULL, 9 | `updated_date` timestamp NULL DEFAULT NULL, 10 | PRIMARY KEY (`id`) 11 | ) ENGINE=InnoDB AUTO_INCREMENT=7 COLLATE='utf8mb4_unicode_ci' DEFAULT CHARSET=UTF8MB4; 12 | -------------------------------------------------------------------------------- /spring-data-jpa-audit/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-data-jpa-audit 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.2.2 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-data-jpa 33 | 34 | 35 | 36 | com.mysql 37 | mysql-connector-j 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-data-jpa-audit/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-data-jpa-entity-auditing-using-entitylisteners/ 2 | -------------------------------------------------------------------------------- /spring-data-jpa-audit/src/main/java/com/roytuts/spring/data/jpa/audit/App.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.audit; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.domain.EntityScan; 6 | import org.springframework.data.jpa.repository.config.EnableJpaAuditing; 7 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 8 | 9 | @SpringBootApplication 10 | @EnableJpaAuditing(auditorAwareRef = "auditorAwareImpl") 11 | @EntityScan(basePackages = "com.roytuts.spring.data.jpa.audit.entity") 12 | @EnableJpaRepositories(basePackages = "com.roytuts.spring.data.jpa.audit.repository") 13 | public class App { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(App.class, args); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /spring-data-jpa-audit/src/main/java/com/roytuts/spring/data/jpa/audit/domain/AuditorAwareImpl.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.audit.domain; 2 | 3 | import java.util.Optional; 4 | 5 | import org.springframework.data.domain.AuditorAware; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | public class AuditorAwareImpl implements AuditorAware { 10 | 11 | @Override 12 | public Optional getCurrentAuditor() { 13 | return Optional.of("Admin"); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-data-jpa-audit/src/main/java/com/roytuts/spring/data/jpa/audit/repository/EmployeeJpaRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.audit.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.data.jpa.audit.entity.Employee; 6 | 7 | public interface EmployeeJpaRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-data-jpa-audit/src/main/java/com/roytuts/spring/data/jpa/audit/rest/controller/EmployeeRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.audit.rest.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.PostMapping; 10 | import org.springframework.web.bind.annotation.RequestBody; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | import com.roytuts.spring.data.jpa.audit.service.EmployeeService; 14 | import com.roytuts.spring.data.jpa.audit.vo.EmployeeVo; 15 | 16 | @RestController 17 | public class EmployeeRestController { 18 | 19 | @Autowired 20 | private EmployeeService employeeService; 21 | 22 | @PostMapping("/employee/save") 23 | public ResponseEntity saveOrUpdateEmployee(@RequestBody EmployeeVo employee) { 24 | employeeService.saveOrUpdateEmployee(employee); 25 | 26 | return new ResponseEntity<>(HttpStatus.OK); 27 | } 28 | 29 | @GetMapping("/employees") 30 | public ResponseEntity> getEmployees() { 31 | List employees = employeeService.getEmployees(); 32 | 33 | return new ResponseEntity<>(employees, HttpStatus.OK); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /spring-data-jpa-audit/src/main/java/com/roytuts/spring/data/jpa/audit/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.audit.service; 2 | 3 | import java.util.List; 4 | import java.util.stream.Collectors; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import com.roytuts.spring.data.jpa.audit.entity.Employee; 10 | import com.roytuts.spring.data.jpa.audit.repository.EmployeeJpaRepository; 11 | import com.roytuts.spring.data.jpa.audit.vo.EmployeeVo; 12 | 13 | @Service 14 | public class EmployeeService { 15 | 16 | @Autowired 17 | private EmployeeJpaRepository employeeJpaRepository; 18 | 19 | public void saveOrUpdateEmployee(EmployeeVo employee) { 20 | Employee emp = null; 21 | 22 | if (employee.getId() != null) { 23 | emp = employeeJpaRepository.findById(employee.getId()).get(); 24 | 25 | if (emp == null) { 26 | return; 27 | } 28 | } else { 29 | emp = new Employee(); 30 | } 31 | 32 | emp.setName(employee.getName()); 33 | emp.setEmail(employee.getEmail()); 34 | emp.setAddress(employee.getAddress()); 35 | 36 | employeeJpaRepository.save(emp); 37 | } 38 | 39 | public List getEmployees() { 40 | List employees = employeeJpaRepository.findAll(); 41 | 42 | return employees.stream().map(e -> { 43 | EmployeeVo employeeVo = new EmployeeVo(); 44 | 45 | employeeVo.setId(e.getEmpId()); 46 | employeeVo.setName(e.getName()); 47 | employeeVo.setEmail(e.getEmail()); 48 | employeeVo.setAddress(e.getAddress()); 49 | 50 | return employeeVo; 51 | }).collect(Collectors.toList()); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /spring-data-jpa-audit/src/main/java/com/roytuts/spring/data/jpa/audit/vo/EmployeeVo.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.audit.vo; 2 | 3 | public class EmployeeVo { 4 | 5 | private Integer id; 6 | private String name; 7 | private String email; 8 | private String address; 9 | 10 | public Integer getId() { 11 | return id; 12 | } 13 | 14 | public void setId(Integer id) { 15 | this.id = id; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | public String getEmail() { 27 | return email; 28 | } 29 | 30 | public void setEmail(String email) { 31 | this.email = email; 32 | } 33 | 34 | public String getAddress() { 35 | return address; 36 | } 37 | 38 | public void setAddress(String address) { 39 | this.address = address; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring-data-jpa-audit/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | 7 | #disable schema generation from Hibernate 8 | spring.jpa.hibernate.ddl-auto=none 9 | -------------------------------------------------------------------------------- /spring-data-jpa-batch-insertion/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.2.6.RELEASE' to 2.4.3 4 | } 5 | 6 | repositories { 7 | mavenLocal() 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 13 | } 14 | } 15 | 16 | plugins { 17 | id 'java-library' 18 | id 'org.springframework.boot' version "${springBootVersion}" 19 | } 20 | 21 | sourceCompatibility = 12 22 | targetCompatibility = 12 23 | 24 | repositories { 25 | mavenLocal() 26 | mavenCentral() 27 | } 28 | 29 | dependencies { 30 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 31 | implementation("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 32 | implementation('mysql:mysql-connector-java:8.0.17') //8.0.22 33 | 34 | //required for jdk 9 or above 35 | runtimeOnly('javax.xml.bind:jaxb-api:2.4.0-b180830.0359') 36 | } -------------------------------------------------------------------------------- /spring-data-jpa-batch-insertion/employee.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `employee` ( 2 | `id` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL AUTO_INCREMENT, 3 | `name` varchar(40) COLLATE utf8mb4_unicode_ci NOT NULL, 4 | PRIMARY KEY (`id`) 5 | ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -------------------------------------------------------------------------------- /spring-data-jpa-batch-insertion/readme.rst: -------------------------------------------------------------------------------- 1 | You can go through the tutorial https://roytuts.com/spring-data-jpa-batch-insertion/ 2 | -------------------------------------------------------------------------------- /spring-data-jpa-batch-insertion/src/main/java/com/roytuts/spring/data/jpa/batch/insertion/SpringDataJpabatchInsertionApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.batch.insertion; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringDataJpabatchInsertionApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringDataJpabatchInsertionApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-data-jpa-batch-insertion/src/main/java/com/roytuts/spring/data/jpa/batch/insertion/controller/EmployeeRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.batch.insertion.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.PostMapping; 9 | import org.springframework.web.bind.annotation.RequestBody; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import com.roytuts.spring.data.jpa.batch.insertion.entity.Employee; 13 | import com.roytuts.spring.data.jpa.batch.insertion.service.EmployeeService; 14 | 15 | @RestController 16 | public class EmployeeRestController { 17 | 18 | @Autowired 19 | private EmployeeService employeeService; 20 | 21 | @PostMapping("/employees/save") 22 | public ResponseEntity saveEmployees(@RequestBody List employees) { 23 | employeeService.saveEmployees(employees); 24 | return new ResponseEntity(HttpStatus.OK); 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /spring-data-jpa-batch-insertion/src/main/java/com/roytuts/spring/data/jpa/batch/insertion/entity/Employee.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.batch.insertion.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.Table; 11 | 12 | @Entity 13 | @Table(name = "employee") 14 | public class Employee implements Serializable { 15 | 16 | private static final long serialVersionUID = 1L; 17 | 18 | @Id 19 | @Column(name = "id") 20 | @GeneratedValue(strategy = GenerationType.IDENTITY) 21 | private long id; 22 | 23 | @Column(name = "name") 24 | private String name; 25 | 26 | public long getId() { 27 | return id; 28 | } 29 | 30 | public void setId(long id) { 31 | this.id = id; 32 | } 33 | 34 | public String getName() { 35 | return name; 36 | } 37 | 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /spring-data-jpa-batch-insertion/src/main/java/com/roytuts/spring/data/jpa/batch/insertion/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.batch.insertion.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.data.jpa.batch.insertion.entity.Employee; 6 | 7 | public interface EmployeeRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-data-jpa-batch-insertion/src/main/java/com/roytuts/spring/data/jpa/batch/insertion/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.batch.insertion.service; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.roytuts.spring.data.jpa.batch.insertion.entity.Employee; 11 | import com.roytuts.spring.data.jpa.batch.insertion.repository.EmployeeRepository; 12 | 13 | @Service 14 | public class EmployeeService { 15 | 16 | @Autowired 17 | private EmployeeRepository employeeRepository; 18 | 19 | @Transactional 20 | public void saveEmployees(List employees) { 21 | int size = employees.size(); 22 | int counter = 0; 23 | 24 | List temp = new ArrayList<>(); 25 | 26 | for (Employee emp : employees) { 27 | temp.add(emp); 28 | 29 | if ((counter + 1) % 500 == 0 || (counter + 1) == size) { 30 | employeeRepository.saveAll(temp); 31 | temp.clear(); 32 | } 33 | 34 | counter++; 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /spring-data-jpa-batch-insertion/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | 7 | #server.port=9999 8 | 9 | #disable schema generation from Hibernate 10 | spring.jpa.hibernate.ddl-auto=none -------------------------------------------------------------------------------- /spring-data-jpa-criteriadelete/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.roytuts 6 | spring-data-jpa-criteriadelete 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | UTF-8 12 | 8 13 | 8 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.5.6 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-jpa 31 | 32 | 33 | 34 | mysql 35 | mysql-connector-java 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-maven-plugin 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriadelete/pom.xml_3_1_3: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-data-jpa-criteriadelete 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | org.springframework.boot 14 | spring-boot-starter-parent 15 | 3.1.3 16 | 17 | 18 | 19 | UTF-8 20 | 19 21 | 19 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-data-jpa 33 | 34 | 35 | 36 | com.mysql 37 | mysql-connector-j 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriadelete/product.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `product` ( 2 | `id` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL AUTO_INCREMENT, 3 | `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, 4 | `price` double COLLATE utf8mb4_unicode_ci NOT NULL, 5 | `sale_price` double COLLATE utf8mb4_unicode_ci NOT NULL, 6 | `sales_count` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL, 7 | `sale_date` VARCHAR(20) NOT NULL, 8 | PRIMARY KEY (`id`) 9 | ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=UTF8MB4_UNICODE_CI; 10 | 11 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(1, 'Desktop','30000','35000','55','02-10-2021'); 12 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(2, 'Desktop','30300','37030','43','03-10-2021'); 13 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(3, 'Tablet','39010','48700','145','10-10-2021'); 14 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(4, 'Phone','15000','17505','251','05-10-2021'); 15 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(5, 'Phone','18000','22080','178','05-10-2021'); 16 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(6, 'Tablet','30500','34100','58','05-10-2021'); 17 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(7, 'Adapter','2000','2500','68','06-10-2021'); 18 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(8, 'TV','45871','55894','165','07-10-2021'); 19 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriadelete/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/bulk-delete-using-spring-jpa-criteriadelete/ -------------------------------------------------------------------------------- /spring-data-jpa-criteriadelete/src/main/java/com/roytuts/spring/data/jpa/criteriadelete/CriteriaDeleteApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.criteriadelete; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.CommandLineRunner; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.boot.autoconfigure.domain.EntityScan; 8 | 9 | import com.roytuts.spring.data.jpa.criteriadelete.service.DeleteService; 10 | 11 | @SpringBootApplication 12 | @EntityScan(basePackages = "com.roytuts.spring.data.jpa.criteriadelete.entity") 13 | public class CriteriaDeleteApp implements CommandLineRunner { 14 | 15 | @Autowired 16 | private DeleteService deleteService; 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(CriteriaDeleteApp.class, args); 20 | } 21 | 22 | @Override 23 | public void run(String... args) throws Exception { 24 | System.out.println("CriteriaDelete::Bulk Delete Started..."); 25 | deleteService.deleteBulkProducts("Desktop"); 26 | System.out.println("CriteriaDelete::Bulk Delete Done."); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriadelete/src/main/java/com/roytuts/spring/data/jpa/criteriadelete/CriteriaDeleteApp.java_3_1_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.criteriadelete; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.CommandLineRunner; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.boot.autoconfigure.domain.EntityScan; 8 | 9 | import com.roytuts.spring.data.jpa.criteriadelete.service.DeleteService; 10 | 11 | @EntityScan 12 | @SpringBootApplication 13 | public class CriteriaDeleteApp implements CommandLineRunner { 14 | 15 | @Autowired 16 | private DeleteService deleteService; 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(CriteriaDeleteApp.class, args); 20 | } 21 | 22 | @Override 23 | public void run(String... args) throws Exception { 24 | System.out.println("CriteriaDelete::Bulk Delete Started..."); 25 | 26 | deleteService.deleteBulkProducts("Desktop"); 27 | 28 | System.out.println("CriteriaDelete::Bulk Delete Done."); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriadelete/src/main/java/com/roytuts/spring/data/jpa/criteriadelete/entity/Product.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.criteriadelete.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | 10 | @Entity 11 | @Table(name = "product") 12 | public class Product { 13 | 14 | @Id 15 | @GeneratedValue(strategy = GenerationType.IDENTITY) 16 | private int id; 17 | @Column 18 | private String name; 19 | @Column 20 | private double price; 21 | @Column 22 | private double salePrice; 23 | @Column 24 | private int salesCount; 25 | @Column 26 | private String saleDate; 27 | 28 | public int getId() { 29 | return id; 30 | } 31 | 32 | public void setId(int id) { 33 | this.id = id; 34 | } 35 | 36 | public String getName() { 37 | return name; 38 | } 39 | 40 | public void setName(String name) { 41 | this.name = name; 42 | } 43 | 44 | public double getPrice() { 45 | return price; 46 | } 47 | 48 | public void setPrice(double price) { 49 | this.price = price; 50 | } 51 | 52 | public double getSalePrice() { 53 | return salePrice; 54 | } 55 | 56 | public void setSalePrice(double salePrice) { 57 | this.salePrice = salePrice; 58 | } 59 | 60 | public int getSalesCount() { 61 | return salesCount; 62 | } 63 | 64 | public void setSalesCount(int salesCount) { 65 | this.salesCount = salesCount; 66 | } 67 | 68 | public String getSaleDate() { 69 | return saleDate; 70 | } 71 | 72 | public void setSaleDate(String saleDate) { 73 | this.saleDate = saleDate; 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriadelete/src/main/java/com/roytuts/spring/data/jpa/criteriadelete/service/DeleteService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.criteriadelete.service; 2 | 3 | import javax.persistence.EntityManager; 4 | import javax.persistence.PersistenceContext; 5 | import javax.persistence.criteria.CriteriaBuilder; 6 | import javax.persistence.criteria.CriteriaDelete; 7 | import javax.persistence.criteria.Root; 8 | 9 | import org.springframework.stereotype.Service; 10 | import org.springframework.transaction.annotation.Transactional; 11 | 12 | import com.roytuts.spring.data.jpa.criteriadelete.entity.Product; 13 | 14 | @Service 15 | public class DeleteService { 16 | 17 | @PersistenceContext 18 | private EntityManager entityManager; 19 | 20 | @Transactional 21 | public void deleteBulkProducts(final String productName) { 22 | CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); 23 | 24 | CriteriaDelete criteriaDelete = criteriaBuilder.createCriteriaDelete(Product.class); 25 | 26 | Root root = criteriaDelete.from(Product.class); 27 | 28 | criteriaDelete.where(criteriaBuilder.equal(root.get("name"), productName)); 29 | 30 | entityManager.createQuery(criteriaDelete).executeUpdate(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriadelete/src/main/java/com/roytuts/spring/data/jpa/criteriadelete/service/DeleteService.java_3_1_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.criteriadelete.service; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | import com.roytuts.spring.data.jpa.criteriadelete.entity.Product; 6 | 7 | import jakarta.persistence.EntityManager; 8 | import jakarta.persistence.PersistenceContext; 9 | import jakarta.persistence.criteria.CriteriaBuilder; 10 | import jakarta.persistence.criteria.CriteriaDelete; 11 | import jakarta.persistence.criteria.Root; 12 | import jakarta.transaction.Transactional; 13 | 14 | @Service 15 | public class DeleteService { 16 | 17 | @PersistenceContext 18 | private EntityManager entityManager; 19 | 20 | @Transactional 21 | public void deleteBulkProducts(final String productName) { 22 | CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); 23 | 24 | CriteriaDelete criteriaDelete = criteriaBuilder.createCriteriaDelete(Product.class); 25 | 26 | Root root = criteriaDelete.from(Product.class); 27 | 28 | criteriaDelete.where(criteriaBuilder.equal(root.get("name"), productName)); 29 | 30 | entityManager.createQuery(criteriaDelete).executeUpdate(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriadelete/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 2 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | 6 | spring.jpa.hibernate.ddl-auto=none 7 | 8 | spring.jpa.show-sql=true 9 | spring.jpa.properties.hibernate.format_sql=true 10 | logging.level.org.hibernate.type.descriptor.sql=trace -------------------------------------------------------------------------------- /spring-data-jpa-criteriaupdate/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.roytuts 6 | spring-data-jpa-criteriaupdate 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | UTF-8 12 | 8 13 | 8 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.5.6 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-jpa 31 | 32 | 33 | 34 | mysql 35 | mysql-connector-java 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-maven-plugin 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriaupdate/pom.xml_3_1_3: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-data-jpa-criteriaupdate 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | org.springframework.boot 14 | spring-boot-starter-parent 15 | 3.1.3 16 | 17 | 18 | 19 | UTF-8 20 | 19 21 | 19 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-data-jpa 33 | 34 | 35 | 36 | com.mysql 37 | mysql-connector-j 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriaupdate/product.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `product` ( 2 | `id` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL AUTO_INCREMENT, 3 | `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, 4 | `price` double COLLATE utf8mb4_unicode_ci NOT NULL, 5 | `sale_price` double COLLATE utf8mb4_unicode_ci NOT NULL, 6 | `sales_count` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL, 7 | `sale_date` VARCHAR(20) NOT NULL, 8 | PRIMARY KEY (`id`) 9 | ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=UTF8MB4_UNICODE_CI; 10 | 11 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(1, 'Desktop','30000','35000','55','02-10-2021'); 12 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(2, 'Desktop','30300','37030','43','03-10-2021'); 13 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(3, 'Tablet','39010','48700','145','10-10-2021'); 14 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(4, 'Phone','15000','17505','251','05-10-2021'); 15 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(5, 'Phone','18000','22080','178','05-10-2021'); 16 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(6, 'Tablet','30500','34100','58','05-10-2021'); 17 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(7, 'Adapter','2000','2500','68','06-10-2021'); 18 | insert into product(id,name,price,sale_price,sales_count,sale_date) values(8, 'TV','45871','55894','165','07-10-2021'); 19 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriaupdate/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/bulk-update-using-spring-jpa-criteriaupdate/ -------------------------------------------------------------------------------- /spring-data-jpa-criteriaupdate/src/main/java/com/roytuts/spring/data/jpa/criteriaupdate/CriteriaUpdateApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.criteriaupdate; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.CommandLineRunner; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.boot.autoconfigure.domain.EntityScan; 8 | 9 | import com.roytuts.spring.data.jpa.criteriaupdate.service.UpdateService; 10 | 11 | @SpringBootApplication 12 | @EntityScan(basePackages = "com.roytuts.spring.data.jpa.criteriaupdate.entity") 13 | public class CriteriaUpdateApp implements CommandLineRunner { 14 | 15 | @Autowired 16 | private UpdateService updateService; 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(CriteriaUpdateApp.class, args); 20 | } 21 | 22 | @Override 23 | public void run(String... args) throws Exception { 24 | System.out.println("CriteriaUpdate::Bulk Update Started..."); 25 | updateService.updateBulkProducts("Desktop", 31000.50); 26 | System.out.println("CriteriaUpdate::Bulk Update Done."); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriaupdate/src/main/java/com/roytuts/spring/data/jpa/criteriaupdate/CriteriaUpdateApp.java_3_1_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.criteriaupdate; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.CommandLineRunner; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.boot.autoconfigure.domain.EntityScan; 8 | 9 | import com.roytuts.spring.data.jpa.criteriaupdate.service.UpdateService; 10 | 11 | @EntityScan 12 | @SpringBootApplication 13 | public class CriteriaUpdateApp implements CommandLineRunner { 14 | 15 | @Autowired 16 | private UpdateService updateService; 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(CriteriaUpdateApp.class, args); 20 | } 21 | 22 | @Override 23 | public void run(String... args) throws Exception { 24 | System.out.println("CriteriaUpdate::Bulk Update Started..."); 25 | 26 | updateService.updateBulkProducts("Desktop", 31000.50); 27 | 28 | System.out.println("CriteriaUpdate::Bulk Update Done."); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriaupdate/src/main/java/com/roytuts/spring/data/jpa/criteriaupdate/service/UpdateService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.criteriaupdate.service; 2 | 3 | import javax.persistence.EntityManager; 4 | import javax.persistence.PersistenceContext; 5 | import javax.persistence.criteria.CriteriaBuilder; 6 | import javax.persistence.criteria.CriteriaUpdate; 7 | import javax.persistence.criteria.Root; 8 | 9 | import org.springframework.stereotype.Service; 10 | import org.springframework.transaction.annotation.Transactional; 11 | 12 | import com.roytuts.spring.data.jpa.criteriaupdate.entity.Product; 13 | 14 | @Service 15 | public class UpdateService { 16 | 17 | @PersistenceContext 18 | private EntityManager entityManager; 19 | 20 | @Transactional 21 | public void updateBulkProducts(final String productName, final double price) { 22 | CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); 23 | 24 | CriteriaUpdate criteriaUpdate = criteriaBuilder.createCriteriaUpdate(Product.class); 25 | 26 | Root root = criteriaUpdate.from(Product.class); 27 | 28 | criteriaUpdate.set("price", price); 29 | criteriaUpdate.where(criteriaBuilder.equal(root.get("name"), productName)); 30 | 31 | entityManager.createQuery(criteriaUpdate).executeUpdate(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriaupdate/src/main/java/com/roytuts/spring/data/jpa/criteriaupdate/service/UpdateService.java_3_1_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.criteriaupdate.service; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | import com.roytuts.spring.data.jpa.criteriaupdate.entity.Product; 6 | 7 | import jakarta.persistence.EntityManager; 8 | import jakarta.persistence.PersistenceContext; 9 | import jakarta.persistence.criteria.CriteriaBuilder; 10 | import jakarta.persistence.criteria.CriteriaUpdate; 11 | import jakarta.persistence.criteria.Root; 12 | import jakarta.transaction.Transactional; 13 | 14 | @Service 15 | public class UpdateService { 16 | 17 | @PersistenceContext 18 | private EntityManager entityManager; 19 | 20 | @Transactional 21 | public void updateBulkProducts(final String productName, final double price) { 22 | CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); 23 | 24 | CriteriaUpdate criteriaUpdate = criteriaBuilder.createCriteriaUpdate(Product.class); 25 | 26 | Root root = criteriaUpdate.from(Product.class); 27 | 28 | criteriaUpdate.set("price", price); 29 | criteriaUpdate.where(criteriaBuilder.equal(root.get("name"), productName)); 30 | 31 | entityManager.createQuery(criteriaUpdate).executeUpdate(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /spring-data-jpa-criteriaupdate/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 2 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | 6 | spring.jpa.hibernate.ddl-auto=none 7 | 8 | spring.jpa.show-sql=true 9 | spring.jpa.properties.hibernate.format_sql=true 10 | logging.level.org.hibernate.type.descriptor.sql=trace -------------------------------------------------------------------------------- /spring-data-jpa-crud/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java-library' 3 | } 4 | 5 | repositories { 6 | jcenter() 7 | mavenCentral() 8 | } 9 | 10 | dependencies { 11 | implementation 'org.springframework:spring-core:5.2.8.RELEASE' 12 | implementation 'org.springframework:spring-context:5.2.8.RELEASE' 13 | implementation 'org.springframework.data:spring-data-jpa:2.3.3.RELEASE' 14 | implementation 'org.hsqldb:hsqldb:2.5.1' 15 | implementation 'org.hibernate.orm:hibernate-core:6.0.0.Alpha5' 16 | } 17 | -------------------------------------------------------------------------------- /spring-data-jpa-crud/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-data-jpa-crud-example/ 2 | -------------------------------------------------------------------------------- /spring-data-jpa-crud/src/main/java/com/roytuts/spring/data/jpa/crud/repository/ProductRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.crud.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.jpa.repository.Modifying; 5 | import org.springframework.data.jpa.repository.Query; 6 | import org.springframework.data.repository.query.Param; 7 | import org.springframework.transaction.annotation.Transactional; 8 | 9 | import com.roytuts.spring.data.jpa.crud.entity.Product; 10 | 11 | public interface ProductRepository extends JpaRepository { 12 | 13 | @Query("select p from Product p where p.productName = ?1") 14 | Product findByProductName(String productName); 15 | 16 | @Modifying 17 | @Transactional 18 | @Query("UPDATE Product p SET p.productName = ?1, p.productDesc = ?2, p.productPrice = ?3" 19 | + " WHERE p.productId = ?4") 20 | void updateProduct(String name, String desc, Double price, Integer id); 21 | 22 | @Modifying 23 | @Transactional 24 | @Query("DELETE FROM Product p WHERE p.productId = :id") 25 | void deleteProduct(@Param("id") Integer id); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /spring-data-jpa-crud/src/main/java/com/roytuts/spring/data/jpa/crud/repository/ProductTypeRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.crud.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.jpa.repository.Query; 5 | 6 | import com.roytuts.spring.data.jpa.crud.entity.ProductType; 7 | 8 | public interface ProductTypeRepository extends JpaRepository { 9 | 10 | @Query("select pt from ProductType pt where pt.productTypeName = ?1") 11 | ProductType findByProductTypeName(String productTypeName); 12 | 13 | // if you want case insensitive 14 | @Query("select pt from ProductType pt where upper(pt.productTypeName) = upper(?1)") 15 | ProductType findByProductTypeNameCaseInsensitive(String productTypeName); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-data-jpa-crud/src/main/java/com/roytuts/spring/data/jpa/crud/service/ProductService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.crud.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | import org.springframework.transaction.annotation.Transactional; 8 | 9 | import com.roytuts.spring.data.jpa.crud.entity.Product; 10 | import com.roytuts.spring.data.jpa.crud.repository.ProductRepository; 11 | 12 | @Service 13 | public class ProductService { 14 | 15 | @Autowired 16 | private ProductRepository productRepository; 17 | 18 | public List getAllProducts() { 19 | List products = productRepository.findAll(); 20 | return products; 21 | } 22 | 23 | public Product findProductById(int id) { 24 | return productRepository.findById(id).get(); 25 | } 26 | 27 | public Product findProductByName(String productName) { 28 | return productRepository.findByProductName(productName); 29 | } 30 | 31 | @Transactional 32 | public void saveProduct(Product product) { 33 | productRepository.save(product); 34 | } 35 | 36 | @Transactional 37 | public void updateProduct(Product product) { 38 | productRepository.updateProduct(product.getProductName(), product.getProductDesc(), product.getProductPrice(), 39 | product.getProductId()); 40 | } 41 | 42 | @Transactional 43 | public void deleteProduct(Product product) { 44 | productRepository.deleteProduct(product.getProductId()); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-data-jpa-crud/src/main/java/com/roytuts/spring/data/jpa/crud/service/ProductTypeService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.crud.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | import org.springframework.transaction.annotation.Transactional; 8 | 9 | import com.roytuts.spring.data.jpa.crud.entity.ProductType; 10 | import com.roytuts.spring.data.jpa.crud.repository.ProductTypeRepository; 11 | 12 | @Service 13 | public class ProductTypeService { 14 | 15 | @Autowired 16 | private ProductTypeRepository productTypeRepository; 17 | 18 | public List getAllProductTypes() { 19 | return productTypeRepository.findAll(); 20 | } 21 | 22 | public ProductType findProductTypeById(int id) { 23 | return productTypeRepository.findById(id).get(); 24 | } 25 | 26 | public ProductType findProductTypeByName(String productTypeName) { 27 | return productTypeRepository.findByProductTypeName(productTypeName); 28 | } 29 | 30 | public ProductType findProductTypeByNameIgnoreCase(String productTypeName) { 31 | return productTypeRepository.findByProductTypeNameCaseInsensitive(productTypeName); 32 | } 33 | 34 | @Transactional 35 | public void saveProductType(ProductType productType) { 36 | productTypeRepository.save(productType); 37 | } 38 | 39 | @Transactional 40 | public void updateProductType(ProductType productType) { 41 | productTypeRepository.save(productType); 42 | } 43 | 44 | @Transactional 45 | public void deleteProductType(ProductType productType) { 46 | productTypeRepository.delete(productType); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /spring-data-jpa-crud/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO ProductType(ProductTypeName, ProductTypeDesc) VALUES('Mobile','All kind of Mobile Products') 2 | INSERT INTO ProductType(ProductTypeName, ProductTypeDesc) VALUES('Tab','All kind of Tab Products') 3 | INSERT INTO ProductType(ProductTypeName, ProductTypeDesc) VALUES('Laptop','All type of Laptops') 4 | INSERT INTO ProductType(ProductTypeName, ProductTypeDesc) VALUES('Desktop','All type of Desktops') 5 | INSERT INTO Product(ProductTypeId, ProductName, ProductDesc, ProductPrice) VALUES((SELECT ProductTypeId FROM ProductType WHERE ProductTypeName = 'Mobile'), 'Samsung S7', 'Samsung S7 mobile', 30000.00) 6 | INSERT INTO Product(ProductTypeId, ProductName, ProductDesc, ProductPrice) VALUES((SELECT ProductTypeId FROM ProductType WHERE ProductTypeName = 'Tab'), 'Galaxy Tab A', 'Samsung Galaxy Tab', 32000.00) 7 | INSERT INTO Product(ProductTypeId, ProductName, ProductDesc, ProductPrice) VALUES((SELECT ProductTypeId FROM ProductType WHERE ProductTypeName = 'Laptop'), 'Lenovo Yoga', 'Lenovo Laptop', 35340.00) 8 | INSERT INTO Product(ProductTypeId, ProductName, ProductDesc, ProductPrice) VALUES((SELECT ProductTypeId FROM ProductType WHERE ProductTypeName = 'Desktop'), 'Dell Optiplex', 'Dell Desktop', 24800.00) 9 | INSERT INTO Product(ProductTypeId, ProductName, ProductDesc, ProductPrice) VALUES((SELECT ProductTypeId FROM ProductType WHERE ProductTypeName = 'Desktop'), 'Samsung 18.5 Slim', 'Samsung Desktop', 5010.00) -------------------------------------------------------------------------------- /spring-data-jpa-crud/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE ProductType IF EXISTS; 2 | CREATE TABLE ProductType ( 3 | ProductTypeId integer identity primary key, 4 | ProductTypeName varchar(50) not null, 5 | ProductTypeDesc varchar(255) not null 6 | ); 7 | DROP TABLE Product IF EXISTS; 8 | CREATE TABLE Product ( 9 | ProductId integer identity primary key, 10 | ProductTypeId integer, 11 | ProductName varchar(50) not null, 12 | ProductDesc varchar(255) not null, 13 | ProductPrice float not null 14 | ); 15 | ALTER TABLE Product 16 | ADD FOREIGN KEY (ProductTypeId) 17 | REFERENCES ProductType(ProductTypeId); -------------------------------------------------------------------------------- /spring-data-jpa-entity-graphs/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.3.2.RELEASE' 4 | } 5 | 6 | repositories { 7 | mavenCentral() 8 | } 9 | 10 | dependencies { 11 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 12 | } 13 | } 14 | 15 | plugins { 16 | id 'java-library' 17 | id 'org.springframework.boot' version "${springBootVersion}" 18 | } 19 | 20 | sourceCompatibility = 12 21 | targetCompatibility = 12 22 | 23 | repositories { 24 | mavenCentral() 25 | } 26 | 27 | dependencies { 28 | implementation "org.springframework.boot:spring-boot-starter:${springBootVersion}" 29 | implementation("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 30 | runtime("mysql:mysql-connector-java:8.0.17") 31 | 32 | //required for jdk 9 or above 33 | runtimeOnly('javax.xml.bind:jaxb-api:2.4.0-b180830.0359') 34 | } -------------------------------------------------------------------------------- /spring-data-jpa-entity-graphs/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-data-jpa-entity-graphs/ 2 | -------------------------------------------------------------------------------- /spring-data-jpa-entity-graphs/src/main/java/com/roytuts/spring/data/jpa/entity/graphs/SpringDataJpaEntityGraphApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.entity.graphs; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.CommandLineRunner; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | 8 | import com.roytuts.spring.data.jpa.entity.graphs.entity.Department; 9 | import com.roytuts.spring.data.jpa.entity.graphs.service.DepartmentService; 10 | 11 | @SpringBootApplication 12 | public class SpringDataJpaEntityGraphApp implements CommandLineRunner { 13 | 14 | @Autowired 15 | private DepartmentService departmentService; 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(SpringDataJpaEntityGraphApp.class, args); 19 | } 20 | 21 | @Override 22 | public void run(String... args) throws Exception { 23 | Department department = departmentService.getDepartmentById(10); 24 | 25 | System.out.println("Department name: " + department); 26 | department.getEmployeeCollection().stream().forEach(emp -> System.out.println(emp)); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring-data-jpa-entity-graphs/src/main/java/com/roytuts/spring/data/jpa/entity/graphs/config/Config.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.entity.graphs.config; 2 | 3 | import javax.sql.DataSource; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.context.annotation.PropertySource; 9 | import org.springframework.core.env.Environment; 10 | import org.springframework.jdbc.datasource.DriverManagerDataSource; 11 | 12 | @Configuration 13 | @PropertySource("classpath:application.properties") 14 | public class Config { 15 | 16 | @Autowired 17 | private Environment environment; 18 | 19 | @Bean 20 | public DataSource dataSource() { 21 | 22 | DriverManagerDataSource ds = new DriverManagerDataSource(); 23 | ds.setDriverClassName(environment.getRequiredProperty("spring.datasource.driverClassName")); 24 | ds.setUrl(environment.getRequiredProperty("spring.datasource.url")); 25 | ds.setUsername(environment.getRequiredProperty("spring.datasource.username")); 26 | ds.setPassword(environment.getRequiredProperty("spring.datasource.password")); 27 | return ds; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /spring-data-jpa-entity-graphs/src/main/java/com/roytuts/spring/data/jpa/entity/graphs/repository/DepartmentRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.entity.graphs.repository; 2 | 3 | import org.springframework.data.jpa.repository.EntityGraph; 4 | import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType; 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | 7 | import com.roytuts.spring.data.jpa.entity.graphs.entity.Department; 8 | 9 | public interface DepartmentRepository extends JpaRepository { 10 | 11 | @EntityGraph(value = "department.employees", type = EntityGraphType.LOAD) 12 | Department findByDeptId(int deptId); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /spring-data-jpa-entity-graphs/src/main/java/com/roytuts/spring/data/jpa/entity/graphs/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.entity.graphs.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.data.jpa.entity.graphs.entity.Employee; 6 | 7 | public interface EmployeeRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-data-jpa-entity-graphs/src/main/java/com/roytuts/spring/data/jpa/entity/graphs/service/DepartmentService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.entity.graphs.service; 2 | 3 | import javax.annotation.Resource; 4 | 5 | import org.springframework.stereotype.Service; 6 | 7 | import com.roytuts.spring.data.jpa.entity.graphs.entity.Department; 8 | import com.roytuts.spring.data.jpa.entity.graphs.repository.DepartmentRepository; 9 | 10 | @Service 11 | public class DepartmentService { 12 | 13 | @Resource 14 | private DepartmentRepository departmentRepository; 15 | 16 | public Department getDepartmentById(int deptId) { 17 | return departmentRepository.findByDeptId(deptId); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /spring-data-jpa-entity-graphs/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost/roytuts 2 | spring.datasource.username=root 3 | spring.datasource.password=root 4 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 5 | 6 | #disable schema generation from Hibernate 7 | spring.jpa.hibernate.ddl-auto=none -------------------------------------------------------------------------------- /spring-data-jpa-hibernate-usertype/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.3.2.RELEASE' to 2.4.3 4 | } 5 | 6 | repositories { 7 | mavenCentral() 8 | } 9 | 10 | dependencies { 11 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 12 | } 13 | } 14 | 15 | plugins { 16 | id 'java-library' 17 | id 'org.springframework.boot' version "${springBootVersion}" 18 | } 19 | 20 | sourceCompatibility = 12 21 | targetCompatibility = 12 22 | 23 | repositories { 24 | mavenCentral() 25 | } 26 | 27 | dependencies { 28 | implementation "org.springframework.boot:spring-boot-starter:${springBootVersion}" 29 | implementation("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 30 | runtime("mysql:mysql-connector-java:8.0.17") //to 8.0.22 31 | 32 | //required for jdk 9 or above 33 | runtimeOnly('javax.xml.bind:jaxb-api:2.4.0-b180830.0359') 34 | } -------------------------------------------------------------------------------- /spring-data-jpa-hibernate-usertype/employee.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `employee` ( 2 | `id` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL AUTO_INCREMENT, 3 | `first_name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL, 4 | `last_name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL, 5 | PRIMARY KEY (`id`) 6 | ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 7 | 8 | INSERT INTO `employee` (`id`, `first_name`, `last_name`) VALUES 9 | (1, 'Soumitra', 'Roy'), 10 | (2, 'Rahul', 'Kumar'); -------------------------------------------------------------------------------- /spring-data-jpa-hibernate-usertype/readme.rst: -------------------------------------------------------------------------------- 1 | You can read tutorial https://roytuts.com/hibernate-usertype-example-using-spring-data-jpa/ -------------------------------------------------------------------------------- /spring-data-jpa-hibernate-usertype/src/main/java/com/roytuts/spring/data/jpa/hibernate/usertype/SpringDataJpaHibernateUserTypeApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.hibernate.usertype; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.CommandLineRunner; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | 8 | import com.roytuts.spring.data.jpa.hibernate.usertype.service.EmployeeService; 9 | 10 | @SpringBootApplication 11 | public class SpringDataJpaHibernateUserTypeApp implements CommandLineRunner { 12 | 13 | @Autowired 14 | private EmployeeService employeeService; 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(SpringDataJpaHibernateUserTypeApp.class, args); 18 | } 19 | 20 | @Override 21 | public void run(String... args) throws Exception { 22 | employeeService.getEmployees().forEach(e -> System.out.println(e)); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-data-jpa-hibernate-usertype/src/main/java/com/roytuts/spring/data/jpa/hibernate/usertype/config/Config.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.hibernate.usertype.config; 2 | 3 | import javax.sql.DataSource; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.context.annotation.PropertySource; 9 | import org.springframework.core.env.Environment; 10 | import org.springframework.jdbc.datasource.DriverManagerDataSource; 11 | 12 | @Configuration 13 | @PropertySource("classpath:application.properties") 14 | public class Config { 15 | 16 | @Autowired 17 | private Environment environment; 18 | 19 | @Bean 20 | public DataSource dataSource() { 21 | 22 | DriverManagerDataSource ds = new DriverManagerDataSource(); 23 | ds.setDriverClassName(environment.getRequiredProperty("spring.datasource.driverClassName")); 24 | ds.setUrl(environment.getRequiredProperty("spring.datasource.url")); 25 | ds.setUsername(environment.getRequiredProperty("spring.datasource.username")); 26 | ds.setPassword(environment.getRequiredProperty("spring.datasource.password")); 27 | return ds; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /spring-data-jpa-hibernate-usertype/src/main/java/com/roytuts/spring/data/jpa/hibernate/usertype/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.hibernate.usertype.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.data.jpa.hibernate.usertype.entity.Employee; 6 | 7 | public interface EmployeeRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-data-jpa-hibernate-usertype/src/main/java/com/roytuts/spring/data/jpa/hibernate/usertype/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.hibernate.usertype.service; 2 | 3 | import java.util.List; 4 | 5 | import javax.annotation.Resource; 6 | 7 | import org.springframework.stereotype.Service; 8 | 9 | import com.roytuts.spring.data.jpa.hibernate.usertype.entity.Employee; 10 | import com.roytuts.spring.data.jpa.hibernate.usertype.repository.EmployeeRepository; 11 | 12 | @Service 13 | public class EmployeeService { 14 | 15 | @Resource 16 | private EmployeeRepository employeeRepository; 17 | 18 | public List getEmployees() { 19 | return employeeRepository.findAll(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-data-jpa-hibernate-usertype/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost/roytuts 2 | spring.datasource.username=root 3 | spring.datasource.password=root 4 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 5 | 6 | #disable schema generation from Hibernate 7 | spring.jpa.hibernate.ddl-auto=none -------------------------------------------------------------------------------- /spring-data-jpa-in-clause-where-table-multiple-rows-selection-deletion/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.2.6.RELEASE' 4 | } 5 | 6 | repositories { 7 | mavenLocal() 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 13 | } 14 | } 15 | 16 | plugins { 17 | id 'java-library' 18 | id 'org.springframework.boot' version "${springBootVersion}" 19 | } 20 | 21 | sourceCompatibility = 12 22 | targetCompatibility = 12 23 | 24 | repositories { 25 | mavenLocal() 26 | mavenCentral() 27 | } 28 | 29 | dependencies { 30 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 31 | implementation("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 32 | implementation('mysql:mysql-connector-java:8.0.17') 33 | 34 | //required for jdk 9 or above 35 | runtimeOnly('javax.xml.bind:jaxb-api:2.4.0-b180830.0359') 36 | } -------------------------------------------------------------------------------- /spring-data-jpa-in-clause-where-table-multiple-rows-selection-deletion/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-data-jpa-in-clause-with-where-condition/ -------------------------------------------------------------------------------- /spring-data-jpa-in-clause-where-table-multiple-rows-selection-deletion/src/main/java/com/roytuts/spring/data/jpa/table/multiple/rows/selection/deletion/SpringJpaTableMultiRowsSelectionDeletionApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.table.multiple.rows.selection.deletion; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.domain.EntityScan; 6 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 7 | import org.springframework.transaction.annotation.EnableTransactionManagement; 8 | 9 | @SpringBootApplication 10 | @EnableTransactionManagement 11 | @EntityScan(basePackages = "com.roytuts.spring.data.jpa.table.multiple.rows.selection.deletion.entity") 12 | @EnableJpaRepositories(basePackages = "com.roytuts.spring.data.jpa.table.multiple.rows.selection.deletion.repository") 13 | public class SpringJpaTableMultiRowsSelectionDeletionApp { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(SpringJpaTableMultiRowsSelectionDeletionApp.class, args); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /spring-data-jpa-in-clause-where-table-multiple-rows-selection-deletion/src/main/java/com/roytuts/spring/data/jpa/table/multiple/rows/selection/deletion/dto/Input.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.table.multiple.rows.selection.deletion.dto; 2 | 3 | import java.util.List; 4 | 5 | public class Input { 6 | 7 | private List ids; 8 | 9 | public List getIds() { 10 | return ids; 11 | } 12 | 13 | public void setIds(List ids) { 14 | this.ids = ids; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-data-jpa-in-clause-where-table-multiple-rows-selection-deletion/src/main/java/com/roytuts/spring/data/jpa/table/multiple/rows/selection/deletion/entity/Product.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.table.multiple.rows.selection.deletion.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.Table; 11 | 12 | @Entity 13 | @Table(name = "product") 14 | public class Product implements Serializable { 15 | 16 | private static final long serialVersionUID = 1L; 17 | 18 | @Id 19 | @Column(name = "id") 20 | @GeneratedValue(strategy = GenerationType.IDENTITY) 21 | private Integer id; 22 | 23 | @Column(name = "name") 24 | private String name; 25 | 26 | @Column(name = "code") 27 | private String code; 28 | 29 | @Column(name = "price") 30 | private Double price; 31 | 32 | public Integer getId() { 33 | return id; 34 | } 35 | 36 | public void setId(Integer id) { 37 | this.id = id; 38 | } 39 | 40 | public String getName() { 41 | return name; 42 | } 43 | 44 | public void setName(String name) { 45 | this.name = name; 46 | } 47 | 48 | public String getCode() { 49 | return code; 50 | } 51 | 52 | public void setCode(String code) { 53 | this.code = code; 54 | } 55 | 56 | public Double getPrice() { 57 | return price; 58 | } 59 | 60 | public void setPrice(Double price) { 61 | this.price = price; 62 | } 63 | 64 | } -------------------------------------------------------------------------------- /spring-data-jpa-in-clause-where-table-multiple-rows-selection-deletion/src/main/java/com/roytuts/spring/data/jpa/table/multiple/rows/selection/deletion/repository/ProductRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.table.multiple.rows.selection.deletion.repository; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.data.jpa.repository.Modifying; 7 | import org.springframework.data.jpa.repository.Query; 8 | import org.springframework.data.repository.query.Param; 9 | 10 | import com.roytuts.spring.data.jpa.table.multiple.rows.selection.deletion.entity.Product; 11 | 12 | public interface ProductRepository extends JpaRepository { 13 | 14 | @Query("SELECT p FROM Product p WHERE p.id IN (:ids)") 15 | public List findProductsByIds(@Param("ids") List ids); 16 | 17 | @Modifying 18 | @Query("DELETE FROM Product p WHERE p.id IN (:ids)") 19 | public void deleteProductsByIds(@Param("ids") List ids); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-data-jpa-in-clause-where-table-multiple-rows-selection-deletion/src/main/java/com/roytuts/spring/data/jpa/table/multiple/rows/selection/deletion/service/ProductService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.table.multiple.rows.selection.deletion.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | import org.springframework.transaction.annotation.Transactional; 8 | 9 | import com.roytuts.spring.data.jpa.table.multiple.rows.selection.deletion.entity.Product; 10 | import com.roytuts.spring.data.jpa.table.multiple.rows.selection.deletion.repository.ProductRepository; 11 | 12 | @Service 13 | public class ProductService { 14 | 15 | @Autowired 16 | private ProductRepository repository; 17 | 18 | public List getProducts() { 19 | return repository.findAll(); 20 | } 21 | 22 | public List getProductsByIds(List ids) { 23 | return repository.findProductsByIds(ids); 24 | } 25 | 26 | @Transactional 27 | public void deleteProducts(List ids) { 28 | repository.deleteProductsByIds(ids); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /spring-data-jpa-in-clause-where-table-multiple-rows-selection-deletion/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Spring Datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | 7 | #ignore null fields in json 8 | spring.jackson.default-property-inclusion=NON_NULL 9 | 10 | #SQL related 11 | spring.jpa.show-sql = true 12 | spring.jpa.properties.hibernate.format_sql=true 13 | logging.level.org.hibernate.type.descriptor.sql=trace 14 | 15 | #schema generation from Hibernate 16 | spring.jpa.hibernate.ddl-auto=create 17 | 18 | #load initial data from data.sql 19 | spring.datasource.initialization-mode=always 20 | spring.jpa.defer-datasource-initialization=true 21 | -------------------------------------------------------------------------------- /spring-data-jpa-in-clause-where-table-multiple-rows-selection-deletion/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO `product` (`id`, `name`, `code`, `price`) VALUES 2 | (1, 'American Tourist', 'AMTR01', 12000), 3 | (2, 'EXP Portable Hard Drive', 'USB02', 5000), 4 | (3, 'Shoes', 'SH03', 1000), 5 | (4, 'XP 1155 Intel Core Laptop', 'LPN4', 80000), 6 | (5, 'FinePix Pro2 3D Camera', '3DCAM01', 150000), 7 | (6, 'Simple Mobile', 'MB06', 3000), 8 | (7, 'Luxury Ultra thin Wrist Watch', 'WristWear03', 3000), 9 | (8, 'Headphone', 'HD08', 400), 10 | (9, 'Test 1', 'test1', 10), 11 | (10, 'Test 2', 'test2', 11), 12 | (11, 'Test 3', 'test3', 12); 13 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join-three-tables/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.2.1.RELEASE' 4 | } 5 | 6 | repositories { 7 | mavenLocal() 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 13 | } 14 | } 15 | 16 | apply plugin: 'java' 17 | apply plugin: 'org.springframework.boot' 18 | 19 | sourceCompatibility = 12 20 | targetCompatibility = 12 21 | 22 | repositories { 23 | mavenLocal() 24 | mavenCentral() 25 | } 26 | 27 | dependencies { 28 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 29 | implementation("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 30 | implementation('mysql:mysql-connector-java:8.0.17') 31 | runtimeOnly('javax.xml.bind:jaxb-api:2.4.0-b180830.0359') 32 | } -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join-three-tables/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-boot-data-jpa-left-right-inner-and-cross-join-examples-on-three-tables/ 2 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join-three-tables/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/SpringDataJpaLeftrightinnerCrossJoinApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication(scanBasePackages = "com.roytuts.spring.data.jpa.left.right.inner.cross.join") 7 | public class SpringDataJpaLeftrightinnerCrossJoinApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringDataJpaLeftrightinnerCrossJoinApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join-three-tables/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/dto/JoinDto.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto; 2 | 3 | public class JoinDto { 4 | 5 | private long saleId; 6 | private String foodName; 7 | private String companyName; 8 | private double amount; 9 | 10 | public JoinDto(long saleId, String foodName, String companyName, double amount) { 11 | this.saleId = saleId; 12 | this.foodName = foodName; 13 | this.companyName = companyName; 14 | this.amount = amount; 15 | } 16 | 17 | public long getSaleId() { 18 | return saleId; 19 | } 20 | 21 | public void setSaleId(long saleId) { 22 | this.saleId = saleId; 23 | } 24 | 25 | public String getFoodName() { 26 | return foodName; 27 | } 28 | 29 | public void setFoodName(String foodName) { 30 | this.foodName = foodName; 31 | } 32 | 33 | public String getCompanyName() { 34 | return companyName; 35 | } 36 | 37 | public void setCompanyName(String companyName) { 38 | this.companyName = companyName; 39 | } 40 | 41 | public double getAmount() { 42 | return amount; 43 | } 44 | 45 | public void setAmount(double amount) { 46 | this.amount = amount; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join-three-tables/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/entity/Company.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Set; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.FetchType; 9 | import javax.persistence.GeneratedValue; 10 | import javax.persistence.GenerationType; 11 | import javax.persistence.Id; 12 | import javax.persistence.OneToMany; 13 | import javax.persistence.Table; 14 | 15 | @Entity 16 | @Table(name = "company") 17 | public class Company implements Serializable { 18 | 19 | private static final long serialVersionUID = 1L; 20 | 21 | @Id 22 | @Column(name = "id") 23 | @GeneratedValue(strategy = GenerationType.IDENTITY) 24 | private long id; 25 | 26 | @Column(name = "name") 27 | private String name; 28 | 29 | @OneToMany(targetEntity = Food.class, mappedBy = "id", orphanRemoval = false, fetch = FetchType.LAZY) 30 | private Set foods; 31 | 32 | // getters and setters 33 | } 34 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join-three-tables/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/entity/Food.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Set; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.FetchType; 9 | import javax.persistence.GeneratedValue; 10 | import javax.persistence.GenerationType; 11 | import javax.persistence.Id; 12 | import javax.persistence.JoinColumn; 13 | import javax.persistence.ManyToOne; 14 | import javax.persistence.OneToMany; 15 | import javax.persistence.Table; 16 | 17 | import org.hibernate.annotations.Fetch; 18 | import org.hibernate.annotations.FetchMode; 19 | 20 | @Entity 21 | @Table(name = "food") 22 | public class Food implements Serializable { 23 | 24 | private static final long serialVersionUID = 1L; 25 | 26 | @Id 27 | @Column(name = "id") 28 | @GeneratedValue(strategy = GenerationType.IDENTITY) 29 | private long id; 30 | 31 | @Column(name = "name") 32 | private String name; 33 | 34 | @ManyToOne(fetch = FetchType.LAZY) 35 | @JoinColumn(name = "company_id", insertable = false, updatable = false) 36 | @Fetch(FetchMode.JOIN) 37 | private Company company; 38 | 39 | @OneToMany(targetEntity = Sale.class, mappedBy = "id", orphanRemoval = false, fetch = FetchType.LAZY) 40 | private Set sales; 41 | 42 | // getters and setters 43 | 44 | } 45 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join-three-tables/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/entity/Sale.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.FetchType; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.GenerationType; 10 | import javax.persistence.Id; 11 | import javax.persistence.JoinColumn; 12 | import javax.persistence.ManyToOne; 13 | import javax.persistence.Table; 14 | 15 | import org.hibernate.annotations.Fetch; 16 | import org.hibernate.annotations.FetchMode; 17 | 18 | @Entity 19 | @Table(name = "sale") 20 | public class Sale implements Serializable { 21 | 22 | private static final long serialVersionUID = 1L; 23 | 24 | @Id 25 | @Column(name = "id") 26 | @GeneratedValue(strategy = GenerationType.IDENTITY) 27 | private long id; 28 | 29 | @Column(name = "quantity") 30 | private int quantity; 31 | 32 | @Column(name = "rate") 33 | private double rate; 34 | 35 | @Column(name = "amount") 36 | private double amount; 37 | 38 | @ManyToOne(fetch = FetchType.LAZY) 39 | @JoinColumn(name = "food_id", insertable = false, updatable = false) 40 | @Fetch(FetchMode.JOIN) 41 | private Food food; 42 | 43 | // getters and setters 44 | 45 | } 46 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join-three-tables/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/repository/JoinRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join.repository; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.data.jpa.repository.Query; 7 | 8 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.JoinDto; 9 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.entity.Company; 10 | 11 | public interface JoinRepository extends JpaRepository { 12 | 13 | @Query("SELECT new com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.JoinDto(s.id, f.name, c.name, s.amount) " 14 | + "FROM Sale s LEFT JOIN s.food f LEFT JOIN f.company c") 15 | List fetchDataLeftJoin(); 16 | 17 | @Query("SELECT new com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.JoinDto(s.id, f.name, c.name, s.amount) " 18 | + "FROM Sale s RIGHT JOIN s.food f RIGHT JOIN f.company c") 19 | List fetchDataRightJoin(); 20 | 21 | @Query("SELECT new com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.JoinDto(s.id, f.name, c.name, s.amount) " 22 | + "FROM Sale s INNER JOIN s.food f INNER JOIN f.company c") 23 | List fetchDataInnerJoin(); 24 | 25 | @Query("SELECT new com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.JoinDto(s.id, f.name, c.name, s.amount) " 26 | + "FROM Sale s, Food f, Company c WHERE s.food.id = f.id AND f.company.id = c.id") 27 | List fetchDataCrossJoin(); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join-three-tables/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/rest/controller/JoinRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join.rest.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.JoinDto; 12 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.service.JoinService; 13 | 14 | @RestController 15 | public class JoinRestController { 16 | 17 | @Autowired 18 | private JoinService joinService; 19 | 20 | @GetMapping("/join/left") 21 | public ResponseEntity> getLeftJoinData() { 22 | return new ResponseEntity>(joinService.leftJoinData(), HttpStatus.OK); 23 | } 24 | 25 | @GetMapping("/join/right") 26 | public ResponseEntity> getRightJoinData() { 27 | return new ResponseEntity>(joinService.rightJoinData(), HttpStatus.OK); 28 | } 29 | 30 | @GetMapping("/join/inner") 31 | public ResponseEntity> getLeftInnerData() { 32 | return new ResponseEntity>(joinService.innerJoinData(), HttpStatus.OK); 33 | } 34 | 35 | @GetMapping("/join/cross") 36 | public ResponseEntity> getLeftCrossData() { 37 | return new ResponseEntity>(joinService.crossJoinData(), HttpStatus.OK); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join-three-tables/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/service/JoinService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.JoinDto; 9 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.repository.JoinRepository; 10 | 11 | @Service 12 | public class JoinService { 13 | 14 | @Autowired 15 | private JoinRepository joinRepository; 16 | 17 | public List leftJoinData() { 18 | return joinRepository.fetchDataLeftJoin(); 19 | } 20 | 21 | public List rightJoinData() { 22 | return joinRepository.fetchDataRightJoin(); 23 | } 24 | 25 | public List innerJoinData() { 26 | return joinRepository.fetchDataInnerJoin(); 27 | } 28 | 29 | public List crossJoinData() { 30 | return joinRepository.fetchDataCrossJoin(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join-three-tables/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.jdbcUrl=jdbc:mysql://localhost/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | 7 | #disable schema generation from Hibernate 8 | spring.jpa.hibernate.ddl-auto=none -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-boot-data-jpa-left-right-inner-and-cross-join-examples/ 2 | 3 | For spring boot 3.x.y, check the folder spring-boot-3 4 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join/spring-boot-3/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-data-jpa-left-right-inner-cross-join 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.1.5 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-data-jpa 33 | 34 | 35 | 36 | com.mysql 37 | mysql-connector-j 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join/spring-boot-3/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/SpringDataJpaLeftRightInnerCrossJoinApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringDataJpaLeftRightInnerCrossJoinApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringDataJpaLeftRightInnerCrossJoinApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join/spring-boot-3/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/dto/DeptEmpDto.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto; 2 | 3 | public class DeptEmpDto { 4 | 5 | private String empDept; 6 | private String empName; 7 | private String empEmail; 8 | private String empAddress; 9 | 10 | public DeptEmpDto(String empDept, String empName, String empEmail, String empAddress) { 11 | this.empDept = empDept; 12 | this.empName = empName; 13 | this.empEmail = empEmail; 14 | this.empAddress = empAddress; 15 | } 16 | 17 | public String getEmpDept() { 18 | return empDept; 19 | } 20 | 21 | public void setEmpDept(String empDept) { 22 | this.empDept = empDept; 23 | } 24 | 25 | public String getEmpName() { 26 | return empName; 27 | } 28 | 29 | public void setEmpName(String empName) { 30 | this.empName = empName; 31 | } 32 | 33 | public String getEmpEmail() { 34 | return empEmail; 35 | } 36 | 37 | public void setEmpEmail(String empEmail) { 38 | this.empEmail = empEmail; 39 | } 40 | 41 | public String getEmpAddress() { 42 | return empAddress; 43 | } 44 | 45 | public void setEmpAddress(String empAddress) { 46 | this.empAddress = empAddress; 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return "DeptEmpDto [empDept=" + empDept + ", empName=" + empName + ", empEmail=" + empEmail + ", empAddress=" 52 | + empAddress + "]"; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join/spring-boot-3/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/repository/DepartmentRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join.repository; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.data.jpa.repository.Query; 7 | 8 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.DeptEmpDto; 9 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.entity.Department; 10 | 11 | public interface DepartmentRepository extends JpaRepository { 12 | 13 | @Query("SELECT new com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.DeptEmpDto(d.name, e.name, e.email, e.address) " 14 | + "FROM Department d LEFT JOIN d.employees e") 15 | List fetchEmpDeptDataLeftJoin(); 16 | 17 | @Query("SELECT new com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.DeptEmpDto(d.name, e.name, e.email, e.address) " 18 | + "FROM Department d RIGHT JOIN d.employees e") 19 | List fetchEmpDeptDataRightJoin(); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join/spring-boot-3/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join.repository; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.data.jpa.repository.Query; 7 | 8 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.DeptEmpDto; 9 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.entity.Employee; 10 | 11 | public interface EmployeeRepository extends JpaRepository { 12 | 13 | @Query("SELECT new com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.DeptEmpDto(d.name, e.name, e.email, e.address) " 14 | + "FROM Department d INNER JOIN d.employees e") 15 | List fetchEmpDeptDataInnerJoin(); 16 | 17 | @Query("SELECT new com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.DeptEmpDto(d.name, e.name, e.email, e.address) " 18 | + "FROM Department d, Employee e") 19 | List fetchEmpDeptDataCrossJoin(); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join/spring-boot-3/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/service/JoinQueryService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.stereotype.Service; 6 | 7 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.DeptEmpDto; 8 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.repository.DepartmentRepository; 9 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.repository.EmployeeRepository; 10 | 11 | import jakarta.annotation.Resource; 12 | 13 | @Service 14 | public class JoinQueryService { 15 | 16 | @Resource 17 | private DepartmentRepository departmentRepository; 18 | 19 | @Resource 20 | private EmployeeRepository employeeRepository; 21 | 22 | public List getDeptEmployeesLeftJoin() { 23 | List list = departmentRepository.fetchEmpDeptDataLeftJoin(); 24 | list.forEach(l -> System.out.println(l)); 25 | return list; 26 | } 27 | 28 | public List getDeptEmployeesRightJoin() { 29 | List list = departmentRepository.fetchEmpDeptDataRightJoin(); 30 | list.forEach(l -> System.out.println(l)); 31 | return list; 32 | } 33 | 34 | public List getDeptEmployeesInnerJoin() { 35 | List list = employeeRepository.fetchEmpDeptDataInnerJoin(); 36 | list.forEach(l -> System.out.println(l)); 37 | return list; 38 | } 39 | 40 | public List getDeptEmployeesCrossJoin() { 41 | List list = employeeRepository.fetchEmpDeptDataCrossJoin(); 42 | list.forEach(l -> System.out.println(l)); 43 | return list; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join/spring-boot-3/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | #spring.datasource.jdbcUrl=jdbc:mysql://localhost/roytuts 5 | spring.datasource.username=root 6 | spring.datasource.password=root 7 | 8 | #disable schema generation from Hibernate 9 | #spring.jpa.hibernate.ddl-auto=none 10 | #spring.jpa.hibernate.ddl-auto=create 11 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join/spring-data-join.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE IF NOT EXISTS `roytuts` 2 | USE `roytuts`; 3 | 4 | CREATE TABLE IF NOT EXISTS `department` ( 5 | `id` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL AUTO_INCREMENT, 6 | `name` varchar(40) COLLATE utf8mb4_unicode_ci NOT NULL, 7 | `description` varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL, 8 | PRIMARY KEY (`id`) 9 | ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 10 | 11 | INSERT INTO `department` (`id`, `name`, `description`) VALUES 12 | (1, 'IT', 'Information Technology'), 13 | (2, 'TelComm', 'Telecommunication'), 14 | (3, 'Ins', 'Insurance'), 15 | (4, 'HR', 'Human Resources'); 16 | 17 | CREATE TABLE IF NOT EXISTS `employee` ( 18 | `id` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL AUTO_INCREMENT, 19 | `name` varchar(40) COLLATE utf8mb4_unicode_ci NOT NULL, 20 | `email` varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL, 21 | `address` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL, 22 | `dept_id` int unsigned COLLATE utf8mb4_unicode_ci DEFAULT NULL, 23 | PRIMARY KEY (`id`), 24 | KEY `dept_id` (`dept_id`), 25 | CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`dept_id`) REFERENCES `department` (`id`) 26 | ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 27 | 28 | 29 | INSERT INTO `employee` (`id`, `name`, `email`, `address`, `dept_id`) VALUES 30 | (1, 'Soumitra', 'soumitra@gmail.com', NULL, 1), 31 | (2, 'Suman', 'suman@gmail.com', NULL, 2), 32 | (3, 'Avisek', 'avisek@gmail.com', NULL, 3); 33 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/SpringDataJpaLeftRightInnerCrossJoinApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.domain.EntityScan; 6 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 7 | 8 | @SpringBootApplication 9 | @EntityScan("com.roytuts.spring.data.jpa.left.right.inner.cross.join.entity") 10 | @EnableJpaRepositories("com.roytuts.spring.data.jpa.left.right.inner.cross.join.repository") 11 | public class SpringDataJpaLeftRightInnerCrossJoinApp { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(SpringDataJpaLeftRightInnerCrossJoinApp.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/dto/DeptEmpDto.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto; 2 | 3 | public class DeptEmpDto { 4 | 5 | private String empDept; 6 | private String empName; 7 | private String empEmail; 8 | private String empAddress; 9 | 10 | public DeptEmpDto(String empDept, String empName, String empEmail, String empAddress) { 11 | this.empDept = empDept; 12 | this.empName = empName; 13 | this.empEmail = empEmail; 14 | this.empAddress = empAddress; 15 | } 16 | 17 | public String getEmpDept() { 18 | return empDept; 19 | } 20 | 21 | public void setEmpDept(String empDept) { 22 | this.empDept = empDept; 23 | } 24 | 25 | public String getEmpName() { 26 | return empName; 27 | } 28 | 29 | public void setEmpName(String empName) { 30 | this.empName = empName; 31 | } 32 | 33 | public String getEmpEmail() { 34 | return empEmail; 35 | } 36 | 37 | public void setEmpEmail(String empEmail) { 38 | this.empEmail = empEmail; 39 | } 40 | 41 | public String getEmpAddress() { 42 | return empAddress; 43 | } 44 | 45 | public void setEmpAddress(String empAddress) { 46 | this.empAddress = empAddress; 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return "DeptEmpDto [empDept=" + empDept + ", empName=" + empName + ", empEmail=" + empEmail + ", empAddress=" 52 | + empAddress + "]"; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/repository/DepartmentRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join.repository; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.data.jpa.repository.Query; 7 | 8 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.DeptEmpDto; 9 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.entity.Department; 10 | 11 | public interface DepartmentRepository extends JpaRepository { 12 | 13 | @Query("SELECT new com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.DeptEmpDto(d.name, e.name, e.email, e.address) " 14 | + "FROM Department d LEFT JOIN d.employees e") 15 | List fetchEmpDeptDataLeftJoin(); 16 | 17 | @Query("SELECT new com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.DeptEmpDto(d.name, e.name, e.email, e.address) " 18 | + "FROM Department d RIGHT JOIN d.employees e") 19 | List fetchEmpDeptDataRightJoin(); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join/src/main/java/com/roytuts/spring/data/jpa/left/right/inner/cross/join/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.left.right.inner.cross.join.repository; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.data.jpa.repository.Query; 7 | 8 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.DeptEmpDto; 9 | import com.roytuts.spring.data.jpa.left.right.inner.cross.join.entity.Employee; 10 | 11 | public interface EmployeeRepository extends JpaRepository { 12 | 13 | @Query("SELECT new com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.DeptEmpDto(d.name, e.name, e.email, e.address) " 14 | + "FROM Department d INNER JOIN d.employees e") 15 | List fetchEmpDeptDataInnerJoin(); 16 | 17 | @Query("SELECT new com.roytuts.spring.data.jpa.left.right.inner.cross.join.dto.DeptEmpDto(d.name, e.name, e.email, e.address) " 18 | + "FROM Department d, Employee e") 19 | List fetchEmpDeptDataCrossJoin(); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-data-jpa-left-right-inner-cross-join/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | #spring.datasource.jdbcUrl=jdbc:mysql://localhost/roytuts 5 | spring.datasource.username=root 6 | spring.datasource.password=root 7 | 8 | #disable schema generation from Hibernate 9 | #spring.jpa.hibernate.ddl-auto=none 10 | spring.jpa.hibernate.ddl-auto=create 11 | -------------------------------------------------------------------------------- /spring-data-jpa-named-query/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.3.4.RELEASE' 4 | } 5 | 6 | repositories { 7 | mavenCentral() 8 | } 9 | 10 | dependencies { 11 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 12 | } 13 | } 14 | 15 | plugins { 16 | id 'java-library' 17 | id 'org.springframework.boot' version "${springBootVersion}" 18 | } 19 | 20 | sourceCompatibility = 12 21 | targetCompatibility = 12 22 | 23 | repositories { 24 | mavenCentral() 25 | } 26 | 27 | dependencies { 28 | implementation("org.springframework.boot:spring-boot-starter:${springBootVersion}") 29 | implementation("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 30 | 31 | runtime("com.h2database:h2:1.4.200") 32 | 33 | //required for JDK 9 or above 34 | implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359' 35 | } -------------------------------------------------------------------------------- /spring-data-jpa-named-query/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-data-jpa-namedquery-and-namedqueries-example/ 2 | -------------------------------------------------------------------------------- /spring-data-jpa-named-query/settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * This file was generated by the Gradle 'init' task. 3 | * 4 | * The settings file is used to specify which projects to include in your build. 5 | * 6 | * Detailed information about configuring a multi-project build in Gradle can be found 7 | * in the user manual at https://docs.gradle.org/6.5.1/userguide/multi_project_builds.html 8 | */ 9 | 10 | rootProject.name = 'spring-data-jpa-named-query' 11 | -------------------------------------------------------------------------------- /spring-data-jpa-named-query/src/main/java/com/roytuts/spring/data/jpa/named/query/SpringDataJpaNamedQueryApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.named.query; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.boot.autoconfigure.domain.EntityScan; 10 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 11 | 12 | import com.roytuts.spring.data.jpa.named.query.entity.Item; 13 | import com.roytuts.spring.data.jpa.named.query.service.NamedQueryService; 14 | 15 | @SpringBootApplication 16 | @EntityScan("com.roytuts.spring.data.jpa.named.query.entity") 17 | @EnableJpaRepositories("com.roytuts.spring.data.jpa.named.query.repository") 18 | public class SpringDataJpaNamedQueryApp implements CommandLineRunner { 19 | 20 | @Autowired 21 | private NamedQueryService service; 22 | 23 | public static void main(String[] args) { 24 | SpringApplication.run(SpringDataJpaNamedQueryApp.class, args); 25 | } 26 | 27 | @Override 28 | public void run(String... args) throws Exception { 29 | List itemList = service.getAllItems(); 30 | 31 | itemList.stream().forEach(i -> System.out.println(i)); 32 | 33 | List items = service.getItemsByName("CD"); 34 | 35 | items.stream().forEach(i -> System.out.println(i)); 36 | 37 | Item item = service.getItemByPrice(30.02); 38 | System.out.println(item); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /spring-data-jpa-named-query/src/main/java/com/roytuts/spring/data/jpa/named/query/config/Config.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.named.query.config; 2 | 3 | import javax.sql.DataSource; 4 | 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; 7 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; 8 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; 9 | 10 | //@Configuration 11 | public class Config { 12 | 13 | @Bean 14 | public DataSource dataSource() { 15 | EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); 16 | EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.H2).addScript("classpath:sql/table.sql") 17 | .addScript("classpath:sql/data.sql").build(); 18 | return db; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-data-jpa-named-query/src/main/java/com/roytuts/spring/data/jpa/named/query/repository/NameQueryRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.named.query.repository; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.data.repository.query.Param; 7 | 8 | import com.roytuts.spring.data.jpa.named.query.entity.Item; 9 | 10 | public interface NameQueryRepository extends JpaRepository { 11 | 12 | List findAll(); 13 | 14 | List findByName(String name); 15 | 16 | Item findByPrice(@Param("price") Double price); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /spring-data-jpa-named-query/src/main/java/com/roytuts/spring/data/jpa/named/query/service/NamedQueryService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.named.query.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.roytuts.spring.data.jpa.named.query.entity.Item; 9 | import com.roytuts.spring.data.jpa.named.query.repository.NameQueryRepository; 10 | 11 | @Service 12 | public class NamedQueryService { 13 | 14 | @Autowired 15 | private NameQueryRepository repository; 16 | 17 | public List getAllItems() { 18 | return repository.findAll(); 19 | } 20 | 21 | public List getItemsByName(final String name) { 22 | return repository.findByName(name); 23 | } 24 | 25 | public Item getItemByPrice(final Double price) { 26 | return repository.findByPrice(price); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring-data-jpa-named-query/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 2 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | 6 | spring.jpa.show-sql=true 7 | spring.h2.console.enabled=true 8 | spring.jpa.hibernate.ddl-auto = none 9 | #spring.jpa.hibernate.ddl-auto = update 10 | -------------------------------------------------------------------------------- /spring-data-jpa-named-query/src/main/resources/sql/data.sql: -------------------------------------------------------------------------------- 1 | insert into `item`(`item_id`,`item_name`,`item_desc`,`item_price`) 2 | values (1,'CD','CD is a compact disk',100), 3 | (2,'DVD','DVD is larger than CD in size',150), 4 | (3,'ABC','ABC test description',24), 5 | (4,'XYZ','XYZ test description',25.32), 6 | (5,'CD Player','CD player is used to play CD',30.02), 7 | (6,'New Item1','New Item1 Desc',125); -------------------------------------------------------------------------------- /spring-data-jpa-named-query/src/main/resources/sql/table.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `item` ( 2 | `item_id` int(10) NOT NULL AUTO_INCREMENT, 3 | `item_name` varchar(45) NOT NULL, 4 | `item_desc` text, 5 | `item_price` double NOT NULL DEFAULT '0', 6 | PRIMARY KEY (`item_id`) 7 | ); -------------------------------------------------------------------------------- /spring-data-jpa-native-query-to-dto/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.2.4.RELEASE' to 2.7.0 4 | } 5 | repositories { 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 | } 11 | } 12 | 13 | plugins { 14 | id 'java-library' 15 | id 'org.springframework.boot' version "${springBootVersion}" 16 | } 17 | 18 | sourceCompatibility = 12 19 | targetCompatibility = 12 20 | 21 | repositories { 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | implementation("org.springframework.boot:spring-boot-starter:${springBootVersion}") 27 | implementation("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 28 | implementation('mysql:mysql-connector-java:8.0.17/26') 29 | //required only if jdk 9 or higher version is used 30 | runtimeOnly('javax.xml.bind:jaxb-api:2.4.0-b180830.0359') 31 | } 32 | -------------------------------------------------------------------------------- /spring-data-jpa-native-query-to-dto/event.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `event` ( 2 | `id` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL AUTO_INCREMENT, 3 | `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, 4 | `url` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, 5 | `clasz` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, 6 | `start_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 7 | `end_date` timestamp NULL DEFAULT NULL, 8 | PRIMARY KEY (`id`) 9 | ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 10 | 11 | INSERT INTO `event` (`id`, `title`, `url`, `clasz`, `start_date`, `end_date`) VALUES 12 | (1, 'Example', 'http://www.example.com', 'event-success', '2021-03-03 15:27:51', '2020-04-10 20:01:02'), 13 | (2, 'Jee Tutorials', 'https://roytuts.com', 'event-important', '2021-03-11 19:00:00', '2020-03-12 19:42:45'), 14 | (3, 'Roy Tutorial', 'https://roytuts.com', 'event-info', '2021-03-12 20:03:05', '2020-05-13 08:45:53'); -------------------------------------------------------------------------------- /spring-data-jpa-native-query-to-dto/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/how-to-map-custom-query-results-into-dto-in-spring-data-jpa/ 2 | -------------------------------------------------------------------------------- /spring-data-jpa-native-query-to-dto/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 2 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 3 | spring.datasource.username=root 4 | spring.datasource.password=root -------------------------------------------------------------------------------- /spring-data-jpa-pageable/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | 6 | com.roytuts 7 | spring-data-jpa-pageable 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | 12 | UTF-8 13 | 11 14 | 11 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter-parent 20 | 2.5.6 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-data-jpa 32 | 33 | 34 | 38 | 39 | 40 | mysql 41 | mysql-connector-java 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-maven-plugin 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /spring-data-jpa-pageable/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/bootstrap-ajax-spring-boot-pagination/ -------------------------------------------------------------------------------- /spring-data-jpa-pageable/src/main/java/com/roytuts/spring/data/jpa/pageable/PageableApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.pageable; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.domain.EntityScan; 6 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 7 | 8 | @SpringBootApplication 9 | @EntityScan(basePackages = "com.roytuts.spring.data.jpa.pageable.entity") 10 | @EnableJpaRepositories(basePackages = "com.roytuts.spring.data.jpa.pageable.repository") 11 | public class PageableApp { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(PageableApp.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-data-jpa-pageable/src/main/java/com/roytuts/spring/data/jpa/pageable/converter/EntityDtoConverter.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.pageable.converter; 2 | 3 | import com.roytuts.spring.data.jpa.pageable.dto.NoteDto; 4 | import com.roytuts.spring.data.jpa.pageable.entity.Note; 5 | 6 | public final class EntityDtoConverter { 7 | 8 | private EntityDtoConverter() { 9 | } 10 | 11 | public static NoteDto entityToDto(Note note) { 12 | NoteDto noteDto = new NoteDto(); 13 | 14 | noteDto.setId(note.getId()); 15 | noteDto.setNote(note.getNote()); 16 | 17 | return noteDto; 18 | } 19 | 20 | public static Note dtoToEntity(NoteDto noteDto) { 21 | Note note = new Note(); 22 | 23 | note.setId(noteDto.getId()); 24 | note.setNote(noteDto.getNote()); 25 | 26 | return note; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring-data-jpa-pageable/src/main/java/com/roytuts/spring/data/jpa/pageable/dto/NoteDto.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.pageable.dto; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | 10 | @Entity 11 | @Table 12 | public class NoteDto { 13 | 14 | @Id 15 | @GeneratedValue(strategy = GenerationType.AUTO) 16 | private Integer id; 17 | 18 | @Column 19 | private String note; 20 | 21 | public Integer getId() { 22 | return id; 23 | } 24 | 25 | public void setId(Integer id) { 26 | this.id = id; 27 | } 28 | 29 | public String getNote() { 30 | return note; 31 | } 32 | 33 | public void setNote(String note) { 34 | this.note = note; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /spring-data-jpa-pageable/src/main/java/com/roytuts/spring/data/jpa/pageable/entity/Note.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.pageable.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | 10 | @Entity 11 | @Table 12 | public class Note { 13 | 14 | @Id 15 | @GeneratedValue(strategy = GenerationType.AUTO) 16 | private Integer id; 17 | 18 | @Column 19 | private String note; 20 | 21 | public Integer getId() { 22 | return id; 23 | } 24 | 25 | public void setId(Integer id) { 26 | this.id = id; 27 | } 28 | 29 | public String getNote() { 30 | return note; 31 | } 32 | 33 | public void setNote(String note) { 34 | this.note = note; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /spring-data-jpa-pageable/src/main/java/com/roytuts/spring/data/jpa/pageable/initializer/NoteInitializer.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.pageable.initializer; 2 | 3 | import javax.annotation.PostConstruct; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Component; 7 | 8 | import com.roytuts.spring.data.jpa.pageable.entity.Note; 9 | import com.roytuts.spring.data.jpa.pageable.repository.NoteRepository; 10 | 11 | @Component 12 | public class NoteInitializer { 13 | 14 | @Autowired 15 | private NoteRepository noteRepository; 16 | 17 | @PostConstruct 18 | private void loadNotes() { 19 | for (int i = 0; i < 1000; i++) { 20 | Note note = new Note(); 21 | note.setNote("Note " + i); 22 | 23 | noteRepository.save(note); 24 | } 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /spring-data-jpa-pageable/src/main/java/com/roytuts/spring/data/jpa/pageable/repository/NoteRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.pageable.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.data.jpa.pageable.entity.Note; 6 | 7 | public interface NoteRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-data-jpa-pageable/src/main/java/com/roytuts/spring/data/jpa/pageable/rest/controller/NoteRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.pageable.rest.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.data.domain.Page; 5 | import org.springframework.data.domain.Pageable; 6 | import org.springframework.web.bind.annotation.CrossOrigin; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import com.roytuts.spring.data.jpa.pageable.dto.NoteDto; 11 | import com.roytuts.spring.data.jpa.pageable.service.NoteService; 12 | 13 | @RestController 14 | @CrossOrigin(origins = "*") 15 | public class NoteRestController { 16 | 17 | @Autowired 18 | private NoteService noteService; 19 | 20 | @GetMapping("/notes") 21 | public Page notes(Pageable pageable) { 22 | return noteService.notes(pageable); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-data-jpa-pageable/src/main/java/com/roytuts/spring/data/jpa/pageable/service/NoteService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.pageable.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.data.domain.Page; 5 | import org.springframework.data.domain.Pageable; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.roytuts.spring.data.jpa.pageable.converter.EntityDtoConverter; 9 | import com.roytuts.spring.data.jpa.pageable.dto.NoteDto; 10 | import com.roytuts.spring.data.jpa.pageable.entity.Note; 11 | import com.roytuts.spring.data.jpa.pageable.repository.NoteRepository; 12 | 13 | @Service 14 | public class NoteService { 15 | 16 | @Autowired 17 | private NoteRepository noteRepository; 18 | 19 | public Page notes(Pageable pageable) { 20 | Page notes = noteRepository.findAll(pageable); 21 | 22 | Page pages = notes.map(entity -> { 23 | NoteDto dto = EntityDtoConverter.entityToDto(entity); 24 | return dto; 25 | }); 26 | 27 | return pages; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /spring-data-jpa-pageable/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 2 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | 6 | spring.jpa.show-sql = true 7 | spring.jpa.properties.hibernate.format_sql=true 8 | 9 | logging.level.org.hibernate.type.descriptor.sql=trace 10 | 11 | spring.jpa.hibernate.ddl-auto = create 12 | #spring.jpa.hibernate.ddl-auto = none -------------------------------------------------------------------------------- /spring-data-jpa-pageable/~$ring-data-jpa-pageable.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roytuts/spring-jpa/499b4743729cf4efcd86a9d16713d090cf143aa6/spring-data-jpa-pageable/~$ring-data-jpa-pageable.docx -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.1.8.RELEASE' 4 | } 5 | repositories { 6 | mavenLocal() 7 | mavenCentral() 8 | } 9 | dependencies { 10 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 11 | } 12 | } 13 | 14 | apply plugin: 'java' 15 | apply plugin: 'org.springframework.boot' 16 | 17 | sourceCompatibility = 12 18 | targetCompatibility = 12 19 | 20 | repositories { 21 | mavenLocal() 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 27 | implementation("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 28 | runtime('mysql:mysql-connector-java:8.0.17') 29 | } -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-data-jpa-specification-criteria-query/ 2 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/spring-boot-3/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-data-jpa-specification-criteria-in-clause 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.2.0 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-data-jpa 33 | 34 | 35 | 36 | com.mysql 37 | mysql-connector-j 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/spring-boot-3/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | 7 | #show sql query 8 | logging.level.org.hibernate.SQL=DEBUG 9 | 10 | #disable schema generation from Hibernate 11 | spring.jpa.hibernate.ddl-auto=none 12 | 13 | #server port 14 | server.port=9999 15 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/spring-boot-3/src/main/resources/java/com/roytuts/spring/data/jpa/specification/criteria/in/clause/SpringDataJpaSpecificationCriteriaInClauseApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.specification.criteria.in.clause; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringDataJpaSpecificationCriteriaInClauseApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringDataJpaSpecificationCriteriaInClauseApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/spring-boot-3/src/main/resources/java/com/roytuts/spring/data/jpa/specification/criteria/in/clause/criteria/CaseSearchCriteria.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.specification.criteria.in.clause.criteria; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | public class CaseSearchCriteria implements Serializable { 7 | 8 | private static final long serialVersionUID = 1L; 9 | 10 | private String country; 11 | private List regionIds; 12 | 13 | public String getCountry() { 14 | return country; 15 | } 16 | 17 | public void setCountry(String country) { 18 | this.country = country; 19 | } 20 | 21 | public List getRegionIds() { 22 | return regionIds; 23 | } 24 | 25 | public void setRegionIds(List regionIds) { 26 | this.regionIds = regionIds; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/spring-boot-3/src/main/resources/java/com/roytuts/spring/data/jpa/specification/criteria/in/clause/entity/Case.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.specification.criteria.in.clause.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | import jakarta.persistence.CascadeType; 6 | import jakarta.persistence.Column; 7 | import jakarta.persistence.Entity; 8 | import jakarta.persistence.Id; 9 | import jakarta.persistence.JoinColumn; 10 | import jakarta.persistence.ManyToOne; 11 | import jakarta.persistence.Table; 12 | 13 | @Entity 14 | @Table(name = "cse") 15 | public class Case implements Serializable { 16 | 17 | private static final long serialVersionUID = 1L; 18 | 19 | @Id 20 | @Column(name = "id") 21 | private Integer id; 22 | 23 | @ManyToOne(cascade = CascadeType.MERGE) 24 | @JoinColumn(name = "region_id", nullable = false) 25 | private Region region; 26 | 27 | @ManyToOne(cascade = CascadeType.MERGE) 28 | @JoinColumn(name = "country_id", nullable = false) 29 | private Country country; 30 | 31 | public Integer getId() { 32 | return id; 33 | } 34 | 35 | public void setId(Integer id) { 36 | this.id = id; 37 | } 38 | 39 | public Region getRegion() { 40 | return region; 41 | } 42 | 43 | public void setRegion(Region region) { 44 | this.region = region; 45 | } 46 | 47 | public Country getCountry() { 48 | return country; 49 | } 50 | 51 | public void setCountry(Country country) { 52 | this.country = country; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/spring-boot-3/src/main/resources/java/com/roytuts/spring/data/jpa/specification/criteria/in/clause/entity/Country.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.specification.criteria.in.clause.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Set; 5 | 6 | import jakarta.persistence.CascadeType; 7 | import jakarta.persistence.Column; 8 | import jakarta.persistence.Entity; 9 | import jakarta.persistence.Id; 10 | import jakarta.persistence.OneToMany; 11 | import jakarta.persistence.Table; 12 | 13 | @Entity 14 | @Table(name = "COUNTRY") 15 | public class Country implements Serializable { 16 | 17 | private static final long serialVersionUID = 1L; 18 | 19 | @Id 20 | @Column(name = "ID") 21 | private Integer id; 22 | 23 | @Column(name = "NAME") 24 | private String name; 25 | 26 | @OneToMany(cascade = CascadeType.MERGE, mappedBy = "country") 27 | //@OneToMany(cascade = CascadeType.ALL, mappedBy = "country") 28 | private Set cases; 29 | 30 | public Integer getId() { 31 | return id; 32 | } 33 | 34 | public void setId(Integer id) { 35 | this.id = id; 36 | } 37 | 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | public void setName(String name) { 43 | this.name = name; 44 | } 45 | 46 | public Set getCases() { 47 | return cases; 48 | } 49 | 50 | public void setCases(Set cases) { 51 | this.cases = cases; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/spring-boot-3/src/main/resources/java/com/roytuts/spring/data/jpa/specification/criteria/in/clause/entity/Region.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.specification.criteria.in.clause.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Set; 5 | 6 | import jakarta.persistence.CascadeType; 7 | import jakarta.persistence.Column; 8 | import jakarta.persistence.Entity; 9 | import jakarta.persistence.Id; 10 | import jakarta.persistence.OneToMany; 11 | import jakarta.persistence.Table; 12 | 13 | @Entity 14 | @Table(name = "REGION") 15 | public class Region implements Serializable { 16 | 17 | private static final long serialVersionUID = 1L; 18 | 19 | @Id 20 | @Column(name = "ID") 21 | private Integer id; 22 | 23 | @Column(name = "NAME") 24 | private String name; 25 | 26 | @OneToMany(cascade = CascadeType.MERGE, mappedBy = "region") 27 | // @OneToMany(cascade = CascadeType.ALL, mappedBy = "region") 28 | private Set cases; 29 | 30 | public Integer getId() { 31 | return id; 32 | } 33 | 34 | public void setId(Integer id) { 35 | this.id = id; 36 | } 37 | 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | public void setName(String name) { 43 | this.name = name; 44 | } 45 | 46 | public Set getCases() { 47 | return cases; 48 | } 49 | 50 | public void setCases(Set cases) { 51 | this.cases = cases; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/spring-boot-3/src/main/resources/java/com/roytuts/spring/data/jpa/specification/criteria/in/clause/repository/CaseRepo.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.specification.criteria.in.clause.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor; 5 | 6 | import com.roytuts.spring.data.jpa.specification.criteria.in.clause.entity.Case; 7 | 8 | public interface CaseRepo extends JpaRepository, JpaSpecificationExecutor { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/spring-boot-3/src/main/resources/java/com/roytuts/spring/data/jpa/specification/criteria/in/clause/vo/CaseVo.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.specification.criteria.in.clause.vo; 2 | 3 | public class CaseVo { 4 | 5 | private int caseId; 6 | private String region; 7 | private String country; 8 | 9 | public int getCaseId() { 10 | return caseId; 11 | } 12 | 13 | public void setCaseId(int caseId) { 14 | this.caseId = caseId; 15 | } 16 | 17 | public String getRegion() { 18 | return region; 19 | } 20 | 21 | public void setRegion(String region) { 22 | this.region = region; 23 | } 24 | 25 | public String getCountry() { 26 | return country; 27 | } 28 | 29 | public void setCountry(String country) { 30 | this.country = country; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/sql.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `region` ( 2 | `id` int unsigned NOT NULL AUTO_INCREMENT, 3 | `name` varchar(50) DEFAULT NULL, 4 | PRIMARY KEY (`id`) 5 | ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 6 | 7 | insert into `region`(`name`) 8 | values ('Asia'), ('US'), ('UK'); 9 | 10 | CREATE TABLE `country` ( 11 | `id` int unsigned NOT NULL AUTO_INCREMENT, 12 | `name` varchar(50) DEFAULT NULL, 13 | PRIMARY KEY (`id`) 14 | ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 15 | 16 | insert into `country`(`name`) 17 | values ('India'), ('Mexico'), ('Canada'), ('England'), ('Scotland'); 18 | 19 | CREATE TABLE `cse` ( 20 | `id` int unsigned NOT NULL AUTO_INCREMENT, 21 | `region_id` int unsigned NOT NULL, 22 | `country_id` int unsigned NOT NULL, 23 | PRIMARY KEY (`id`), 24 | FOREIGN KEY (region_id) 25 | REFERENCES region(id), 26 | FOREIGN KEY (country_id) 27 | REFERENCES country(id) 28 | ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 29 | 30 | insert into `cse`(`region_id`, `country_id`) 31 | values (1, 1), (2, 3), (2, 2), (3, 4); 32 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/src/main/java/com/roytuts/spring/data/jpa/specification/criteria/in/clause/SpringDataJpaSpecificationCriteriaInClauseApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.specification.criteria.in.clause; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication(scanBasePackages = "com.roytuts.spring.data.jpa.specification.criteria.in.clause") 7 | public class SpringDataJpaSpecificationCriteriaInClauseApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringDataJpaSpecificationCriteriaInClauseApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/src/main/java/com/roytuts/spring/data/jpa/specification/criteria/in/clause/criteria/CaseSearchCriteria.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.specification.criteria.in.clause.criteria; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | public class CaseSearchCriteria implements Serializable { 7 | 8 | private static final long serialVersionUID = 1L; 9 | 10 | private String country; 11 | private List regionIds; 12 | 13 | public String getCountry() { 14 | return country; 15 | } 16 | 17 | public void setCountry(String country) { 18 | this.country = country; 19 | } 20 | 21 | public List getRegionIds() { 22 | return regionIds; 23 | } 24 | 25 | public void setRegionIds(List regionIds) { 26 | this.regionIds = regionIds; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/src/main/java/com/roytuts/spring/data/jpa/specification/criteria/in/clause/entity/Case.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.specification.criteria.in.clause.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.CascadeType; 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.Id; 9 | import javax.persistence.JoinColumn; 10 | import javax.persistence.ManyToOne; 11 | import javax.persistence.Table; 12 | 13 | @Entity 14 | @Table(name = "CSE") 15 | public class Case implements Serializable { 16 | 17 | private static final long serialVersionUID = 1L; 18 | 19 | @Id 20 | @Column(name = "ID") 21 | private Integer id; 22 | 23 | @ManyToOne(cascade = CascadeType.MERGE) 24 | @JoinColumn(name = "REGION_ID", nullable = false) 25 | private Region region; 26 | 27 | @ManyToOne(cascade = CascadeType.MERGE) 28 | @JoinColumn(name = "COUNTRY_ID", nullable = false) 29 | private Country country; 30 | 31 | public Integer getId() { 32 | return id; 33 | } 34 | 35 | public void setId(Integer id) { 36 | this.id = id; 37 | } 38 | 39 | public Region getRegion() { 40 | return region; 41 | } 42 | 43 | public void setRegion(Region region) { 44 | this.region = region; 45 | } 46 | 47 | public Country getCountry() { 48 | return country; 49 | } 50 | 51 | public void setCountry(Country country) { 52 | this.country = country; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/src/main/java/com/roytuts/spring/data/jpa/specification/criteria/in/clause/entity/Country.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.specification.criteria.in.clause.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Set; 5 | 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.Id; 10 | import javax.persistence.OneToMany; 11 | import javax.persistence.Table; 12 | 13 | @Entity 14 | @Table(name = "COUNTRY") 15 | public class Country implements Serializable { 16 | 17 | private static final long serialVersionUID = 1L; 18 | 19 | @Id 20 | @Column(name = "ID") 21 | private Integer id; 22 | 23 | @Column(name = "NAME") 24 | private String name; 25 | 26 | @OneToMany(cascade = CascadeType.MERGE, mappedBy = "country") 27 | private Set cases; 28 | 29 | public Integer getId() { 30 | return id; 31 | } 32 | 33 | public void setId(Integer id) { 34 | this.id = id; 35 | } 36 | 37 | public String getName() { 38 | return name; 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public Set getCases() { 46 | return cases; 47 | } 48 | 49 | public void setCases(Set cases) { 50 | this.cases = cases; 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/src/main/java/com/roytuts/spring/data/jpa/specification/criteria/in/clause/entity/Region.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.specification.criteria.in.clause.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Set; 5 | 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.Id; 10 | import javax.persistence.OneToMany; 11 | import javax.persistence.Table; 12 | 13 | @Entity 14 | @Table(name = "REGION") 15 | public class Region implements Serializable { 16 | 17 | private static final long serialVersionUID = 1L; 18 | 19 | @Id 20 | @Column(name = "ID") 21 | private Integer id; 22 | 23 | @Column(name = "NAME") 24 | private String name; 25 | 26 | @OneToMany(cascade = CascadeType.MERGE, mappedBy = "region") 27 | private Set cases; 28 | 29 | public Integer getId() { 30 | return id; 31 | } 32 | 33 | public void setId(Integer id) { 34 | this.id = id; 35 | } 36 | 37 | public String getName() { 38 | return name; 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public Set getCases() { 46 | return cases; 47 | } 48 | 49 | public void setCases(Set cases) { 50 | this.cases = cases; 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/src/main/java/com/roytuts/spring/data/jpa/specification/criteria/in/clause/repository/CaseRepo.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.specification.criteria.in.clause.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor; 5 | 6 | import com.roytuts.spring.data.jpa.specification.criteria.in.clause.entity.Case; 7 | 8 | public interface CaseRepo extends JpaRepository, JpaSpecificationExecutor { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/src/main/java/com/roytuts/spring/data/jpa/specification/criteria/in/clause/vo/CaseVo.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.data.jpa.specification.criteria.in.clause.vo; 2 | 3 | public class CaseVo { 4 | 5 | private int caseId; 6 | private String region; 7 | private String country; 8 | 9 | public int getCaseId() { 10 | return caseId; 11 | } 12 | 13 | public void setCaseId(int caseId) { 14 | this.caseId = caseId; 15 | } 16 | 17 | public String getRegion() { 18 | return region; 19 | } 20 | 21 | public void setRegion(String region) { 22 | this.region = region; 23 | } 24 | 25 | public String getCountry() { 26 | return country; 27 | } 28 | 29 | public void setCountry(String country) { 30 | this.country = country; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-data-jpa-specification-criteria-in-clause/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect 4 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 5 | spring.datasource.username=root 6 | spring.datasource.password=root 7 | 8 | #show sql query 9 | logging.level.org.hibernate.SQL=DEBUG 10 | 11 | #disable schema generation from Hibernate 12 | spring.jpa.hibernate.ddl-auto=none 13 | 14 | #server port 15 | server.port=9999 -------------------------------------------------------------------------------- /spring-data-jpa-stored-procedure/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.2.5.RELEASE' to 2.4.3 4 | } 5 | repositories { 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 | } 11 | } 12 | 13 | plugins { 14 | id 'java-library' 15 | id 'org.springframework.boot' version "${springBootVersion}" 16 | } 17 | 18 | sourceCompatibility = 12 19 | targetCompatibility = 12 20 | 21 | repositories { 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | implementation("org.springframework.boot:spring-boot-starter:${springBootVersion}") 27 | implementation("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 28 | implementation('mysql:mysql-connector-java:8.0.17') to 8.0.22 29 | //required only if jdk 9 or higher version is used 30 | runtimeOnly('javax.xml.bind:jaxb-api:2.4.0-b180830.0359') 31 | } -------------------------------------------------------------------------------- /spring-data-jpa-stored-procedure/pom.xml_3: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | com.roytuts 8 | spring-data-jpa-stored-procedure 9 | 0.0.1-SNAPSHOT 10 | 11 | 12 | UTF-8 13 | 19 14 | 19 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter-parent 20 | 3.2.0 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-data-jpa 32 | 33 | 34 | 35 | com.mysql 36 | mysql-connector-j 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-maven-plugin 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /spring-data-jpa-stored-procedure/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/how-to-work-with-in-out-and-inout-parameters-in-stored-procedures-using-spring-data-jpa/ 2 | -------------------------------------------------------------------------------- /spring-data-jpa-stored-procedure/src/main/java/spring/data/jpa/stored/procedure/repository/UserDetailsJpaRepository.java: -------------------------------------------------------------------------------- 1 | package spring.data.jpa.stored.procedure.repository; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.data.jpa.repository.Query; 7 | import org.springframework.data.jpa.repository.query.Procedure; 8 | import org.springframework.data.repository.query.Param; 9 | 10 | import spring.data.jpa.stored.procedure.entity.UserDetails; 11 | 12 | public interface UserDetailsJpaRepository extends JpaRepository { 13 | 14 | @Query(value = "call get_users()", nativeQuery = true) 15 | List findUserDetailsList(); 16 | 17 | @Procedure(procedureName = "get_user_full_name_in_out", outputParameterName = "full_name") 18 | String findUserFullNameIn_OutUsingName(@Param("user_id") Integer in); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /spring-data-jpa-stored-procedure/src/main/java/spring/data/jpa/stored/procedure/service/UserService.java: -------------------------------------------------------------------------------- 1 | package spring.data.jpa.stored.procedure.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import spring.data.jpa.stored.procedure.entity.UserDetails; 9 | import spring.data.jpa.stored.procedure.repository.UserDetailsJpaRepository; 10 | import spring.data.jpa.stored.procedure.repository.UserDetailsRepository; 11 | 12 | @Service 13 | public class UserService { 14 | 15 | @Autowired 16 | private UserDetailsJpaRepository jpaRepository; 17 | 18 | @Autowired 19 | private UserDetailsRepository repository; 20 | 21 | public List getUserListUsingNativeQuery() { 22 | return jpaRepository.findUserDetailsList(); 23 | } 24 | 25 | public List getUserDetailsListUsingProcAlias() { 26 | return repository.findUserDetailsListUsingAlias(); 27 | } 28 | 29 | public List getUserDetailsListUsingProcName() { 30 | return repository.findUserDetailsListUsingAlias(); 31 | } 32 | 33 | public String getUserFullNameInOutUsingProcName(String dob) { 34 | return repository.findUserFullNameInOutUsingName(dob); 35 | } 36 | 37 | public String getUserFullNameIn_OutUsingProcName(int in) { 38 | return repository.findUserFullNameIn_OutUsingName(in); 39 | // return repository.findUserFullNameIn_OutUsingName(in); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring-data-jpa-stored-procedure/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 2 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 3 | spring.datasource.username=root 4 | spring.datasource.password=root -------------------------------------------------------------------------------- /spring-image-blob-image/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/store-image-as-blob-and-retrieve-blob-as-image-using-spring/ 2 | -------------------------------------------------------------------------------- /spring-image-blob-image/src/main/java/com/roytuts/spring/image/blob/App.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.image.blob; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class App { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(App.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-image-blob-image/src/main/java/com/roytuts/spring/image/blob/repository/ImageRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.image.blob.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.image.blob.entity.Image; 6 | 7 | public interface ImageRepository extends JpaRepository{ 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-image-blob-image/src/main/java/com/roytuts/spring/image/blob/service/ImageService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.image.blob.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.roytuts.spring.image.blob.entity.Image; 9 | import com.roytuts.spring.image.blob.repository.ImageRepository; 10 | 11 | @Service 12 | public class ImageService { 13 | 14 | @Autowired 15 | private ImageRepository imageRepository; 16 | 17 | public void saveImage(Image image) { 18 | imageRepository.save(image); 19 | } 20 | 21 | public void deleteImage(Integer id) { 22 | imageRepository.deleteById(id); 23 | } 24 | 25 | public Image getImage(Integer id) { 26 | return imageRepository.findById(id).get(); 27 | } 28 | 29 | public List imageList() { 30 | return imageRepository.findAll(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-image-blob-image/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | -------------------------------------------------------------------------------- /spring-image-blob-image/src/main/resources/templates/images.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Display Image Files 5 | 6 | 7 |

Upload An Image

8 | 9 |
10 | 11 |
12 |

List of Images

13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 32 | 33 | 34 |
IDNameImageActions
[[${img.id}]][[${img.name}]]picture 29 | Download 30 | Delete 31 |
35 |
36 | 37 | 38 | -------------------------------------------------------------------------------- /spring-image-blob-image/src/main/resources/templates/upload.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Upload Image File 5 | 6 | 7 |

List Of Images

8 | 9 |

Upload An Image File

10 | 11 |
12 | 13 |
14 |

Select Image:

15 |

16 |
17 | 18 | 19 | -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytomany-without-join-entity/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-jpa-bidirectional-manytomany-association-without-join-entity/ -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytomany-without-join-entity/src/main/java/com/roytuts/spring/jpa/bidirectional/manytomany/without/join/entity/entity/Artist.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.bidirectional.manytomany.without.join.entity.entity; 2 | 3 | import java.util.Set; 4 | 5 | import javax.persistence.CascadeType; 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.GenerationType; 10 | import javax.persistence.Id; 11 | import javax.persistence.ManyToMany; 12 | import javax.persistence.Table; 13 | 14 | @Table 15 | @Entity 16 | public class Artist { 17 | 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.IDENTITY) 20 | private int artistId; 21 | 22 | @Column 23 | private String artistName; 24 | 25 | @ManyToMany(mappedBy = "artists", cascade = CascadeType.ALL) 26 | private Set cds; 27 | 28 | public int getArtistId() { 29 | return artistId; 30 | } 31 | 32 | public void setArtistId(int artistId) { 33 | this.artistId = artistId; 34 | } 35 | 36 | public String getArtistName() { 37 | return artistName; 38 | } 39 | 40 | public void setArtistName(String artistName) { 41 | this.artistName = artistName; 42 | } 43 | 44 | public Set getCds() { 45 | return cds; 46 | } 47 | 48 | public void setCds(Set cds) { 49 | this.cds = cds; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytomany-without-join-entity/src/main/java/com/roytuts/spring/jpa/bidirectional/manytomany/without/join/entity/entity/Cd.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.bidirectional.manytomany.without.join.entity.entity; 2 | 3 | import java.util.Set; 4 | 5 | import javax.persistence.CascadeType; 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.GenerationType; 10 | import javax.persistence.Id; 11 | import javax.persistence.JoinColumn; 12 | import javax.persistence.JoinTable; 13 | import javax.persistence.ManyToMany; 14 | import javax.persistence.Table; 15 | 16 | @Table 17 | @Entity 18 | public class Cd { 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.IDENTITY) 22 | private int cdId; 23 | 24 | @Column 25 | private String cdTitle; 26 | 27 | @ManyToMany(cascade = CascadeType.ALL) 28 | @JoinTable(name = "artist_cd", joinColumns = @JoinColumn(name = "cd_id"), inverseJoinColumns = @JoinColumn(name = "artist_id")) 29 | private Set artists; 30 | 31 | public int getCdId() { 32 | return cdId; 33 | } 34 | 35 | public void setCdId(int cdId) { 36 | this.cdId = cdId; 37 | } 38 | 39 | public String getCdTitle() { 40 | return cdTitle; 41 | } 42 | 43 | public void setCdTitle(String cdTitle) { 44 | this.cdTitle = cdTitle; 45 | } 46 | 47 | public Set getArtists() { 48 | return artists; 49 | } 50 | 51 | public void setArtists(Set artists) { 52 | this.artists = artists; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytomany-without-join-entity/src/main/java/com/roytuts/spring/jpa/bidirectional/manytomany/without/join/entity/repository/ArtistRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.bidirectional.manytomany.without.join.entity.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.jpa.bidirectional.manytomany.without.join.entity.entity.Artist; 6 | 7 | public interface ArtistRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytomany-without-join-entity/src/main/java/com/roytuts/spring/jpa/bidirectional/manytomany/without/join/entity/repository/CdRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.bidirectional.manytomany.without.join.entity.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.jpa.bidirectional.manytomany.without.join.entity.entity.Cd; 6 | 7 | public interface CdRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytomany-without-join-entity/src/main/java/com/roytuts/spring/jpa/bidirectional/manytomany/without/join/entity/service/ManyToManyService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.bidirectional.manytomany.without.join.entity.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | 6 | import com.roytuts.spring.jpa.bidirectional.manytomany.without.join.entity.entity.Artist; 7 | import com.roytuts.spring.jpa.bidirectional.manytomany.without.join.entity.entity.Cd; 8 | import com.roytuts.spring.jpa.bidirectional.manytomany.without.join.entity.repository.ArtistRepository; 9 | import com.roytuts.spring.jpa.bidirectional.manytomany.without.join.entity.repository.CdRepository; 10 | 11 | @Service 12 | public class ManyToManyService { 13 | 14 | @Autowired 15 | private CdRepository cdRepository; 16 | 17 | @Autowired 18 | private ArtistRepository artistRepository; 19 | 20 | public void saveCd(Cd cd) { 21 | cdRepository.save(cd); 22 | } 23 | 24 | public void saveArtist(Artist artist) { 25 | artistRepository.save(artist); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytomany-without-join-entity/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 2 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | 6 | spring.jpa.show-sql = true 7 | spring.jpa.properties.hibernate.format_sql=true 8 | 9 | logging.level.org.hibernate.type.descriptor.sql=trace 10 | 11 | spring.jpa.hibernate.ddl-auto = create 12 | #spring.jpa.hibernate.ddl-auto = none -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytoone-onetomany/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-jpa-bidirectional-manytoone-onetomany 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 16 15 | 16 16 | 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-parent 22 | 2.5.6 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-data-jpa 34 | 35 | 36 | 37 | mysql 38 | mysql-connector-java 39 | 40 | 41 | 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-maven-plugin 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytoone-onetomany/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-data-jpa-bidirectional-many-to-oneone-to-many-relationship/ -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytoone-onetomany/src/main/java/com/roytuts/spring/jpa/bidirectional/manytoone/onetomany/entity/Artist.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.bidirectional.manytoone.onetomany.entity; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.GeneratedValue; 10 | import javax.persistence.GenerationType; 11 | import javax.persistence.Id; 12 | import javax.persistence.OneToMany; 13 | import javax.persistence.Table; 14 | 15 | @Table 16 | @Entity 17 | public class Artist { 18 | 19 | @Id 20 | @GeneratedValue(strategy = GenerationType.IDENTITY) 21 | private int artistId; 22 | 23 | @Column 24 | private String artistName; 25 | 26 | @OneToMany(mappedBy = "artist", cascade = CascadeType.ALL) 27 | private Set cds = new HashSet<>(); 28 | 29 | public int getArtistId() { 30 | return artistId; 31 | } 32 | 33 | public void setArtistId(int artistId) { 34 | this.artistId = artistId; 35 | } 36 | 37 | public String getArtistName() { 38 | return artistName; 39 | } 40 | 41 | public void setArtistName(String artistName) { 42 | this.artistName = artistName; 43 | } 44 | 45 | public Set getCds() { 46 | return cds; 47 | } 48 | 49 | public void setCds(Set cds) { 50 | this.cds = cds; 51 | 52 | for (Cd cd : cds) { 53 | cd.setArtist(this); 54 | } 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytoone-onetomany/src/main/java/com/roytuts/spring/jpa/bidirectional/manytoone/onetomany/entity/Cd.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.bidirectional.manytoone.onetomany.entity; 2 | 3 | import javax.persistence.CascadeType; 4 | import javax.persistence.Column; 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.GenerationType; 8 | import javax.persistence.Id; 9 | import javax.persistence.JoinColumn; 10 | import javax.persistence.ManyToOne; 11 | import javax.persistence.Table; 12 | 13 | @Table 14 | @Entity 15 | public class Cd { 16 | 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private int cdId; 20 | 21 | @Column 22 | private String cdTitle; 23 | 24 | @JoinColumn(name = "artistId") 25 | @ManyToOne(cascade = CascadeType.ALL, optional = false) 26 | private Artist artist; 27 | 28 | public int getCdId() { 29 | return cdId; 30 | } 31 | 32 | public void setCdId(int cdId) { 33 | this.cdId = cdId; 34 | } 35 | 36 | public String getCdTitle() { 37 | return cdTitle; 38 | } 39 | 40 | public void setCdTitle(String cdTitle) { 41 | this.cdTitle = cdTitle; 42 | } 43 | 44 | public Artist getArtist() { 45 | return artist; 46 | } 47 | 48 | public void setArtist(Artist artist) { 49 | this.artist = artist; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytoone-onetomany/src/main/java/com/roytuts/spring/jpa/bidirectional/manytoone/onetomany/repository/ArtistRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.bidirectional.manytoone.onetomany.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.jpa.bidirectional.manytoone.onetomany.entity.Artist; 6 | 7 | public interface ArtistRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytoone-onetomany/src/main/java/com/roytuts/spring/jpa/bidirectional/manytoone/onetomany/repository/CdRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.bidirectional.manytoone.onetomany.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.jpa.bidirectional.manytoone.onetomany.entity.Cd; 6 | 7 | public interface CdRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytoone-onetomany/src/main/java/com/roytuts/spring/jpa/bidirectional/manytoone/onetomany/service/ManyToOneOneToManyService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.bidirectional.manytoone.onetomany.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.roytuts.spring.jpa.bidirectional.manytoone.onetomany.entity.Artist; 9 | import com.roytuts.spring.jpa.bidirectional.manytoone.onetomany.entity.Cd; 10 | import com.roytuts.spring.jpa.bidirectional.manytoone.onetomany.repository.ArtistRepository; 11 | import com.roytuts.spring.jpa.bidirectional.manytoone.onetomany.repository.CdRepository; 12 | 13 | @Service 14 | public class ManyToOneOneToManyService { 15 | 16 | @Autowired 17 | private CdRepository cdRepository; 18 | 19 | @Autowired 20 | private ArtistRepository artistRepository; 21 | 22 | public void saveCdArtistList(List cds) { 23 | cdRepository.saveAll(cds); 24 | } 25 | 26 | public void saveArtistCdList(Artist artist) { 27 | artistRepository.save(artist); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /spring-jpa-bidirectional-manytoone-onetomany/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 2 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | 6 | spring.jpa.show-sql = true 7 | spring.jpa.properties.hibernate.format_sql=true 8 | 9 | logging.level.org.hibernate.type.descriptor.sql=trace 10 | 11 | spring.jpa.hibernate.ddl-auto = create 12 | #spring.jpa.hibernate.ddl-auto = none -------------------------------------------------------------------------------- /spring-jpa-unidirectional-manytoone/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-jpa-unidirectional-manytoone 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 16 15 | 16 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 2.5.6 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-data-jpa 33 | 34 | 35 | 36 | mysql 37 | mysql-connector-java 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-jpa-unidirectional-manytoone/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-boot-data-jpa-unidirectional-many-to-one-relationship/ -------------------------------------------------------------------------------- /spring-jpa-unidirectional-manytoone/src/main/java/com/roytuts/spring/jpa/unidirectional/manytoone/ManyToOneUnidirectionalApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.unidirectional.manytoone; 2 | 3 | import java.util.Arrays; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | 10 | import com.roytuts.spring.jpa.unidirectional.manytoone.entity.Artist; 11 | import com.roytuts.spring.jpa.unidirectional.manytoone.entity.Cd; 12 | import com.roytuts.spring.jpa.unidirectional.manytoone.service.ManyToOneService; 13 | 14 | @SpringBootApplication 15 | public class ManyToOneUnidirectionalApp implements CommandLineRunner { 16 | 17 | @Autowired 18 | private ManyToOneService manyToOneService; 19 | 20 | public static void main(String[] args) { 21 | SpringApplication.run(ManyToOneUnidirectionalApp.class, args); 22 | } 23 | 24 | @Override 25 | public void run(String... args) throws Exception { 26 | // create Artist object 27 | Artist artist = new Artist(); 28 | artist.setArtistName("Soumitra"); 29 | 30 | // create CD 31 | Cd cd1 = new Cd(); 32 | cd1.setCdTitle("Java"); 33 | cd1.setArtist(artist); 34 | 35 | // create another CD 36 | Cd cd2 = new Cd(); 37 | cd2.setCdTitle("PHP"); 38 | cd2.setArtist(artist); 39 | 40 | manyToOneService.saveCdArtist(Arrays.asList(cd1, cd2)); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /spring-jpa-unidirectional-manytoone/src/main/java/com/roytuts/spring/jpa/unidirectional/manytoone/entity/Artist.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.unidirectional.manytoone.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | 10 | @Table 11 | @Entity 12 | public class Artist { 13 | 14 | @Id 15 | @GeneratedValue(strategy = GenerationType.IDENTITY) 16 | private int artistId; 17 | 18 | @Column 19 | private String artistName; 20 | 21 | public int getArtistId() { 22 | return artistId; 23 | } 24 | 25 | public void setArtistId(int artistId) { 26 | this.artistId = artistId; 27 | } 28 | 29 | public String getArtistName() { 30 | return artistName; 31 | } 32 | 33 | public void setArtistName(String artistName) { 34 | this.artistName = artistName; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /spring-jpa-unidirectional-manytoone/src/main/java/com/roytuts/spring/jpa/unidirectional/manytoone/entity/Cd.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.unidirectional.manytoone.entity; 2 | 3 | import javax.persistence.CascadeType; 4 | import javax.persistence.Column; 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.GenerationType; 8 | import javax.persistence.Id; 9 | import javax.persistence.JoinColumn; 10 | import javax.persistence.ManyToOne; 11 | import javax.persistence.Table; 12 | 13 | @Table 14 | @Entity 15 | public class Cd { 16 | 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private int cdId; 20 | 21 | @Column 22 | private String cdTitle; 23 | 24 | @JoinColumn(name = "artistId") 25 | @ManyToOne(cascade = CascadeType.ALL, optional = false) 26 | private Artist artist; 27 | 28 | public int getCdId() { 29 | return cdId; 30 | } 31 | 32 | public void setCdId(int cdId) { 33 | this.cdId = cdId; 34 | } 35 | 36 | public String getCdTitle() { 37 | return cdTitle; 38 | } 39 | 40 | public void setCdTitle(String cdTitle) { 41 | this.cdTitle = cdTitle; 42 | } 43 | 44 | public Artist getArtist() { 45 | return artist; 46 | } 47 | 48 | public void setArtist(Artist artist) { 49 | this.artist = artist; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /spring-jpa-unidirectional-manytoone/src/main/java/com/roytuts/spring/jpa/unidirectional/manytoone/repository/CdRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.unidirectional.manytoone.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.jpa.unidirectional.manytoone.entity.Cd; 6 | 7 | public interface CdRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-jpa-unidirectional-manytoone/src/main/java/com/roytuts/spring/jpa/unidirectional/manytoone/service/ManyToOneService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jpa.unidirectional.manytoone.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.roytuts.spring.jpa.unidirectional.manytoone.entity.Cd; 9 | import com.roytuts.spring.jpa.unidirectional.manytoone.repository.CdRepository; 10 | 11 | @Service 12 | public class ManyToOneService { 13 | 14 | @Autowired 15 | private CdRepository cdRepository; 16 | 17 | public void saveCdArtist(List cds) { 18 | cdRepository.saveAll(cds); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-jpa-unidirectional-manytoone/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | The following datasource configuration is done in the src/main/resources/application.properties file: 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | 7 | spring.jpa.show-sql = true 8 | spring.jpa.properties.hibernate.format_sql=true 9 | 10 | logging.level.org.hibernate.type.descriptor.sql=trace 11 | 12 | spring.jpa.hibernate.ddl-auto = create 13 | #spring.jpa.hibernate.ddl-auto = none -------------------------------------------------------------------------------- /springboot-data-jpa-composite-primary-key/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-boot-composite-primary-key-example/ 2 | -------------------------------------------------------------------------------- /springboot-data-jpa-composite-primary-key/spring_340/src/main/java/com/roytuts/springboot/datajpa/composite/primarykey/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.datajpa.composite.primarykey.entity; 2 | 3 | import jakarta.persistence.EmbeddedId; 4 | import jakarta.persistence.Entity; 5 | import jakarta.persistence.Table; 6 | 7 | @Entity 8 | @Table // (name = "user") 9 | public class User { 10 | 11 | //@GeneratedValue(strategy = GenerationType.IDENTITY) 12 | private int id; 13 | 14 | @EmbeddedId 15 | private UserPKey userPKey; 16 | 17 | public int getId() { 18 | return id; 19 | } 20 | 21 | public void setId(int id) { 22 | this.id = id; 23 | } 24 | 25 | @Override 26 | public String toString() { 27 | return "User [id=" + id + ", userPKey=" + userPKey + "]"; 28 | } 29 | 30 | public UserPKey getUserPKey() { 31 | return userPKey; 32 | } 33 | 34 | public void setUserPKey(UserPKey userPKey) { 35 | this.userPKey = userPKey; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /springboot-data-jpa-composite-primary-key/spring_340/src/main/java/com/roytuts/springboot/datajpa/composite/primarykey/entity/UserPKey.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.datajpa.composite.primarykey.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Objects; 5 | 6 | import jakarta.persistence.Column; 7 | import jakarta.persistence.Embeddable; 8 | 9 | @Embeddable 10 | public class UserPKey implements Serializable { 11 | 12 | private static final long serialVersionUID = 1L; 13 | 14 | @Column(name = "first_name") 15 | private String firstName; 16 | 17 | @Column(name = "last_name") 18 | private String lastName; 19 | 20 | public UserPKey() { 21 | } 22 | 23 | public UserPKey(String firstName, String lastName) { 24 | this.firstName = firstName; 25 | this.lastName = lastName; 26 | } 27 | 28 | public String getFirstName() { 29 | return firstName; 30 | } 31 | 32 | public String getLastName() { 33 | return lastName; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return Objects.hash(getFirstName(), getLastName()); 39 | } 40 | 41 | @Override 42 | public boolean equals(Object obj) { 43 | if (this == obj) 44 | return true; 45 | if (obj == null) 46 | return false; 47 | if (getClass() != obj.getClass()) 48 | return false; 49 | UserPKey other = (UserPKey) obj; 50 | 51 | return Objects.equals(getFirstName(), other.getFirstName()) 52 | && Objects.equals(getLastName(), other.getLastName()); 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | return "CompositePKey [firstName=" + firstName + ", lastName=" + lastName + "]"; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /springboot-data-jpa-composite-primary-key/spring_340/src/main/java/com/roytuts/springboot/datajpa/composite/primarykey/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.datajpa.composite.primarykey.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.springboot.datajpa.composite.primarykey.entity.User; 6 | import com.roytuts.springboot.datajpa.composite.primarykey.entity.UserPKey; 7 | 8 | public interface UserRepository extends JpaRepository { 9 | 10 | User findByUserPKey(UserPKey userPKey); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /springboot-data-jpa-composite-primary-key/spring_340/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost/roytuts 2 | spring.datasource.username=root 3 | spring.datasource.password=root 4 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 5 | -------------------------------------------------------------------------------- /springboot-data-jpa-composite-primary-key/src/main/java/com/roytuts/springboot/datajpa/composite/primarykey/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.datajpa.composite.primarykey.entity; 2 | 3 | import javax.persistence.EmbeddedId; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Table; 8 | 9 | @Entity 10 | @Table // (name = "user") 11 | public class User { 12 | 13 | @GeneratedValue(strategy = GenerationType.IDENTITY) 14 | private int id; 15 | 16 | @EmbeddedId 17 | private UserPKey userPKey; 18 | 19 | public int getId() { 20 | return id; 21 | } 22 | 23 | public void setId(int id) { 24 | this.id = id; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return "User [id=" + id + ", userPKey=" + userPKey + "]"; 30 | } 31 | 32 | public UserPKey getUserPKey() { 33 | return userPKey; 34 | } 35 | 36 | public void setUserPKey(UserPKey userPKey) { 37 | this.userPKey = userPKey; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /springboot-data-jpa-composite-primary-key/src/main/java/com/roytuts/springboot/datajpa/composite/primarykey/entity/UserPKey.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.datajpa.composite.primarykey.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Objects; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Embeddable; 8 | 9 | @Embeddable 10 | public class UserPKey implements Serializable { 11 | 12 | private static final long serialVersionUID = 1L; 13 | 14 | @Column(name = "first_name") 15 | private String firstName; 16 | 17 | @Column(name = "last_name") 18 | private String lastName; 19 | 20 | public UserPKey() { 21 | } 22 | 23 | public UserPKey(String firstName, String lastName) { 24 | this.firstName = firstName; 25 | this.lastName = lastName; 26 | } 27 | 28 | public String getFirstName() { 29 | return firstName; 30 | } 31 | 32 | public String getLastName() { 33 | return lastName; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | return Objects.hash(getFirstName(), getLastName()); 39 | } 40 | 41 | @Override 42 | public boolean equals(Object obj) { 43 | if (this == obj) 44 | return true; 45 | if (obj == null) 46 | return false; 47 | if (getClass() != obj.getClass()) 48 | return false; 49 | UserPKey other = (UserPKey) obj; 50 | 51 | return Objects.equals(getFirstName(), other.getFirstName()) 52 | && Objects.equals(getLastName(), other.getLastName()); 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | return "CompositePKey [firstName=" + firstName + ", lastName=" + lastName + "]"; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /springboot-data-jpa-composite-primary-key/src/main/java/com/roytuts/springboot/datajpa/composite/primarykey/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.datajpa.composite.primarykey.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.springboot.datajpa.composite.primarykey.entity.User; 6 | import com.roytuts.springboot.datajpa.composite.primarykey.entity.UserPKey; 7 | 8 | public interface UserRepository extends JpaRepository { 9 | 10 | User findByUserPKey(UserPKey userPKey); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /springboot-data-jpa-composite-primary-key/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost/roytuts 2 | spring.datasource.username=root 3 | spring.datasource.password=root 4 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 5 | -------------------------------------------------------------------------------- /springboot-data-jpa-composite-primary-key/user.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE user ( 2 | id int unsigned COLLATE utf8mb4_unicode_ci NOT NULL, 3 | first_name varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, 4 | last_name varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, 5 | PRIMARY KEY (last_name,first_name) 6 | )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 7 | 8 | INSERT INTO `user` (`id`, `first_name`, `last_name`) VALUES 9 | (3, 'Arup', 'Roy'), 10 | (1, 'Soumitra', 'Roy'), 11 | (2, 'Liton', 'Sarkar'); 12 | 13 | -------------------------------------------------------------------------------- /springboot-mapstruct-jpa-entity-dto-mapping/readme.rst: -------------------------------------------------------------------------------- 1 | https://roytuts.com/spring-boot-data-jpa-entity-dto-mapping-using-mapstruct/ 2 | -------------------------------------------------------------------------------- /springboot-mapstruct-jpa-entity-dto-mapping/src/main/java/com/roytuts/springboot/mapstruct/jpa/entity/dto/mapping/mapper/EventMapper.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.mapstruct.jpa.entity.dto.mapping.mapper; 2 | 3 | import java.util.List; 4 | 5 | import org.mapstruct.Mapper; 6 | 7 | import com.roytuts.springboot.mapstruct.jpa.entity.dto.mapping.dto.EventDto; 8 | import com.roytuts.springboot.mapstruct.jpa.entity.dto.mapping.entity.Event; 9 | 10 | @Mapper(componentModel = "spring") 11 | public interface EventMapper { 12 | 13 | EventDto toEventDto(Event event); 14 | 15 | List toEventDtos(List events); 16 | 17 | Event toEvent(EventDto eventDto); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /springboot-mapstruct-jpa-entity-dto-mapping/src/main/java/com/roytuts/springboot/mapstruct/jpa/entity/dto/mapping/repository/EventRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.mapstruct.jpa.entity.dto.mapping.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.springboot.mapstruct.jpa.entity.dto.mapping.entity.Event; 6 | 7 | public interface EventRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /springboot-mapstruct-jpa-entity-dto-mapping/src/main/java/com/roytuts/springboot/mapstruct/jpa/entity/dto/mapping/service/EventService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.mapstruct.jpa.entity.dto.mapping.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.roytuts.springboot.mapstruct.jpa.entity.dto.mapping.dto.EventDto; 9 | import com.roytuts.springboot.mapstruct.jpa.entity.dto.mapping.entity.Event; 10 | import com.roytuts.springboot.mapstruct.jpa.entity.dto.mapping.mapper.EventMapper; 11 | import com.roytuts.springboot.mapstruct.jpa.entity.dto.mapping.repository.EventRepository; 12 | 13 | @Service 14 | public class EventService { 15 | 16 | @Autowired 17 | private EventRepository eventRepository; 18 | 19 | @Autowired 20 | private EventMapper eventMapper; 21 | 22 | public List getEvents() { 23 | return eventRepository.findAll(); 24 | } 25 | 26 | public List getEventDtos() { 27 | return eventMapper.toEventDtos(eventRepository.findAll()); 28 | } 29 | 30 | public EventDto getEventDto(Integer id) { 31 | return eventMapper.toEventDto(eventRepository.findById(id).orElseThrow()); 32 | } 33 | 34 | public Event getEvent(Integer id) { 35 | return eventMapper.toEvent(getEventDto(id)); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /springboot-mapstruct-jpa-entity-dto-mapping/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Spring Datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | 7 | #SQL related 8 | spring.jpa.show-sql = true 9 | spring.jpa.properties.hibernate.format_sql=true 10 | 11 | spring.jpa.hibernate.ddl-auto = none 12 | --------------------------------------------------------------------------------