更新 Guava 项目的依赖和 Spring JPA

This commit is contained in:
YuCheng Hu 2022-07-15 14:31:15 -04:00
parent cd46aea7ac
commit ae29615530
43 changed files with 967 additions and 75 deletions

44
.idea/compiler.xml Normal file
View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="codebank" />
<module name="core-java-collections-list" />
<module name="spring-data-jpa-repo-2" />
<module name="core-java-streams" />
<module name="assertion-libraries" />
<module name="core-java-io" />
<module name="core-java-annotations" />
<module name="core-java-8-2" />
<module name="core-java-numbers" />
<module name="discourse" />
<module name="core-java-strings" />
<module name="core-java-11" />
<module name="core-java-collections-list-2" />
<module name="core-java-collections" />
<module name="core-java-datetime-conversion" />
<module name="core-java-collections-list-3" />
<module name="core-java-8" />
<module name="junit-5-basics" />
<module name="core-java-collections-3" />
<module name="core-java-collections-2" />
<module name="activejdbc" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="core-java-11-2" target="11" />
<module name="hibernate-annotations" target="1.5" />
<module name="hibernate5" target="11" />
</bytecodeTargetLevel>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="core-java-streams" options="-parameters" />
</option>
</component>
</project>

60
.idea/encodings.xml Normal file
View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-11-2/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-11-2/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-11/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-11/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-8-2/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-8-2/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-8/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-8/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-annotations/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-annotations/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-collections-2/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-collections-2/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-collections-3/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-collections-3/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-collections-list-2/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-collections-list-2/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-collections-list-3/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-collections-list-3/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-collections-list/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-collections-list/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-collections/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-collections/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-io/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-io/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-numbers/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-numbers/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-streams/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-streams/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-strings/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-strings/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/activejdbc/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/activejdbc/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/hibernate5/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/hibernate5/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/spring-data-jpa-repo-2/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/spring-data-jpa-repo-2/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/persistence-modules/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/testing-modules/assertion-libraries/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/testing-modules/assertion-libraries/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/testing-modules/junit-5-basics/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/testing-modules/junit-5-basics/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/testing-modules/junit-5-basics/src/test/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/testing-modules/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/testing-modules/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/toolkits/codebank/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/toolkits/codebank/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/toolkits/discourse/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/toolkits/discourse/src/main/resources" charset="UTF-8" />
</component>
</project>

25
.idea/jarRepositories.xml Normal file
View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="geodb-repo" />
<option name="name" value="GeoDB repository" />
<option name="url" value="http://repo.boundlessgeo.com/main/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

6
.idea/thriftCompiler.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ThriftCompiler">
<compilers />
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -18,7 +18,7 @@
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>${guava.version}</version> <version>${version.guava}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mock-server</groupId> <groupId>org.mock-server</groupId>

View File

@ -20,7 +20,7 @@
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>${guava.version}</version> <version>${version.guava}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.openjdk.jmh</groupId> <groupId>org.openjdk.jmh</groupId>

View File

@ -29,7 +29,7 @@
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>log4j</groupId>
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<version>${log4j.version}</version> <version>${version.log4j}</version>
</dependency> </dependency>
<!-- test scoped --> <!-- test scoped -->
<dependency> <dependency>

View File

@ -71,12 +71,12 @@
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>log4j</groupId>
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<version>${log4j.version}</version> <version>${version.log4j}</version>
</dependency> </dependency>
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly --> <dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId> <artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version> <version>${version.slf4j}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>

View File

@ -35,7 +35,7 @@
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>log4j</groupId>
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<version>${log4j.version}</version> <version>${version.log4j}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>

View File

@ -18,7 +18,7 @@
<module>core-java-8</module> <module>core-java-8</module>
<module>core-java-8-2</module> <module>core-java-8-2</module>
<module>core-java-11</module> <module>core-java-11</module>
<module>core-java-11-2</module> <!-- <module>core-java-11-2</module>-->
<module>core-java-annotations</module> <module>core-java-annotations</module>
<module>core-java-collections</module> <module>core-java-collections</module>
<module>core-java-collections-2</module> <module>core-java-collections-2</module>

View File

