Merge pull request #9956 from dupirefr/team/JAVA-2306-split-spring_persistence_modules

Team/java 2306 split spring persistence modules
This commit is contained in:
Loredana Crusoveanu
2020-09-03 11:45:24 +03:00
committed by GitHub
120 changed files with 649 additions and 3003 deletions
+4 -1
View File
@@ -87,4 +87,7 @@ transaction.log
apache-cxf/cxf-aegis/baeldung.xml
testing-modules/report-*.json
libraries-2/*.db
libraries-2/*.db
# SDKMan
.sdkmanrc
@@ -1,4 +1,4 @@
package com.baeldung.jdbc;
package com.baeldung.spring.jdbc;
import java.sql.*;
import java.util.UUID;
@@ -1,4 +1,4 @@
package com.baeldung.jdbc;
package com.baeldung.spring.jdbc;
import java.util.Objects;
@@ -1,4 +1,4 @@
package com.baeldung.jdbc.joins;
package com.baeldung.spring.jdbc.joins;
class ArticleWithAuthor {
@@ -1,4 +1,4 @@
package com.baeldung.jdbc.joins;
package com.baeldung.spring.jdbc.joins;
import java.sql.Connection;
import java.sql.ResultSet;
@@ -1,4 +1,4 @@
package com.baeldung.jdbc;
package com.baeldung.spring.jdbc;
import org.apache.log4j.Logger;
import org.junit.After;
@@ -1,4 +1,4 @@
package com.baeldung.jdbc;
package com.baeldung.spring.jdbc;
import static org.junit.Assert.assertEquals;
@@ -1,4 +1,4 @@
package com.baeldung.jdbc.joins;
package com.baeldung.spring.jdbc.joins;
import org.junit.After;
import org.junit.Before;
+3 -1
View File
@@ -68,7 +68,9 @@
<module>spring-data-jpa-enterprise</module>
<module>spring-data-jpa-filtering</module>
<module>spring-data-jpa-query</module>
<module>spring-data-jpa-query-2</module>
<module>spring-data-jpa-repo</module>
<module>spring-data-jpa-repo-2</module>
<module>spring-data-jdbc</module>
@@ -82,9 +84,9 @@
<module>spring-hibernate4</module>
<module>spring-jpa</module>
<module>spring-jpa-2</module>
<module>spring-jdbc</module>
<!-- <module>spring-mybatis</module> --> <!-- needs fixing in BAEL-9021 -->
<module>spring-persistence-simple</module>
<module>spring-persistence-simple-2</module>
</modules>
<properties>
@@ -4,6 +4,7 @@ This module contains articles about querying data using Spring Data JPA
### Relevant Articles:
- [Use Criteria Queries in a Spring Data Application](https://www.baeldung.com/spring-data-criteria-queries)
- [Query Entities by Dates and Times with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-query-by-date)
- More articles: [[<-- prev]](../spring-data-jpa-query)
### Eclipse Config
@@ -3,7 +3,7 @@
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-query</artifactId>
<artifactId>spring-data-jpa-query-2</artifactId>
<name>spring-data-jpa-query-2</name>
<parent>
@@ -1,4 +1,4 @@
package com.baeldung.spring.data.persistence.model;
package com.baeldung.spring.data.jpa.query;
import javax.persistence.*;
import java.time.LocalDate;
@@ -8,7 +8,6 @@ import java.util.Objects;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@@ -28,9 +27,6 @@ public class User {
private Integer status;
@OneToMany
List<Possession> possessionList;
public User() {
super();
}
@@ -87,12 +83,20 @@ public class User {
return creationDate;
}
public List<Possession> getPossessionList() {
return possessionList;
public LocalDate getLastLoginDate() {
return lastLoginDate;
}
public void setPossessionList(List<Possession> possessionList) {
this.possessionList = possessionList;
public void setLastLoginDate(LocalDate lastLoginDate) {
this.lastLoginDate = lastLoginDate;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
@Override
@@ -119,21 +123,4 @@ public class User {
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;
}
}
@@ -1,6 +1,5 @@
package com.baeldung.spring.data.persistence.repository;
package com.baeldung.spring.data.jpa.query;
import com.baeldung.spring.data.persistence.model.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
@@ -9,51 +8,16 @@ import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;
public interface UserRepository extends JpaRepository<User, Integer>, UserRepositoryCustom {
Stream<User> findAllByName(String name);
@Query("SELECT u FROM User u WHERE u.status = 1")
Collection<User> findAllActiveUsers();
@Query("select u from User u where u.email like '%@gmail.com'")
List<User> findUsersWithGmailAddress();
@Query(value = "SELECT * FROM Users u WHERE u.status = 1", nativeQuery = true)
Collection<User> findAllActiveUsersNative();
@Query("SELECT u FROM User u WHERE u.status = ?1")
User findUserByStatus(Integer status);
@Query(value = "SELECT * FROM Users u WHERE u.status = ?1", nativeQuery = true)
User findUserByStatusNative(Integer status);
@Query("SELECT u FROM User u WHERE u.status = ?1 and u.name = ?2")
User findUserByStatusAndName(Integer status, String name);
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :name")
User findUserByStatusAndNameNamedParams(@Param("status") Integer status, @Param("name") String name);
@Query(value = "SELECT * FROM Users u WHERE u.status = :status AND u.name = :name", nativeQuery = true)
User findUserByStatusAndNameNamedParamsNative(@Param("status") Integer status, @Param("name") String name);
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :name")
User findUserByUserStatusAndUserName(@Param("status") Integer userStatus, @Param("name") String userName);
@Query("SELECT u FROM User u WHERE u.name like ?1%")
User findUserByNameLike(String name);
@Query("SELECT u FROM User u WHERE u.name like :name%")
User findUserByNameLikeNamedParam(@Param("name") String name);
@Query(value = "SELECT * FROM users u WHERE u.name LIKE ?1%", nativeQuery = true)
User findUserByNameLikeNative(String name);
@Query(value = "SELECT u FROM User u")
List<User> findAllUsers(Sort sort);
@@ -63,6 +27,27 @@ public interface UserRepository extends JpaRepository<User, Integer>, UserReposi
@Query(value = "SELECT * FROM Users ORDER BY id", countQuery = "SELECT count(*) FROM Users", nativeQuery = true)
Page<User> findAllUsersWithPaginationNative(Pageable pageable);
@Query("SELECT u FROM User u WHERE u.status = ?1")
User findUserByStatus(Integer status);
@Query("SELECT u FROM User u WHERE u.status = ?1 and u.name = ?2")
User findUserByStatusAndName(Integer status, String name);
@Query(value = "SELECT * FROM Users u WHERE u.status = ?1", nativeQuery = true)
User findUserByStatusNative(Integer status);
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :name")
User findUserByStatusAndNameNamedParams(@Param("status") Integer status, @Param("name") String name);
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :name")
User findUserByUserStatusAndUserName(@Param("status") Integer userStatus, @Param("name") String userName);
@Query(value = "SELECT * FROM Users u WHERE u.status = :status AND u.name = :name", nativeQuery = true)
User findUserByStatusAndNameNamedParamsNative(@Param("status") Integer status, @Param("name") String name);
@Query(value = "SELECT u FROM User u WHERE u.name IN :names")
List<User> findUserByNameList(@Param("names") Collection<String> names);
@Modifying
@Query("update User u set u.status = :status where u.name = :name")
int updateUserSetStatusForName(@Param("status") Integer status, @Param("name") String name);
@@ -74,25 +59,4 @@ public interface UserRepository extends JpaRepository<User, Integer>, UserReposi
@Query(value = "INSERT INTO Users (name, age, email, status, active) VALUES (:name, :age, :email, :status, :active)", nativeQuery = true)
@Modifying
void insertUser(@Param("name") String name, @Param("age") Integer age, @Param("email") String email, @Param("status") Integer status, @Param("active") boolean active);
@Modifying
@Query(value = "UPDATE Users u SET status = ? WHERE u.name = ?", nativeQuery = true)
int updateUserSetStatusForNameNativePostgres(Integer status, String name);
@Query(value = "SELECT u FROM User u WHERE u.name IN :names")
List<User> findUserByNameList(@Param("names") Collection<String> names);
void deleteAllByCreationDateAfter(LocalDate date);
@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(1) not null default 0", nativeQuery = true)
void addDeletedColumn();
}
@@ -1,6 +1,4 @@
package com.baeldung.spring.data.persistence.repository;
import com.baeldung.spring.data.persistence.model.User;
package com.baeldung.spring.data.jpa.query;
import java.util.Collection;
import java.util.List;
@@ -3,7 +3,6 @@
This module contains articles about querying data using Spring Data JPA
### Relevant Articles:
- [Query Entities by Dates and Times with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-query-by-date)
- [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)
@@ -0,0 +1,5 @@
## Spring Data JPA - Repositories
### Relevant Articles:
- [Introduction to Spring Data JPA](https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa)
- More articles: [[<-- prev]](/spring-data-jpa-repo/)
@@ -0,0 +1,47 @@
<?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>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<artifactId>spring-data-jpa-repo-2</artifactId>
<name>spring-data-jpa-repo-2</name>
<dependencies>
<!-- Persistence -->
<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>
<!-- Utilities -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
<properties>
<guava.version>29.0-jre</guava.version>
</properties>
</project>
@@ -1,16 +1,10 @@
package com.baeldung.spring.data.persistence.model;
package com.baeldung.spring.data.persistence.repository;
import javax.persistence.*;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@@ -28,8 +22,6 @@ public class Foo implements Serializable {
this.name = name;
}
// API
public long getId() {
return id;
}
@@ -46,8 +38,6 @@ public class Foo implements Serializable {
this.name = name;
}
//
@Override
public int hashCode() {
final int prime = 31;
@@ -79,5 +69,4 @@ public class Foo implements Serializable {
builder.append("Foo [name=").append(name).append("]");
return builder.toString();
}
}
}
@@ -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);
}
}
@@ -1,13 +1,13 @@
package com.baeldung.spring.data.persistence.repository;
import com.baeldung.spring.data.persistence.model.Foo;
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> {
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);
}
@@ -0,0 +1,5 @@
package com.baeldung.spring.data.persistence.repository;
public interface IFooService {
Foo create(Foo foo);
}
@@ -1,4 +1,4 @@
package com.baeldung.spring.data.persistence.config;
package com.baeldung.spring.data.persistence.repository;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
@@ -20,11 +20,11 @@ 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
@PropertySource({ "classpath:persistence-${envTarget:h2}.properties" })
@ComponentScan({ "com.baeldung.spring.data.persistence" })
//@ImportResource("classpath*:*springDataJpaRepositoriesConfig.xml")
@EnableJpaRepositories("com.baeldung.spring.data.persistence.repository")
@EnableJpaRepositories(basePackages = "com.baeldung.spring.data.persistence.repository")
public class PersistenceConfig {
@Autowired
@@ -38,7 +38,7 @@ public class PersistenceConfig {
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan("com.baeldung.spring.data.persistence.model");
em.setPackagesToScan("com.baeldung.spring.data.persistence.repository");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
@@ -78,5 +78,4 @@ public class PersistenceConfig {
return hibernateProperties;
}
}
}
@@ -0,0 +1,3 @@
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
@@ -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=
# hibernate.X
hibernate.hbm2ddl.auto=create-drop
hibernate.dialect=org.hibernate.dialect.H2Dialect
@@ -2,12 +2,10 @@
<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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
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>
@@ -0,0 +1,21 @@
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;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = PersistenceConfig.class)
public class FooServiceIntegrationTest {
@Autowired
private IFooService service;
@Test(expected = DataIntegrityViolationException.class)
public final void whenInvalidEntityIsCreated_thenDataException() {
service.create(new Foo());
}
}
@@ -11,6 +11,7 @@ This module contains articles about repositories in Spring Data JPA
- [Spring Data Composable Repositories](https://www.baeldung.com/spring-data-composable-repositories)
- [Spring Data JPA Repository Populators](https://www.baeldung.com/spring-data-jpa-repository-populators)
- [Calling Stored Procedures from Spring Data JPA Repositories](https://www.baeldung.com/spring-data-jpa-stored-procedures)
- More articles: [[--> next]](/spring-data-jpa-repo-2/)
### Eclipse Config
After importing the project into Eclipse, you may see the following error:
@@ -0,0 +1,6 @@
## Spring JDBC
### Relevant Articles:
- [Spring JDBC Template](https://www.baeldung.com/spring-jdbc-jdbctemplate)
- [Spring JDBC Template Testing](https://www.baeldung.com/spring-jdbctemplate-testing)
- [Spring JDBC Template In Clause](https://www.baeldung.com/spring-jdbctemplate-in-list)

Some files were not shown because too many files have changed in this diff Show More