Update code base for BAEL-3872 (#9032)

* resolve an sql syntax error for H2 version 1.4.200

* update spring and spring-data-jpa version to latest
migrate spring-data-jpa Sort initializations to Sort.by static factory methods to fix compile errors

* update hibernate version to latest

* cleanup code

* show how to get a bean object from factory bean as shown in the section 4

* update mysql version and fix tests under persistence.service

* clean up code
This commit is contained in:
Yavuz Tas 2020-04-19 16:18:58 +02:00 committed by GitHub
parent 8da4206dc4
commit 87ac7a74be
8 changed files with 46 additions and 58 deletions

View File

@ -24,7 +24,11 @@
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- persistence -->
<dependency>
<groupId>org.hibernate</groupId>
@ -128,12 +132,13 @@
<properties>
<!-- Spring -->
<org.springframework.version>5.1.6.RELEASE</org.springframework.version>
<org.springframework.version>5.2.5.RELEASE</org.springframework.version>
<!-- persistence -->
<hibernate.version>5.4.2.Final</hibernate.version>
<mysql-connector-java.version>6.0.6</mysql-connector-java.version>
<spring-data-jpa.version>2.1.6.RELEASE</spring-data-jpa.version>
<hibernate.version>5.4.13.Final</hibernate.version>
<mysql-connector-java.version>8.0.19</mysql-connector-java.version>
<h2.version>1.4.200</h2.version>
<spring-data-jpa.version>2.2.6.RELEASE</spring-data-jpa.version>
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
<jta.version>1.1</jta.version>
<querydsl.version>4.2.1</querydsl.version>

View File

@ -1,9 +1,7 @@
package com.baeldung.config;
import java.util.Properties;
import javax.sql.DataSource;
import com.baeldung.persistence.service.FooService;
import com.google.common.base.Preconditions;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
@ -23,16 +21,15 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.baeldung.hibernate.dao.FooDao;
import com.baeldung.jpa.dao.IFooDao;
import com.google.common.base.Preconditions;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "com.baeldung.hibernate.dao" }, transactionManagerRef = "jpaTransactionManager")
@EnableJpaAuditing
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "com.baeldung.persistence", "com.baeldung.hibernate.dao" })
@ComponentScan(basePackages = { "com.baeldung.persistence.dao", "com.baeldung.jpa.dao" })
public class PersistenceConfig {
@Autowired
@ -46,7 +43,7 @@ public class PersistenceConfig {
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
sessionFactory.setPackagesToScan("com.baeldung.persistence.model");
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
@ -56,7 +53,7 @@ public class PersistenceConfig {
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(restDataSource());
emf.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
emf.setPackagesToScan("com.baeldung.persistence.model");
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
emf.setJpaVendorAdapter(vendorAdapter);
@ -96,18 +93,15 @@ public class PersistenceConfig {
}
@Bean
public IFooDao fooHibernateDao() {
return new FooDao();
public FooService fooService() {
return new FooService();
}
private final Properties hibernateProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
hibernateProperties.setProperty("hibernate.show_sql", "true");
// hibernateProperties.setProperty("hibernate.format_sql", "true");
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
// Envers properties
hibernateProperties.setProperty("org.hibernate.envers.audit_table_suffix", env.getProperty("envers.audit_table_suffix"));

View File

@ -1,10 +1,6 @@
package com.baeldung.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
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;
@ -21,7 +17,8 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableTransactionManagement
@ -43,7 +40,7 @@ public class PersistenceJPAConfig {
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
em.setPackagesToScan("com.baeldung.persistence.model");
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
@ -64,9 +61,9 @@ public class PersistenceJPAConfig {
}
@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
public PlatformTransactionManager transactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}

View File

@ -1,16 +1,15 @@
package com.baeldung.jpa.dao;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.Serializable;
import java.util.List;
public abstract class AbstractJpaDAO<T extends Serializable> {
private Class<T> clazz;
@PersistenceContext
@PersistenceContext(unitName = "entityManagerFactory")
private EntityManager entityManager;
public final void setClazz(final Class<T> clazzToSet) {

View File

@ -21,7 +21,7 @@
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.eventGeneratedId}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pass}"/>
</bean>

View File

@ -3,5 +3,5 @@ CREATE TABLE EMPLOYEE
ID int NOT NULL PRIMARY KEY,
FIRST_NAME varchar(255),
LAST_NAME varchar(255),
ADDRESS varchar(255),
ADDRESS varchar(255)
);

View File

@ -1,7 +1,7 @@
# jdbc.X
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate5_01?createDatabaseIfNotExist=true
jdbc.eventGeneratedId=tutorialuser
jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate5_01?createDatabaseIfNotExist=true&serverTimezone=UTC
jdbc.user=tutorialuser
jdbc.pass=tutorialmy5ql
# hibernate.X

View File

@ -1,25 +1,6 @@
package com.baeldung.spring.data.persistence.jpaquery;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import com.baeldung.spring.data.persistence.config.PersistenceConfig;
import com.baeldung.spring.data.persistence.jpaquery.UserRepository;
import com.baeldung.spring.data.persistence.model.User;
import org.junit.After;
import org.junit.Test;
@ -36,6 +17,16 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
@ -283,7 +274,7 @@ public class UserRepositoryCommon {
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
List<User> usersSortByName = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
List<User> usersSortByName = userRepository.findAll(Sort.by(Sort.Direction.ASC, "name"));
assertThat(usersSortByName.get(0)
.getName()).isEqualTo(USER_NAME_ADAM);
@ -295,7 +286,7 @@ public class UserRepositoryCommon {
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
userRepository.findAll(Sort.by(Sort.Direction.ASC, "name"));
List<User> usersSortByNameLength = userRepository.findAll(Sort.by("LENGTH(name)"));
@ -459,6 +450,8 @@ public class UserRepositoryCommon {
userRepository.save(usr01);
userRepository.save(usr02);
System.out.println(TimeZone.getDefault());
List<User> users = userRepository.findUsersWithGmailAddress();
assertEquals(1, users.size());
assertEquals(usr02, users.get(0));