@ -5,7 +5,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 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> <modelVersion>4.0.0</modelVersion>
<artifactId>parent-boot-2</artifactId> <artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.2-SNAPSHOT</version>
<name>parent-boot-2</name> <name>parent-boot-2</name>
<packaging>pom</packaging> <packaging>pom</packaging>
<description>Parent for all Spring Boot 2 modules</description> <description>Parent for all Spring Boot 2 modules</description>
@ -13,7 +13,7 @@
<parent> <parent>
<groupId>com.ossez</groupId> <groupId>com.ossez</groupId>
<artifactId>parent-modules</artifactId> <artifactId>parent-modules</artifactId>
<version>0.0.1</version> <version>0.0.2-SNAPSHOT</version>
</parent> </parent>
<dependencyManagement> <dependencyManagement>

View File

@ -9,9 +9,9 @@
<url>http://maven.apache.org</url> <url>http://maven.apache.org</url>
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.ossez</groupId>
<artifactId>persistence-modules</artifactId> <artifactId>persistence-modules</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>0.0.2-SNAPSHOT</version>
</parent> </parent>
<dependencies> <dependencies>

View File

@ -8,9 +8,9 @@
<name>hibernate5</name> <name>hibernate5</name>
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.ossez</groupId>
<artifactId>persistence-modules</artifactId> <artifactId>persistence-modules</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>0.0.2-SNAPSHOT</version>
</parent> </parent>
<dependencies> <dependencies>

View File

