Move code to common module - Spring Data JPA (#15733)
* JAVA-30484: Move code to new module - Spring Data JPA * JAVA-30484: remove the duplicate code from the old modules.
This commit is contained in:
@@ -82,7 +82,6 @@
|
||||
<module>spring-data-jpa-annotations</module>
|
||||
<module>spring-data-jpa-annotations-2</module>
|
||||
<module>spring-data-jpa-crud</module>
|
||||
<module>spring-data-jpa-crud-2</module>
|
||||
<module>spring-data-jpa-enterprise</module>
|
||||
<module>spring-data-jpa-enterprise-2</module>
|
||||
<module>spring-data-jpa-filtering</module>
|
||||
@@ -93,6 +92,7 @@
|
||||
<module>spring-data-jpa-repo-2</module>
|
||||
<module>spring-data-jpa-repo-4</module>
|
||||
<module>spring-data-jdbc</module>
|
||||
<module>spring-data-jpa-simple</module>
|
||||
<module>spring-data-keyvalue</module>
|
||||
<module>spring-data-mongodb</module>
|
||||
<module>spring-data-mongodb-2</module>
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
## Spring Data JPA - CRUD
|
||||
|
||||
This module contains articles about CRUD operations in Spring Data JPA
|
||||
|
||||
### Relevant Articles:
|
||||
- [Generate Database Schema with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-generate-db-schema)
|
||||
- More articles: [[<--prev]](/persistence-modules/spring-data-jpa-crud)
|
||||
|
||||
### Eclipse Config
|
||||
After importing the project into Eclipse, you may see the following error:
|
||||
"No persistence xml file found in project"
|
||||
|
||||
This can be ignored:
|
||||
- Project -> Properties -> Java Persistance -> JPA -> Error/Warnings -> Select Ignore on "No persistence xml file found in project"
|
||||
Or:
|
||||
- Eclipse -> Preferences - Validation - disable the "Build" execution of the JPA Validator
|
||||
@@ -1,32 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-data-jpa-crud-2</artifactId>
|
||||
<name>spring-data-jpa-crud-2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
-72
@@ -1,72 +0,0 @@
|
||||
package com.baeldung.schemageneration;
|
||||
|
||||
import com.baeldung.schemageneration.model.Account;
|
||||
import com.baeldung.schemageneration.model.AccountSetting;
|
||||
import com.baeldung.schemageneration.repository.AccountRepository;
|
||||
import com.baeldung.schemageneration.repository.AccountSettingRepository;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = AccountApplication.class)
|
||||
public class AccountRepositoryIntegrationTest {
|
||||
|
||||
private static final String USER_NAME = "Eduard";
|
||||
private static final String USER_EMAIL_ADDRESS = "[email protected]";
|
||||
private static final String ACCOUNT_SETTING_NAME = "Timezone";
|
||||
private static final String ACCOUNT_SETTING_VALUE = "UTC+02";
|
||||
|
||||
@Autowired
|
||||
private AccountRepository accountRepository;
|
||||
|
||||
@Autowired
|
||||
private AccountSettingRepository accountSettingRepository;
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
accountRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNewAccount_whenSave_thenSuccess() {
|
||||
Account account = new Account(USER_NAME, USER_EMAIL_ADDRESS);
|
||||
accountRepository.save(account);
|
||||
|
||||
assertEquals(1, accountRepository.count());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSavedAccount_whenFindByName_thenFound() {
|
||||
Account account = new Account(USER_NAME, USER_EMAIL_ADDRESS);
|
||||
accountRepository.save(account);
|
||||
|
||||
Account accountFound = accountRepository.findByName(USER_NAME);
|
||||
|
||||
assertNotNull(accountFound);
|
||||
assertEquals(USER_NAME, accountFound.getName());
|
||||
assertEquals(USER_EMAIL_ADDRESS, accountFound.getEmailAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSavedAccount_whenAccountSettingIsAdded_thenPersisted() {
|
||||
Account account = new Account(USER_NAME, USER_EMAIL_ADDRESS);
|
||||
account.addAccountSetting(new AccountSetting(ACCOUNT_SETTING_NAME, ACCOUNT_SETTING_VALUE));
|
||||
accountRepository.save(account);
|
||||
|
||||
Account accountFound = accountRepository.findByName(USER_NAME);
|
||||
assertNotNull(accountFound);
|
||||
AccountSetting accountSetting = accountSettingRepository.findByAccountId(accountFound.getId());
|
||||
|
||||
assertNotNull(accountSetting);
|
||||
assertEquals(ACCOUNT_SETTING_NAME, accountSetting.getSettingName());
|
||||
assertEquals(ACCOUNT_SETTING_VALUE, accountSetting.getSettingValue());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,9 +8,7 @@ This module contains articles about Spring Data JPA used in enterprise applicati
|
||||
- [DB Integration Tests with Spring Boot and Testcontainers](https://www.baeldung.com/spring-boot-testcontainers-integration-test)
|
||||
- [A Guide to Spring’s Open Session in View](https://www.baeldung.com/spring-open-session-in-view)
|
||||
- [Partial Data Update With Spring Data](https://www.baeldung.com/spring-data-partial-update)
|
||||
- [Spring Data JPA @Modifying Annotation](https://www.baeldung.com/spring-data-jpa-modifying-annotation)
|
||||
- [Spring JPA – Multiple Databases](https://www.baeldung.com/spring-data-jpa-multiple-databases)
|
||||
- [Pagination and Sorting using Spring Data JPA](https://www.baeldung.com/spring-data-jpa-pagination-sorting)
|
||||
|
||||
### Eclipse Config
|
||||
After importing the project into Eclipse, you may see the following error:
|
||||
|
||||
@@ -6,7 +6,6 @@ This module contains articles about filtering data using Spring Data JPA
|
||||
- [An Advanced Tagging Implementation with JPA](https://www.baeldung.com/jpa-tagging-advanced)
|
||||
- [A Simple Tagging Implementation with JPA](https://www.baeldung.com/jpa-tagging)
|
||||
- [Spring Data JPA and Null Parameters](https://www.baeldung.com/spring-data-jpa-null-parameters)
|
||||
- [Spring Data JPA Projections](https://www.baeldung.com/spring-data-jpa-projections)
|
||||
|
||||
### Eclipse Config
|
||||
After importing the project into Eclipse, you may see the following error:
|
||||
|
||||
@@ -4,7 +4,6 @@ This module contains articles about querying data using Spring Data JPA .
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Spring Data JPA @Query](https://www.baeldung.com/spring-data-jpa-query)
|
||||
- [Use Criteria Queries in a Spring Data Application](https://www.baeldung.com/spring-data-criteria-queries)
|
||||
- [Hibernate Pagination](https://www.baeldung.com/hibernate-pagination)
|
||||
- [Sorting with Hibernate](https://www.baeldung.com/hibernate-sort)
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.derivedquery;
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@@ -10,4 +10,4 @@ public class QueryApplication {
|
||||
SpringApplication.run(QueryApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ This module contains articles about querying data using Spring Data JPA
|
||||
|
||||
### Relevant Articles:
|
||||
- [The Exists Query in Spring Data](https://www.baeldung.com/spring-data-exists-query)
|
||||
- [Customizing the Result of JPA Queries with Aggregation Functions](https://www.baeldung.com/jpa-queries-custom-result-with-aggregation-functions)
|
||||
- [Limiting Query Results With JPA and Spring Data JPA](https://www.baeldung.com/jpa-limit-query-results)
|
||||
- [Sorting Query Results with Spring Data](https://www.baeldung.com/spring-data-sorting)
|
||||
- [Spring Data JPA Query by Example](https://www.baeldung.com/spring-data-query-by-example)
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
This module contains articles about Spring Data JPA.
|
||||
|
||||
### Relevant Articles:
|
||||
- [New CRUD Repository Interfaces in Spring Data 3](https://www.baeldung.com/spring-data-3-crud-repository-interfaces)
|
||||
- [How to Persist a List of String in JPA?](https://www.baeldung.com/java-jpa-persist-string-list)
|
||||
- [Hibernate Natural IDs in Spring Boot](https://www.baeldung.com/spring-boot-hibernate-natural-ids)
|
||||
- [Correct Use of flush() in JPA](https://www.baeldung.com/spring-jpa-flush)
|
||||
|
||||
+2
-2
@@ -10,14 +10,14 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import java.util.Arrays;
|
||||
|
||||
@SpringBootTest
|
||||
public class LibraryIntegrationTest {
|
||||
class LibraryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private LibraryRepository libraryRepository;
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenLibrary_whenGetAddressesAndGetBooks_thenGetListOfItems(){
|
||||
void givenLibrary_whenGetAddressesAndGetBooks_thenGetListOfItems(){
|
||||
Library library = new Library();
|
||||
library.setAddresses(Arrays.asList("Address 1", "Address 2"));
|
||||
library.setBooks(Arrays.asList("Book 1", "Book 2"));
|
||||
|
||||
@@ -2,10 +2,8 @@
|
||||
|
||||
This module contains articles about repositories in Spring Data JPA
|
||||
|
||||
### Relevant Articles:
|
||||
- [Introduction to Spring Data JPA](https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa)
|
||||
### Relevant Articles:
|
||||
- [Case Insensitive Queries with Spring Data Repository](https://www.baeldung.com/spring-data-case-insensitive-queries)
|
||||
- [Derived Query Methods in Spring Data JPA Repositories](https://www.baeldung.com/spring-data-derived-queries)
|
||||
- [Spring Data – CrudRepository save() Method](https://www.baeldung.com/spring-data-crud-repository-save)
|
||||
- [Spring Data JPA – Adding a Method in All Repositories](https://www.baeldung.com/spring-data-jpa-method-in-all-repositories)
|
||||
- [Spring Data Composable Repositories](https://www.baeldung.com/spring-data-composable-repositories)
|
||||
|
||||
-172
@@ -1,172 +0,0 @@
|
||||
package com.baeldung.derivedquery.repository;
|
||||
|
||||
import com.baeldung.derivedquery.QueryApplication;
|
||||
import com.baeldung.derivedquery.entity.User;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = QueryApplication.class)
|
||||
public class UserRepositoryIntegrationTest {
|
||||
|
||||
private static final String USER_NAME_ADAM = "Adam";
|
||||
private static final String USER_NAME_EVE = "Eve";
|
||||
private static final ZonedDateTime BIRTHDATE = ZonedDateTime.now();
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
User user1 = new User(USER_NAME_ADAM, 25, BIRTHDATE, true);
|
||||
User user2 = new User(USER_NAME_ADAM, 20, BIRTHDATE, false);
|
||||
User user3 = new User(USER_NAME_EVE, 20, BIRTHDATE, true);
|
||||
User user4 = new User(null, 30, BIRTHDATE, false);
|
||||
|
||||
userRepository.saveAll(Arrays.asList(user1, user2, user3, user4));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
|
||||
userRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindByName_thenReturnsCorrectResult() {
|
||||
|
||||
assertEquals(2, userRepository.findByName(USER_NAME_ADAM).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindByNameIsNull_thenReturnsCorrectResult() {
|
||||
|
||||
assertEquals(1, userRepository.findByNameIsNull().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindByNameNot_thenReturnsCorrectResult() {
|
||||
|
||||
assertEquals(USER_NAME_EVE, userRepository.findByNameNot(USER_NAME_ADAM).get(0).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindByNameStartingWith_thenReturnsCorrectResult() {
|
||||
|
||||
assertEquals(2, userRepository.findByNameStartingWith("A").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindByNameEndingWith_thenReturnsCorrectResult() {
|
||||
|
||||
assertEquals(1, userRepository.findByNameEndingWith("e").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenByNameContaining_thenReturnsCorrectResult() {
|
||||
|
||||
assertEquals(1, userRepository.findByNameContaining("v").size());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenByNameLike_thenReturnsCorrectResult() {
|
||||
|
||||
assertEquals(2, userRepository.findByNameEndingWith("m").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenByAgeLessThan_thenReturnsCorrectResult() {
|
||||
|
||||
assertEquals(2, userRepository.findByAgeLessThan(25).size());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenByAgeLessThanEqual_thenReturnsCorrectResult() {
|
||||
|
||||
assertEquals(3, userRepository.findByAgeLessThanEqual(25).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenByAgeGreaterThan_thenReturnsCorrectResult() {
|
||||
|
||||
assertEquals(1, userRepository.findByAgeGreaterThan(25).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenByAgeGreaterThanEqual_thenReturnsCorrectResult() {
|
||||
|
||||
assertEquals(2, userRepository.findByAgeGreaterThanEqual(25).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenByAgeBetween_thenReturnsCorrectResult() {
|
||||
|
||||
assertEquals(4, userRepository.findByAgeBetween(20, 30).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenByBirthDateAfter_thenReturnsCorrectResult() {
|
||||
|
||||
final ZonedDateTime yesterday = BIRTHDATE.minusDays(1);
|
||||
assertEquals(4, userRepository.findByBirthDateAfter(yesterday).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenByBirthDateBefore_thenReturnsCorrectResult() {
|
||||
|
||||
final ZonedDateTime yesterday = BIRTHDATE.minusDays(1);
|
||||
assertEquals(0, userRepository.findByBirthDateBefore(yesterday).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenByActiveTrue_thenReturnsCorrectResult() {
|
||||
|
||||
assertEquals(2, userRepository.findByActiveTrue().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenByActiveFalse_thenReturnsCorrectResult() {
|
||||
|
||||
assertEquals(2, userRepository.findByActiveFalse().size());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenByAgeIn_thenReturnsCorrectResult() {
|
||||
|
||||
final List<Integer> ages = Arrays.asList(20, 25);
|
||||
assertEquals(3, userRepository.findByAgeIn(ages).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenByNameOrAge() {
|
||||
|
||||
assertEquals(3, userRepository.findByNameOrAge(USER_NAME_ADAM, 20).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenByNameOrAgeAndActive() {
|
||||
|
||||
assertEquals(2, userRepository.findByNameOrAgeAndActive(USER_NAME_ADAM, 20, false).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenByNameOrderByName() {
|
||||
|
||||
assertEquals(2, userRepository.findByNameOrderByName(USER_NAME_ADAM).size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
### Spring Data JPA Articles that are also part of the e-book
|
||||
|
||||
This module contains articles about Spring Data JPA that are also part of an Ebook.
|
||||
|
||||
|
||||
### NOTE:
|
||||
|
||||
Since this is a module tied to an e-book, it should **not** be moved or used to store the code for any further article.
|
||||
|
||||
### Relevant Articles
|
||||
- [Introduction to Spring Data JPA](https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa)
|
||||
- [Customizing the Result of JPA Queries with Aggregation Functions](https://www.baeldung.com/jpa-queries-custom-result-with-aggregation-functions)
|
||||
- [CrudRepository, JpaRepository, and PagingAndSortingRepository in Spring Data](https://www.baeldung.com/spring-data-repositories)
|
||||
- [New CRUD Repository Interfaces in Spring Data 3](https://www.baeldung.com/spring-data-3-crud-repository-interfaces)
|
||||
- [Derived Query Methods in Spring Data JPA Repositories](https://www.baeldung.com/spring-data-derived-queries)
|
||||
- [Spring Data JPA @Query](https://www.baeldung.com/spring-data-jpa-query)
|
||||
- [Spring Data JPA Projections](https://www.baeldung.com/spring-data-jpa-projections)
|
||||
- [Spring Data JPA @Modifying Annotation](https://www.baeldung.com/spring-data-jpa-modifying-annotation)
|
||||
- [Generate Database Schema with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-generate-db-schema)
|
||||
- [Pagination and Sorting using Spring Data JPA](https://www.baeldung.com/spring-data-jpa-pagination-sorting)
|
||||
@@ -0,0 +1,88 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-data-jpa-simple</artifactId>
|
||||
<name>spring-data-jpa-simple</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-oxm</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.xml.bind</groupId>
|
||||
<artifactId>jakarta.xml.bind-api</artifactId>
|
||||
<version>${jakarta.xml.bind.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-ant</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<hibernate.version>6.4.2.Final</hibernate.version>
|
||||
<jakarta.xml.bind.version>4.0.0</jakarta.xml.bind.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>repackage</id>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.jpa.aggregation;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@SpringBootApplication
|
||||
@PropertySource("classpath:aggregation.properties")
|
||||
@Configuration
|
||||
public class AggregationApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AggregationApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+3
-2
@@ -1,9 +1,10 @@
|
||||
package com.baeldung.aggregation.model;
|
||||
package com.baeldung.jpa.aggregation.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Comment {
|
||||
+5
-3
@@ -1,13 +1,15 @@
|
||||
package com.baeldung.aggregation.model;
|
||||
package com.baeldung.jpa.aggregation.model;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Post {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
private String title;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.aggregation.model.custom;
|
||||
package com.baeldung.jpa.aggregation.model.custom;
|
||||
|
||||
public class CommentCount {
|
||||
private Integer year;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.aggregation.model.custom;
|
||||
package com.baeldung.jpa.aggregation.model.custom;
|
||||
|
||||
public interface ICommentCount {
|
||||
|
||||
+7
-6
@@ -1,13 +1,14 @@
|
||||
package com.baeldung.aggregation.repository;
|
||||
package com.baeldung.jpa.aggregation.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.aggregation.model.Comment;
|
||||
import com.baeldung.aggregation.model.custom.CommentCount;
|
||||
import com.baeldung.aggregation.model.custom.ICommentCount;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import com.baeldung.jpa.aggregation.model.Comment;
|
||||
import com.baeldung.jpa.aggregation.model.custom.CommentCount;
|
||||
import com.baeldung.jpa.aggregation.model.custom.ICommentCount;
|
||||
|
||||
@Repository
|
||||
public interface CommentRepository extends JpaRepository<Comment, Integer> {
|
||||
@@ -15,7 +16,7 @@ public interface CommentRepository extends JpaRepository<Comment, Integer> {
|
||||
@Query("SELECT c.year, COUNT(c.year) FROM Comment AS c GROUP BY c.year ORDER BY c.year DESC")
|
||||
List<Object[]> countTotalCommentsByYear();
|
||||
|
||||
@Query("SELECT new com.baeldung.aggregation.model.custom.CommentCount(c.year, COUNT(c.year)) FROM Comment AS c GROUP BY c.year ORDER BY c.year DESC")
|
||||
@Query("SELECT new com.baeldung.jpa.aggregation.model.custom.CommentCount(c.year, COUNT(c.year)) FROM Comment AS c GROUP BY c.year ORDER BY c.year DESC")
|
||||
List<CommentCount> countTotalCommentsByYearClass();
|
||||
|
||||
@Query("SELECT c.year AS yearComment, COUNT(c.year) AS totalComment FROM Comment AS c GROUP BY c.year ORDER BY c.year DESC")
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.jpa.modifying;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableJpaRepositories("com.baeldung.jpa.modifying")
|
||||
@EntityScan("com.baeldung.jpa.modifying")
|
||||
public class ModifyingApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ModifyingApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.jpa.modifying.dao;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import com.baeldung.jpa.modifying.model.User;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
|
||||
Stream<User> findAllByName(String name);
|
||||
|
||||
void deleteAllByCreationDateAfter(LocalDate date);
|
||||
|
||||
@Query("select u from User u where u.email like '%@gmail.com'")
|
||||
List<User> findUsersWithGmailAddress();
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("update User u set u.active = false where u.lastLoginDate < :date")
|
||||
void deactivateUsersNotLoggedInSince(@Param("date") LocalDate date);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("delete User u where u.active = false")
|
||||
int deleteDeactivatedUsers();
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query(value = "alter table USERS add column deleted int not null default 0", nativeQuery = true)
|
||||
void addDeletedColumn();
|
||||
|
||||
@Query("delete User u where u.active = false")
|
||||
int deleteDeactivatedUsersWithNoModifyingAnnotation();
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package com.baeldung.jpa.modifying.model;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
public class Possession {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public Possession() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Possession(final String name) {
|
||||
super();
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = (prime * result) + (int) (id ^ (id >>> 32));
|
||||
result = (prime * result) + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final Possession other = (Possession) obj;
|
||||
if (id != other.id) {
|
||||
return false;
|
||||
}
|
||||
if (name == null) {
|
||||
if (other.name != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!name.equals(other.name)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("Possesion [id=").append(id).append(", name=").append(name).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
package com.baeldung.jpa.modifying.model;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private int id;
|
||||
private String name;
|
||||
private LocalDate creationDate;
|
||||
private LocalDate lastLoginDate;
|
||||
private boolean active;
|
||||
private int age;
|
||||
@Column(unique = true, nullable = false)
|
||||
private String email;
|
||||
private Integer status;
|
||||
@OneToMany
|
||||
List<Possession> possessionList;
|
||||
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public User(String name, LocalDate creationDate,String email, Integer status) {
|
||||
this.name = name;
|
||||
this.creationDate = creationDate;
|
||||
this.email = email;
|
||||
this.status = status;
|
||||
this.active = true;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(final String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(final int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public LocalDate getCreationDate() {
|
||||
return creationDate;
|
||||
}
|
||||
|
||||
public List<Possession> getPossessionList() {
|
||||
return possessionList;
|
||||
}
|
||||
|
||||
public void setPossessionList(List<Possession> possessionList) {
|
||||
this.possessionList = possessionList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("User [name=").append(name).append(", id=").append(id).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
User user = (User) o;
|
||||
return id == user.id &&
|
||||
age == user.age &&
|
||||
Objects.equals(name, user.name) &&
|
||||
Objects.equals(creationDate, user.creationDate) &&
|
||||
Objects.equals(email, user.email) &&
|
||||
Objects.equals(status, user.status);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name, creationDate, age, email, status);
|
||||
}
|
||||
|
||||
public LocalDate getLastLoginDate() {
|
||||
return lastLoginDate;
|
||||
}
|
||||
|
||||
public void setLastLoginDate(LocalDate lastLoginDate) {
|
||||
this.lastLoginDate = lastLoginDate;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
public void setActive(boolean active) {
|
||||
this.active = active;
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.jpa.paginationsorting;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication(scanBasePackages = "com.baeldung.jpa.paginationsorting")
|
||||
public class PaginationSortingApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PaginationSortingApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.jpa.paginationsorting;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Configuration
|
||||
@PropertySource({ "classpath:pagination-sorting-db.properties" })
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.jpa.paginationsorting", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
|
||||
public class PersistenceProductConfiguration {
|
||||
private final Environment env;
|
||||
|
||||
public PersistenceProductConfiguration(Environment env) {
|
||||
this.env = env;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean productEntityManager() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(productDataSource());
|
||||
em.setPackagesToScan("com.baeldung.jpa.paginationsorting");
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
final HashMap<String, Object> properties = new HashMap<String, Object>();
|
||||
properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||
em.setJpaPropertyMap(properties);
|
||||
|
||||
return em;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource productDataSource() {
|
||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
||||
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("product.jdbc.url")));
|
||||
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
|
||||
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager productTransactionManager() {
|
||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||
transactionManager.setEntityManagerFactory(productEntityManager().getObject());
|
||||
return transactionManager;
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.jpa.paginationsorting.model;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(schema = "products")
|
||||
public class Product {
|
||||
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private double price;
|
||||
|
||||
public Product() {
|
||||
super();
|
||||
}
|
||||
|
||||
private Product(int id, String name, double price) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public static Product from(int id, String name, double price) {
|
||||
return new Product(id, name, price);
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(final double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("Product [name=")
|
||||
.append(name)
|
||||
.append(", id=")
|
||||
.append(id)
|
||||
.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.jpa.paginationsorting.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
import com.baeldung.jpa.paginationsorting.model.Product;
|
||||
|
||||
public interface ProductRepository extends PagingAndSortingRepository<Product, Integer>, CrudRepository<Product, Integer> {
|
||||
|
||||
List<Product> findAllByPrice(double price, Pageable pageable);
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.jpa.projection;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ProjectionApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ProjectionApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user