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

[JAVA-2306] Fixes after Loredana's review
This commit is contained in:
Loredana Crusoveanu 2020-09-06 11:58:01 +03:00 committed by GitHub
commit 3ddc6fd882
13 changed files with 105 additions and 34 deletions

View File

@ -3,6 +3,7 @@
This module contains articles about querying data using Spring Data JPA
### Relevant Articles:
- [Spring Data JPA @Query Annotation](https://www.baeldung.com/spring-data-jpa-query)
- [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)

View File

@ -0,0 +1,48 @@
package com.baeldung.spring.data.jpa.query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class UserRepositoryCustomImpl implements UserRepositoryCustom {
@PersistenceContext
private EntityManager entityManager;
@Override
public List<User> findUserByEmails(Set<String> emails) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<User> query = cb.createQuery(User.class);
Root<User> user = query.from(User.class);
Path<String> emailPath = user.get("email");
List<Predicate> predicates = new ArrayList<>();
for (String email : emails) {
predicates.add(cb.like(emailPath, email));
}
query.select(user)
.where(cb.or(predicates.toArray(new Predicate[predicates.size()])));
return entityManager.createQuery(query)
.getResultList();
}
@Override
public List<User> findAllUsersByPredicates(Collection<java.util.function.Predicate<User>> predicates) {
List<User> allUsers = entityManager.createQuery("select u from User u", User.class).getResultList();
Stream<User> allUsersStream = allUsers.stream();
for (java.util.function.Predicate<User> predicate : predicates) {
allUsersStream = allUsersStream.filter(predicate);
}
return allUsersStream.collect(Collectors.toList());
}
}

View File

@ -1,12 +1,13 @@
package com.baeldung.spring.transactional;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
class TransactionalCompareApplication {
public static void main(String[] args) {
SpringApplication.run(TransactionalCompareApplication.class, args);
}
}
package com.baeldung.spring.data.jpa.query.datetime;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.boot.domain;
package com.baeldung.spring.data.jpa.query.datetime;
import javax.persistence.*;
import java.util.Date;

View File

@ -1,11 +1,9 @@
package com.baeldung.boot.daos;
package com.baeldung.spring.data.jpa.query.datetime;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import com.baeldung.boot.domain.Article;
import java.util.Date;
import java.util.List;

View File

@ -1,11 +1,4 @@
package com.baeldung.boot.daos;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
package com.baeldung.spring.data.jpa.query.datetime;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -13,7 +6,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.boot.domain.Article;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
@DataJpaTest(properties="spring.datasource.data=classpath:import_entities.sql")

View File

@ -5,6 +5,7 @@
### Relevant Articles:
- [Transaction Propagation and Isolation in Spring @Transactional](https://www.baeldung.com/spring-transactional-propagation-isolation)
- [JTA Transaction with Spring](https://www.baeldung.com/spring-vs-jta-transactional)
- [Mock JNDI Datasource](https://www.baeldung.com/spring-mock-jndi-datasource)
### Eclipse Config

View File

@ -26,6 +26,31 @@
</dependency>
<!-- persistence -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>${persistence-api.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring-data-jpa.version}</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
<version>${transaction-api.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
@ -58,7 +83,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>5.2.4.RELEASE</org.springframework.version>
<spring-boot-starter.version>2.3.3.RELEASE</spring-boot-starter.version>
<!-- persistence -->
<persistence-api.version>2.2</persistence-api.version>
<transaction-api.version>1.3</transaction-api.version>
<spring-data-jpa.version>2.2.7.RELEASE</spring-data-jpa.version>
<h2.version>1.4.200</h2.version>
<!-- simple-jndi -->
<simple-jndi.version>0.23.0</simple-jndi.version>

View File

@ -1,4 +1,4 @@
package com.baeldung.spring.transactional.entity;
package com.baeldung.spring.transactional;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;

View File

@ -1,6 +1,5 @@
package com.baeldung.spring.transactional.repository;
package com.baeldung.spring.transactional;
import com.baeldung.spring.transactional.entity.Car;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CarRepository extends JpaRepository<Car, Long> {

View File

@ -1,7 +1,5 @@
package com.baeldung.spring.transactional.service;
package com.baeldung.spring.transactional;
import com.baeldung.spring.transactional.entity.Car;
import com.baeldung.spring.transactional.repository.CarRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;

View File

@ -1,7 +1,5 @@
package com.baeldung.spring.transactional.service;
package com.baeldung.spring.transactional;
import com.baeldung.spring.transactional.entity.Car;
import com.baeldung.spring.transactional.repository.CarRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;