@ -9,70 +9,82 @@
<parent> <parent>
<groupId>com.ossez</groupId> <groupId>com.ossez</groupId>
<artifactId>parent-modules</artifactId> <artifactId>parent-modules</artifactId>
<version>1.0.0</version> <version>0.0.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent> </parent>
<modules> <modules>
<module>activejdbc</module> <module>activejdbc</module>
<module>apache-cayenne</module> <!-- <module>apache-cayenne</module>-->
<module>core-java-persistence</module> <!-- <module>core-java-persistence</module>-->
<module>deltaspike</module> <!-- <module>deltaspike</module>-->
<module>elasticsearch</module> <!-- <module>elasticsearch</module>-->
<module>flyway</module> <!-- <module>flyway</module>-->
<module>hbase</module> <!-- <module>hbase</module>-->
<module>hibernate5</module> <!-- <module>hibernate5</module>-->
<module>hibernate-mapping</module> <!-- long running --> <!-- <module>hibernate-mapping</module> &lt;!&ndash; long running &ndash;&gt;-->
<module>hibernate-ogm</module> <!-- <module>hibernate-ogm</module>-->
<module>hibernate-annotations</module> <!-- <module>hibernate-annotations</module>-->
<module>hibernate-jpa</module> <!-- <module>hibernate-jpa</module>-->
<module>hibernate-queries</module> <!-- <module>hibernate-queries</module>-->
<module>hibernate-enterprise</module> <!-- <module>hibernate-enterprise</module>-->
<module>influxdb</module> <!-- <module>influxdb</module>-->
<module>java-cassandra</module> <!-- <module>java-cassandra</module>-->
<module>java-cockroachdb</module> <!-- <module>java-cockroachdb</module>-->
<module>java-jdbi</module> <!-- <module>java-jdbi</module>-->
<module>java-jpa</module> <!-- long running --> <!-- <module>java-jpa</module> &lt;!&ndash; long running &ndash;&gt;-->
<module>java-jpa-2</module> <!-- long running --> <!-- <module>java-jpa-2</module> &lt;!&ndash; long running &ndash;&gt;-->
<module>java-mongodb</module> <!-- long running --> <!-- <module>java-mongodb</module> &lt;!&ndash; long running &ndash;&gt;-->
<module>jnosql</module> <!-- long running --> <!-- <module>jnosql</module> &lt;!&ndash; long running &ndash;&gt;-->
<module>jpa-hibernate-cascade-type</module> <!-- <module>jpa-hibernate-cascade-type</module>-->
<module>liquibase</module> <!-- <module>liquibase</module>-->
<module>orientdb</module> <!-- <module>orientdb</module>-->
<module>persistence-libraries</module> <!-- <module>persistence-libraries</module>-->
<module>querydsl</module> <!-- <module>querydsl</module>-->
<module>r2dbc</module> <!-- <module>r2dbc</module>-->
<module>redis</module> <!-- <module>redis</module>-->
<!-- <module>sirix</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 --> <!-- <module>sirix</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 -->
<module>solr</module> <!-- <module>solr</module>-->
<module>spring-boot-persistence-2</module> <!-- <module>spring-boot-persistence-2</module>-->
<module>spring-boot-mysql</module> <!-- <module>spring-boot-mysql</module>-->
<module>spring-boot-persistence</module> <!-- <module>spring-boot-persistence</module>-->
<module>spring-boot-persistence-h2</module> <!-- <module>spring-boot-persistence-h2</module>-->
<module>spring-boot-persistence-mongodb</module> <!-- <module>spring-boot-persistence-mongodb</module>-->
<module>spring-data-cassandra</module> <!-- <module>spring-boot-persistence-mongodb-2</module>-->
<module>spring-data-cassandra-reactive</module> <!-- <module>spring-data-arangodb</module>-->
<module>spring-data-couchbase-2</module> <!-- <module>spring-data-cassandra</module>-->
<module>spring-data-dynamodb</module> <!-- <module>spring-data-cassandra-test</module>-->
<module>spring-data-eclipselink</module> <!-- <module>spring-data-cassandra-reactive</module>-->
<module>spring-data-elasticsearch</module> <!-- <module>spring-data-cosmosdb</module>-->
<module>spring-data-gemfire</module> <!-- <module>spring-data-couchbase-2</module>-->
<module>spring-data-geode</module> <!-- <module>spring-data-dynamodb</module>-->
<module>spring-data-jpa</module> <!-- <module>spring-data-eclipselink</module>-->
<module>spring-data-jpa-2</module> <!-- <module>spring-data-elasticsearch</module>-->
<module>spring-data-jpa-3</module> <!-- &lt;!&ndash; <module>spring-data-gemfire</module> Fixing in JAVA-11854 &ndash;&gt;-->
<module>spring-data-jpa-4</module> <!-- <module>spring-data-geode</module>-->
<module>spring-data-keyvalue</module> <!-- <module>spring-data-jpa-annotations</module>-->
<module>spring-data-mongodb</module> <!-- <module>spring-data-jpa-crud</module>-->
<module>spring-data-neo4j</module> <!-- <module>spring-data-jpa-crud-2</module>-->
<module>spring-data-redis</module> <!-- <module>spring-data-jpa-enterprise</module>-->
<module>spring-data-solr</module> <!-- <module>spring-data-jpa-enterprise-2</module>-->
<module>spring-hibernate-3</module> <!-- <module>spring-data-jpa-filtering</module>-->
<module>spring-hibernate-5</module> <!-- long running --> <!-- <module>spring-data-jpa-query</module>-->
<module>spring-hibernate4</module> <!-- <module>spring-data-jpa-query-2</module>-->
<module>spring-jpa</module> <!-- <module>spring-data-jpa-query-3</module>-->
<!-- <module>spring-data-jpa-repo</module>-->
<module>spring-data-jpa-repo-2</module>
<!-- <module>spring-data-keyvalue</module>-->
<!-- <module>spring-data-mongodb</module>-->
<!-- <module>spring-data-neo4j</module>-->
<!-- <module>spring-data-redis</module>-->
<!-- <module>spring-data-solr</module>-->
<!-- <module>spring-hibernate-3</module>-->
<!-- <module>spring-hibernate-5</module> &lt;!&ndash; long running &ndash;&gt;-->
<!-- <module>spring-hibernate4</module>-->
<!-- <module>spring-jpa</module>-->
<!-- <module>spring-mybatis</module> --> <!-- needs fixing in BAEL-9021 --> <!-- <module>spring-mybatis</module> --> <!-- needs fixing in BAEL-9021 -->
<module>spring-persistence-simple</module> <!-- <module>spring-persistence-simple</module>-->
<module>spring-persistence-simple-2</module> <!-- <module>spring-persistence-simple-2</module>-->
</modules> </modules>
</project> </project>

View File

@ -0,0 +1,9 @@
## Spring Data JPA - Repositories
### Relevant Articles:
- [Introduction to Spring Data JPA](https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa)
- [Performance Difference Between save() and saveAll() in Spring Data](https://www.baeldung.com/spring-data-save-saveall)
- [LIKE Queries in Spring JPA Repositories](https://www.baeldung.com/spring-jpa-like-queries)
- [How to Access EntityManager with Spring Data](https://www.baeldung.com/spring-data-entitymanager)
- More articles: [[<-- prev]](../spring-data-jpa-repo)

View File

@ -0,0 +1,49 @@
<?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-repo-2</artifactId>
<name>spring-data-jpa-repo-2</name>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<parent>
<groupId>com.ossez</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.2-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${version.guava}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,12 @@
package com.baeldung.spring.data.persistence.customrepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CustomRepositoryApplication {
public static void main(String[] args) {
SpringApplication.run(CustomRepositoryApplication.class, args);
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.spring.data.persistence.customrepository.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Objects;
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
User user = (User) o;
return Objects.equals(id, user.id) && Objects.equals(name, user.name) && Objects.equals(email, user.email);
}
@Override
public int hashCode() {
return Objects.hash(id, name, email);
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.spring.data.persistence.customrepository.repository;
import com.baeldung.spring.data.persistence.customrepository.model.User;
public interface CustomUserRepository {
User customFindMethod(Long id);
}

View File

@ -0,0 +1,26 @@
package com.baeldung.spring.data.persistence.customrepository.repository;
import com.baeldung.spring.data.persistence.customrepository.model.User;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Objects;
public class CustomUserRepositoryImpl implements CustomUserRepository {
@PersistenceContext
private EntityManager entityManager;
@Override
public User customFindMethod(Long id) {
return (User) entityManager.createQuery("FROM User u WHERE u.id = :id")
.setParameter("id", id)
.getSingleResult();
}
@PostConstruct
public void postConstruct() {
Objects.requireNonNull(entityManager);
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.spring.data.persistence.customrepository.repository;
import com.baeldung.spring.data.persistence.customrepository.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long>, CustomUserRepository {
}

View File

@ -0,0 +1,13 @@
package com.baeldung.spring.data.persistence.like;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LikeApplication {
public static void main(String[] args) {
SpringApplication.run(LikeApplication.class, args);
}
}

View File

@ -0,0 +1,58 @@
package com.baeldung.spring.data.persistence.like.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String title;
private String director;
private String rating;
private int duration;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.spring.data.persistence.like.repository;
import com.baeldung.spring.data.persistence.like.model.Movie;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface MovieRepository extends CrudRepository<Movie, Long> {
List<Movie> findByTitleContaining(String title);
List<Movie> findByTitleLike(String title);
List<Movie> findByTitleContains(String title);
List<Movie> findByTitleIsContaining(String title);
List<Movie> findByRatingStartsWith(String rating);
List<Movie> findByDirectorEndsWith(String director);
List<Movie> findByTitleContainingIgnoreCase(String title);
List<Movie> findByRatingNotContaining(String rating);
List<Movie> findByDirectorNotLike(String director);
@Query("SELECT m FROM Movie m WHERE m.title LIKE %:title%")
List<Movie> searchByTitleLike(@Param("title") String title);
@Query("SELECT m FROM Movie m WHERE m.rating LIKE ?1%")
List<Movie> searchByRatingStartsWith(String rating);
//Escaping works in SpringBoot >= 2.4.1
//@Query("SELECT m FROM Movie m WHERE m.director LIKE %?#{escape([0])} escape ?#{escapeCharacter()}")
@Query("SELECT m FROM Movie m WHERE m.director LIKE %:#{[0]}")
List<Movie> searchByDirectorEndsWith(String director);
}

View File

@ -0,0 +1,72 @@
package com.baeldung.spring.data.persistence.repository;
import javax.persistence.*;
import java.io.Serializable;
@Entity
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable = false)
private String name;
public Foo() {
super();
}
public Foo(final String name) {
super();
this.name = name;
}
public long getId() {
return id;
}
public void setId(final long 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 + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Foo other = (Foo) obj;
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("Foo [name=").append(name).append("]");
return builder.toString();
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.spring.data.persistence.repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FooService implements IFooService {
@Autowired
private IFooDAO dao;
@Override
public Foo create(Foo foo) {
return dao.save(foo);
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.spring.data.persistence.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface IFooDAO extends JpaRepository<Foo, Long> {
Foo findByName(String name);
@Query("SELECT f FROM Foo f WHERE LOWER(f.name) = LOWER(:name)")
Foo retrieveByName(@Param("name") String name);
}

View File

@ -0,0 +1,5 @@
package com.baeldung.spring.data.persistence.repository;
public interface IFooService {
Foo create(Foo foo);
}

View File

@ -0,0 +1,81 @@
package com.baeldung.spring.data.persistence.repository;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
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 org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@PropertySource("classpath:persistence.properties")
@ComponentScan("com.baeldung.spring.data.persistence.repository")
//@ImportResource("classpath*:*springDataConfig.xml")
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.baeldung.spring.data.persistence.repository")
public class PersistenceConfig {
@Autowired
private Environment env;
public PersistenceConfig() {
super();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan("com.baeldung.spring.data.persistence.repository");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties additionalProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
return hibernateProperties;
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.spring.data.persistence.saveperformance;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String title;
private String author;
public Book(final String title, final String author) {
this.title = title;
this.author = author;
}
public Book() {
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.spring.data.persistence.saveperformance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
public class BookApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(BookApplication.class);
@Autowired
private BookRepository bookRepository;
public static void main(String[] args) {
SpringApplication.run(BookApplication.class, args);
}
@EventListener(ApplicationReadyEvent.class)
public void executePerformanceBenchmark() {
int bookCount = 10000;
long start = System.currentTimeMillis();
for (int i = 0; i < bookCount; i++) {
bookRepository.save(new Book("Book " + i, "Author " + i));
}
long end = System.currentTimeMillis();
bookRepository.deleteAll();
LOGGER.debug("It took {}ms to execute save() for {} books.", (end - start), bookCount);
List<Book> bookList = new ArrayList<>();
for (int i = 0; i < bookCount; i++) {
bookList.add(new Book("Book " + i, "Author " + i));
}
start = System.currentTimeMillis();
bookRepository.saveAll(bookList);
end = System.currentTimeMillis();
LOGGER.debug("It took {}ms to execute saveAll() with {}} books.", (end - start), bookCount);
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.spring.data.persistence.saveperformance;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BookRepository extends JpaRepository<Book, Long> {
}

View File

@ -0,0 +1,3 @@
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa

View File

@ -0,0 +1,9 @@
# jdbc.X
jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
jdbc.user=sa
jdbc.pass=sa
# hibernate.X
hibernate.hbm2ddl.auto=create-drop
hibernate.dialect=org.hibernate.dialect.H2Dialect

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
>
<jpa:repositories base-package="com.baeldung.spring.data.persistence.repository"/>
</beans>

View File

@ -0,0 +1,27 @@
package com.baeldung.spring.data.persistence.customrepository;
import com.baeldung.spring.data.persistence.customrepository.model.User;
import com.baeldung.spring.data.persistence.customrepository.repository.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest(classes = CustomRepositoryApplication.class)
class CustomRepositoryUnitTest {
@Autowired
private UserRepository userRepository;
@Test
void givenCustomRepository_whenInvokeCustomFindMethod_thenEntityIsFound() {
User user = new User();
user.setEmail("foo@gmail.com");
user.setName("userName");
User persistedUser = userRepository.save(user);
assertEquals(persistedUser, userRepository.customFindMethod(user.getId()));
}
}

View File

@ -0,0 +1,91 @@
package com.baeldung.spring.data.persistence.like;
import com.baeldung.spring.data.persistence.like.model.Movie;
import com.baeldung.spring.data.persistence.like.repository.MovieRepository;
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.jdbc.Sql;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
@RunWith(SpringRunner.class)
@Sql(scripts = { "/test-movie-data.sql" })
@SpringBootTest(classes = LikeApplication.class)
@Sql(scripts = "/test-movie-cleanup.sql", executionPhase = AFTER_TEST_METHOD)
public class MovieRepositoryIntegrationTest {
@Autowired
private MovieRepository movieRepository;
@Autowired
private DataSource dataSource;
@Test
public void givenPartialTitle_WhenFindByTitleContaining_ThenMoviesShouldReturn() {
List<Movie> results = movieRepository.findByTitleContaining("in");
assertEquals(3, results.size());
results = movieRepository.findByTitleLike("%in%");
assertEquals(3, results.size());
results = movieRepository.findByTitleIsContaining("in");
assertEquals(3, results.size());
results = movieRepository.findByTitleContains("in");
assertEquals(3, results.size());
}
@Test
public void givenStartOfRating_WhenFindByRatingStartsWith_ThenMoviesShouldReturn() {
List<Movie> results = movieRepository.findByRatingStartsWith("PG");
assertEquals(6, results.size());
}
@Test
public void givenLastName_WhenFindByDirectorEndsWith_ThenMoviesShouldReturn() {
List<Movie> results = movieRepository.findByDirectorEndsWith("Burton");
assertEquals(1, results.size());
}
@Test
public void givenPartialTitle_WhenFindByTitleContainingIgnoreCase_ThenMoviesShouldReturn() {
List<Movie> results = movieRepository.findByTitleContainingIgnoreCase("the");
assertEquals(2, results.size());
}
@Test
public void givenPartialTitle_WhenSearchByTitleLike_ThenMoviesShouldReturn() {
List<Movie> results = movieRepository.searchByTitleLike("in");
assertEquals(3, results.size());
}
@Test
public void givenStartOfRating_SearchFindByRatingStartsWith_ThenMoviesShouldReturn() {
List<Movie> results = movieRepository.searchByRatingStartsWith("PG");
assertEquals(6, results.size());
}
@Test
public void givenLastName_WhenSearchByDirectorEndsWith_ThenMoviesShouldReturn() {
List<Movie> results = movieRepository.searchByDirectorEndsWith("Burton");
assertEquals(1, results.size());
}
@Test
public void givenPartialRating_findByRatingNotContaining_ThenMoviesShouldReturn() {
List<Movie> results = movieRepository.findByRatingNotContaining("PG");
assertEquals(1, results.size());
}
@Test
public void givenPartialDirector_WhenFindByDirectorNotLike_ThenMoviesShouldReturn() {
List<Movie> results = movieRepository.findByDirectorNotLike("An%");
assertEquals(5, results.size());
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.spring.data.persistence.repository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = PersistenceConfig.class)
public class FooServiceIntegrationTest {
@Autowired
private IFooService service;
@Autowired
private DataSource dataSource;
@Test(expected = DataIntegrityViolationException.class)
public final void whenInvalidEntityIsCreated_thenDataException() {
service.create(new Foo());
}
}

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="15 seconds" debug="false">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%d{ISO8601}]-[%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1 @@
DELETE FROM Movie;

View File

@ -0,0 +1,7 @@
INSERT INTO movie(id, title, director, rating, duration) VALUES(1, 'Godzilla: King of the Monsters', ' Michael Dougherty', 'PG-13', 132);
INSERT INTO movie(id, title, director, rating, duration) VALUES(2, 'Avengers: Endgame', 'Anthony Russo', 'PG-13', 181);
INSERT INTO movie(id, title, director, rating, duration) VALUES(3, 'Captain Marvel', 'Anna Boden', 'PG-13', 123);
INSERT INTO movie(id, title, director, rating, duration) VALUES(4, 'Dumbo', 'Tim Burton', 'PG', 112);
INSERT INTO movie(id, title, director, rating, duration) VALUES(5, 'Booksmart', 'Olivia Wilde', 'R', 102);
INSERT INTO movie(id, title, director, rating, duration) VALUES(6, 'Aladdin', 'Guy Ritchie', 'PG', 128);
INSERT INTO movie(id, title, director, rating, duration) VALUES(7, 'The Sun Is Also a Star', 'Ry Russo-Young', 'PG-13', 100);

View File

@ -43,6 +43,7 @@
<!-- <module>jackson-modules</module>--> <!-- <module>jackson-modules</module>-->
<!-- <module>jackson-simple</module>--> <!-- <module>jackson-simple</module>-->
<!-- <module>parent-java</module>--> <!-- <module>parent-java</module>-->
<module>persistence-modules</module>
<module>testing-modules</module> <module>testing-modules</module>
<module>toolkits</module> <module>toolkits</module>
<!-- <module>xml</module>--> <!-- <module>xml</module>-->
@ -55,9 +56,9 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Logging --> <!-- Logging -->
<slf4j.version>1.7.30</slf4j.version> <version.slf4j>1.7.36</version.slf4j>
<log4j.version>1.2.17</log4j.version> <version.log4j>1.2.17</version.log4j>
<log4j2.version>2.2</log4j2.version> <version.log4j2>2.18.0</version.log4j2>
<!-- APACHE COMMONS--> <!-- APACHE COMMONS-->
<commons-io.version>2.8.0</commons-io.version> <commons-io.version>2.8.0</commons-io.version>
@ -71,7 +72,7 @@
<maven-war-plugin.version>3.0.0</maven-war-plugin.version> <maven-war-plugin.version>3.0.0</maven-war-plugin.version>
<!-- UTILS --> <!-- UTILS -->
<guava.version>31.1-jre</guava.version> <version.guava>31.1-jre</version.guava>
<!-- Hibernate / JPA --> <!-- Hibernate / JPA -->
<hibernate.version>4.3.5.Final</hibernate.version> <hibernate.version>4.3.5.Final</hibernate.version>