Merge branch 'master' into JAVA-7244-Review_log_statements_for_projects

This commit is contained in:
Loredana Crusoveanu
2021-12-09 10:22:41 +02:00
committed by GitHub
1079 changed files with 12491 additions and 4924 deletions
@@ -0,0 +1,3 @@
### Relevant Articles:
- [Getting Started With Apache Derby](https://www.baeldung.com/java-apache-derby)
+5 -8
View File
@@ -1,15 +1,15 @@
<?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">
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>apache-derby</artifactId>
<parent>
<artifactId>persistence-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apache-derby</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.derby/derby -->
@@ -18,15 +18,12 @@
<artifactId>derby</artifactId>
<version>10.13.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.derby/derbyclient -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.13.1.1</version>
</dependency>
</dependencies>
</project>
@@ -44,7 +44,6 @@
</dependencies>
<properties>
<h2.version>1.4.200</h2.version>
<mssql.driver.version>8.4.1.jre11</mssql.driver.version>
<oracle.driver.version>10.2.0.4.0</oracle.driver.version>
<mysql.driver.version>8.0.22</mysql.driver.version>
@@ -22,12 +22,6 @@
<version>${postgresql.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
@@ -61,8 +55,6 @@
</dependencies>
<properties>
<h2.version>1.4.200</h2.version>
<assertj-core.version>3.10.0</assertj-core.version>
<commons-dbcp2.version>2.4.0</commons-dbcp2.version>
<HikariCP.version>3.2.0</HikariCP.version>
<c3p0.version>0.9.5.2</c3p0.version>
+2 -2
View File
@@ -18,8 +18,8 @@
<dependencyManagement>
<dependencies>
<!-- for junit5 to successfully be able to discover junit4 tests, we need 4.12+ version of junit:junit in the classpath. hence forcing the
latest 4.13.2 available version for junit5 compatibility -->
<!-- for junit5 to successfully be able to discover junit4 tests, we need 4.12+ version of
junit:junit in the classpath. hence forcing the latest 4.13.2 available version for junit5 compatibility -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -45,7 +45,6 @@
<properties>
<hibernate-core.version>5.4.7.Final</hibernate-core.version>
<h2.version>1.4.200</h2.version>
<maven.deploy.skip>true</maven.deploy.skip>
<spring-boot.version>2.1.7.RELEASE</spring-boot.version>
<hibernate.core.version>5.4.7.Final</hibernate.core.version>
@@ -19,12 +19,6 @@
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
@@ -87,7 +81,6 @@
<hibernate.version>5.3.7.Final</hibernate.version>
<mysql.version>6.0.6</mysql.version>
<mariaDB4j.version>2.2.3</mariaDB4j.version>
<assertj-core.version>3.8.0</assertj-core.version>
<geodb.version>0.9</geodb.version>
<hsqldb.version>2.3.4</hsqldb.version>
</properties>
@@ -24,7 +24,7 @@ public class HibernateUtil {
settings.put(Environment.USER, "sa");
settings.put(Environment.PASS, "");
settings.put(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect");
settings.put(Environment.SHOW_SQL, "true");
settings.put(Environment.SHOW_SQL, "false");
settings.put(Environment.HBM2DDL_AUTO, "update");
configuration.setProperties(settings);
configuration.addAnnotatedClass(User.class);
@@ -27,7 +27,7 @@ public class HibernateUtil {
settings.put(Environment.USER, "sa");
settings.put(Environment.PASS, "");
settings.put(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect");
settings.put(Environment.SHOW_SQL, "true");
settings.put(Environment.SHOW_SQL, "false");
settings.put(Environment.USE_SQL_COMMENTS, "true");
settings.put(Environment.HBM2DDL_AUTO, "update");
configuration.setProperties(settings);
@@ -45,12 +45,6 @@
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
@@ -92,7 +86,6 @@
<hibernate.version>5.3.7.Final</hibernate.version>
<mysql.version>8.0.13</mysql.version>
<mariaDB4j.version>2.2.3</mariaDB4j.version>
<assertj-core.version>3.8.0</assertj-core.version>
<spring-boot.version>2.1.7.RELEASE</spring-boot.version>
</properties>
@@ -1,5 +1,5 @@
hibernate.connection.driver_class=org.h2.Driver
hibernate.connection.url=jdbc:h2:mem:mydb3;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=100;MVCC=FALSE
hibernate.connection.url=jdbc:h2:mem:mydb3;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=100
hibernate.connection.username=sa
hibernate.connection.autocommit=true
hibernate.dialect=org.hibernate.dialect.H2Dialect
@@ -62,12 +62,6 @@
<version>${hibernate.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
@@ -166,7 +160,6 @@
</profiles>
<properties>
<assertj-core.version>3.15.0</assertj-core.version>
<datasource-proxy.version>1.6</datasource-proxy.version>
<guava.version>29.0-jre</guava.version>
<hibernate-types.version>2.9.7</hibernate-types.version>
@@ -29,12 +29,6 @@
<artifactId>hibernate-types-52</artifactId>
<version>${hibernate-types.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
@@ -75,9 +69,9 @@
</dependencies>
<properties>
<h2.version>1.4.197</h2.version> <!-- needs to be specified as it fails with parent's 1.4.200 -->
<hibernate.version>5.4.12.Final</hibernate.version>
<hibernate-types.version>2.10.4</hibernate-types.version>
<assertj-core.version>3.8.0</assertj-core.version>
<hibernate-validator.version>6.0.16.Final</hibernate-validator.version>
<org.glassfish.javax.el.version>3.0.1-b11</org.glassfish.javax.el.version>
<money-api.version>1.0.3</money-api.version>
@@ -19,12 +19,6 @@
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
@@ -60,7 +54,6 @@
<properties>
<mysql.version>6.0.6</mysql.version>
<mariaDB4j.version>2.2.3</mariaDB4j.version>
<assertj-core.version>3.8.0</assertj-core.version>
</properties>
</project>
-7
View File
@@ -19,12 +19,6 @@
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
@@ -66,7 +60,6 @@
<hibernate.version>5.4.12.Final</hibernate.version>
<mysql.version>6.0.6</mysql.version>
<mariaDB4j.version>2.2.3</mariaDB4j.version>
<assertj-core.version>3.8.0</assertj-core.version>
</properties>
</project>
@@ -16,7 +16,7 @@ public class CustomInterceptor extends EmptyInterceptor {
@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
if (entity instanceof User) {
logger.info(((User) entity).toString());
logger.debug(entity.toString());
}
return super.onSave(entity, id, state, propertyNames, types);
}
@@ -25,7 +25,7 @@ public class CustomInterceptor extends EmptyInterceptor {
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object [] previousState, String[] propertyNames, Type[] types) {
if (entity instanceof User) {
((User) entity).setLastModified(new Date());
logger.info(((User) entity).toString());
logger.debug(entity.toString());
}
return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types);
}
@@ -1,7 +1,5 @@
package com.baeldung.hibernate.interceptors.entity;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@@ -9,6 +7,7 @@ import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;
@Entity(name = "hbi_user")
public class User {
@@ -42,9 +42,9 @@ public class HibernateHiloUnitTest {
private void configureLogger() {
BasicConfigurator.configure();
LogManager.getLogger("org.hibernate").setLevel(Level.ERROR);
LogManager.getLogger("org.hibernate.id.enhanced.SequenceStructure").setLevel(Level.DEBUG);
LogManager.getLogger("org.hibernate.event.internal.AbstractSaveEventListener").setLevel(Level.DEBUG);
LogManager.getLogger("org.hibernate.SQL").setLevel(Level.DEBUG);
LogManager.getLogger("org.hibernate.id.enhanced.SequenceStructure").setLevel(Level.INFO);
LogManager.getLogger("org.hibernate.event.internal.AbstractSaveEventListener").setLevel(Level.INFO);
LogManager.getLogger("org.hibernate.SQL").setLevel(Level.INFO);
}
@@ -1,5 +1,8 @@
package com.baeldung.hibernate.proxy;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.hibernate.*;
import org.hibernate.proxy.HibernateProxy;
import org.junit.After;
@@ -2,7 +2,7 @@ hibernate.connection.driver_class=org.h2.Driver
hibernate.connection.url=jdbc:h2:mem:hilo_db;DB_CLOSE_DELAY=-1
hibernate.connection.username=sa
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
@@ -5,6 +5,6 @@ hibernate.connection.autocommit=true
jdbc.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop
hibernate.current_session_context_class=org.hibernate.context.internal.ThreadLocalSessionContext
@@ -5,5 +5,5 @@ hibernate.connection.password=
hibernate.connection.autocommit=true
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.show_sql=false
hibernate.hbm2ddl.auto=validate
@@ -3,7 +3,7 @@ hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1
hibernate.connection.username=sa
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop
hibernate.physical_naming_strategy=com.baeldung.hibernate.namingstrategy.CustomPhysicalNamingStrategy
@@ -3,5 +3,5 @@ hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1
hibernate.connection.username=sa
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop
@@ -5,7 +5,7 @@ hibernate.connection.autocommit=true
jdbc.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop
hibernate.c3p0.min_size=5
-7
View File
@@ -58,12 +58,6 @@
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -139,7 +133,6 @@
<hibernate.version>5.4.14.Final</hibernate.version>
<eclipselink.version>2.7.4</eclipselink.version>
<javax.persistence-api.version>2.2</javax.persistence-api.version>
<assertj.version>3.11.1</assertj.version>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-processor-plugin.version>3.3.3</maven-processor-plugin.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
-1
View File
@@ -13,5 +13,4 @@ This module contains articles about the Java Persistence API (JPA) in Java.
- [Returning an Auto-Generated Id with JPA](https://www.baeldung.com/jpa-get-auto-generated-id)
- [How to Return Multiple Entities In JPA Query](https://www.baeldung.com/jpa-return-multiple-entities)
- [Defining Unique Constraints in JPA](https://www.baeldung.com/jpa-unique-constraints)
- [How to Check Field Existence in MongoDB?](https://www.baeldung.com/mongodb-check-field-exists)
- [Connecting to a Specific Schema in JDBC](https://www.baeldung.com/jdbc-connect-to-schema)
-13
View File
@@ -62,18 +62,6 @@
<version>${postgresql.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
@@ -100,7 +88,6 @@
<eclipselink.version>2.7.4</eclipselink.version>
<mysql.version>8.0.21</mysql.version>
<javax.persistence-api.version>2.2</javax.persistence-api.version>
<assertj.version>3.11.1</assertj.version>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-processor-plugin.version>3.3.3</maven-processor-plugin.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
+1 -1
View File
@@ -10,5 +10,5 @@ This module contains articles about MongoDB in Java.
- [Geospatial Support in MongoDB](https://www.baeldung.com/mongodb-geospatial-support)
- [Introduction to Morphia Java ODM for MongoDB](https://www.baeldung.com/mongodb-morphia)
- [MongoDB Aggregations Using Java](https://www.baeldung.com/java-mongodb-aggregations)
- [MongoDB BSON to JSON](https://www.baeldung.com/bson-to-json)
- [BSON to JSON Document Conversion in Java](https://www.baeldung.com/java-convert-bson-to-json)
- [How to Check Field Existence in MongoDB?](https://www.baeldung.com/mongodb-check-field-exists)
-1
View File
@@ -45,7 +45,6 @@
<properties>
<jooq.version>3.13.4</jooq.version>
<h2.version>1.4.200</h2.version>
</properties>
</project>
@@ -17,12 +17,6 @@
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
@@ -48,7 +42,6 @@
<properties>
<hibernate.version>5.4.3.Final</hibernate.version>
<assertj-core.version>3.12.2</assertj-core.version>
<hibernate-validator.version>6.0.17.Final</hibernate-validator.version>
<javax.el-api.version>3.0.0</javax.el-api.version>
<org.glassfish.javax.el.version>3.0.1-b11</org.glassfish.javax.el.version>
-3
View File
@@ -99,9 +99,6 @@
<hibernate.version>5.2.17.Final</hibernate.version>
<postgresql.version>42.2.20</postgresql.version>
<!-- testing -->
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
</properties>
</project>
-1
View File
@@ -63,7 +63,6 @@
<properties>
<r2dbc-h2.version>0.8.1.RELEASE</r2dbc-h2.version>
<h2.version>1.4.200</h2.version>
</properties>
</project>
@@ -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>
@@ -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>
@@ -1,10 +1,7 @@
package com.baeldung.h2db.lazy_load_no_trans.config;
import net.ttddyy.dsproxy.listener.DataSourceQueryCountListener;
import net.ttddyy.dsproxy.listener.logging.CommonsQueryLoggingListener;
import net.ttddyy.dsproxy.listener.logging.DefaultQueryLogEntryCreator;
import net.ttddyy.dsproxy.listener.logging.SLF4JLogLevel;
import net.ttddyy.dsproxy.listener.logging.SLF4JQueryLoggingListener;
import net.ttddyy.dsproxy.support.ProxyDataSource;
import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder;
import org.aopalliance.intercept.MethodInterceptor;
@@ -49,7 +46,7 @@ public class DatasourceProxyBeanPostProcessor implements BeanPostProcessor {
this.dataSource = ProxyDataSourceBuilder.create(dataSource)
.name("MyDS")
.multiline()
.logQueryBySlf4j(SLF4JLogLevel.INFO)
.logQueryBySlf4j(SLF4JLogLevel.DEBUG)
.listener(new DataSourceQueryCountListener())
.build();
}
@@ -2,8 +2,10 @@ package com.baeldung.h2db.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:application-h2.properties")
public class SpringBootH2Application {
public static void main(String... args) {
@@ -0,0 +1,7 @@
package com.baeldung.h2db.springboot.daos;
import com.baeldung.h2db.springboot.models.Country;
import org.springframework.data.repository.CrudRepository;
public interface CountryRepository extends CrudRepository<Country, Integer> {
}
@@ -1,10 +0,0 @@
package com.baeldung.h2db.springboot.daos;
import com.baeldung.h2db.springboot.models.User;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Integer> {
}
@@ -0,0 +1,57 @@
package com.baeldung.h2db.springboot.models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Objects;
@Table(name = "countries")
@Entity
public class Country {
@Id
@GeneratedValue
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Country country = (Country) o;
return id == country.id && name.equals(country.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
@Override
public String toString() {
return "Country{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
@@ -1,54 +0,0 @@
package com.baeldung.h2db.springboot.models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "users")
@Entity
public class User {
@Id
@GeneratedValue
private int id;
private String firstName;
private String lastName;
public User() { }
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
@@ -0,0 +1 @@
spring.sql.init.data-locations=data-h2.sql
@@ -4,7 +4,7 @@ spring.datasource.username=sa
spring.datasource.password=
spring.jpa.defer-datasource-initialization=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.validator.apply_to_ddl=false
#spring.jpa.properties.hibernate.check_nullability=true
@@ -0,0 +1,5 @@
INSERT INTO countries (id, name) VALUES (1, 'USA');
INSERT INTO countries (id, name) VALUES (2, 'France');
INSERT INTO countries (id, name) VALUES (3, 'Brazil');
INSERT INTO countries (id, name) VALUES (4, 'Italy');
INSERT INTO countries (id, name) VALUES (5, 'Canada');
@@ -1,17 +1,3 @@
DROP TABLE IF EXISTS billionaires;
CREATE TABLE billionaires (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(250) NOT NULL,
last_name VARCHAR(250) NOT NULL,
career VARCHAR(250) DEFAULT NULL
);
INSERT INTO billionaires (first_name, last_name, career) VALUES
('Aliko', 'Dangote', 'Billionaire Industrialist'),
('Bill', 'Gates', 'Billionaire Tech Entrepreneur'),
('Folrunsho', 'Alakija', 'Billionaire Oil Magnate');
insert into USER values (101, 'user1', 'comment1');
insert into USER values (102, 'user2', 'comment2');
insert into USER values (103, 'user3', 'comment3');
@@ -1,50 +1,39 @@
package com.baeldung;
import com.baeldung.h2db.springboot.SpringBootH2Application;
import com.baeldung.h2db.springboot.daos.UserRepository;
import com.baeldung.h2db.springboot.models.User;
import com.baeldung.h2db.springboot.daos.CountryRepository;
import com.baeldung.h2db.springboot.models.Country;
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.util.List;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootH2Application.class)
public class SpringBootH2IntegrationTest {
private static final Country AN_EXPECTED_COUNTRY = buildCountry();
@Autowired
private UserRepository userRepository;
private CountryRepository countryRepository;
@Test
public void contextLoads() { }
public void givenInitData_whenApplicationStarts_thenDataIsLoaded() {
Iterable<Country> users = countryRepository.findAll();
@Test
public void givenUserProfile_whenAddUser_thenCreateNewUser() {
User user = new User();
user.setFirstName("John");
user.setLastName("Doe");
userRepository.save(user);
List<User> users = (List<User>) userRepository.findAll();
assertFalse(users.isEmpty());
assertThat(users)
.hasSize(5)
.contains(AN_EXPECTED_COUNTRY);
}
String firstName = "Aliko";
String lastName = "Dangote";
User user1 = userRepository.findById(users.get(0).getId()).get();
user1.setLastName(lastName);
user1.setFirstName(firstName);
userRepository.save(user1);
user = userRepository.findById(user.getId()).get();
assertEquals(user.getFirstName(), firstName);
assertEquals(user.getLastName(), lastName);
userRepository.deleteById(user.getId());
assertTrue( ((List<User>) userRepository.findAll()).isEmpty());
private static Country buildCountry() {
Country c = new Country();
c.setId(5);
c.setName("Canada");
return c;
}
}
@@ -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>
@@ -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>
@@ -7,5 +7,4 @@
- [Resolving “Failed to Configure a DataSource” Error](https://www.baeldung.com/spring-boot-failed-to-configure-data-source)
- [Hibernate Field Naming with Spring Boot](https://www.baeldung.com/hibernate-field-naming-spring-boot)
- [Spring Boot with Hibernate](https://www.baeldung.com/spring-boot-hibernate)
- [A Guide to Spring AbstractRoutingDatasource](https://www.baeldung.com/spring-abstract-routing-data-source)
- More articles: [[more -->]](../spring-boot-persistence-2)
- More articles: [[more -->]](../spring-boot-persistence-2)
@@ -1,3 +1,5 @@
drop table if exists client;
create table client (
id numeric,
name varchar(50),
@@ -1,72 +1,73 @@
<?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 https://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>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>spring-data-cassandra-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-data-cassandra-2</name>
<description>Demo project for Spring Data Cassandra</description>
<groupId>org.baeldung</groupId>
<artifactId>spring-cassandra</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cassandra</name>
<description>Demo project for Spring Data Cassandra</description>
<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-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
<version>${org.springframework.data.version}</version>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-core</artifactId>
<version>${system.stubs.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>cassandra</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<version>${system.stubs.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
<version>${org.springframework.data.version}</version>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-core</artifactId>
<version>${system.stubs.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>cassandra</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<version>${system.stubs.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>11</java.version>
<org.springframework.data.version>3.1.11</org.springframework.data.version>
<testcontainers.version>1.15.3</testcontainers.version>
<system.stubs.version>1.1.0</system.stubs.version>
<junit.jupiter.version>5.6.2</junit.jupiter.version>
</properties>
<properties>
<java.version>11</java.version>
<org.springframework.data.version>3.1.11</org.springframework.data.version>
<testcontainers.version>1.15.3</testcontainers.version>
<system.stubs.version>1.1.0</system.stubs.version>
<junit.jupiter.version>5.6.2</junit.jupiter.version>
</properties>
</project>
</project>
@@ -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>
@@ -49,7 +49,8 @@
<version>${cassandra-unit-shaded.version}</version>
<scope>test</scope>
<exclusions>
<!-- junit4 dependency is excluded as it should to be resolved from junit-vintage-engine included in parent-modules. -->
<!-- junit4 dependency is excluded as it should to be resolved from junit-vintage-engine
included in parent-modules. -->
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -66,6 +66,7 @@
<properties>
<spring.version>1.5.9.RELEASE</spring.version>
<eclipselink.version>2.7.0</eclipselink.version>
<h2.version>1.4.197</h2.version> <!-- needs to be specified as it fails with parent's 1.4.200 -->
</properties>
</project>
@@ -9,7 +9,7 @@ spring.datasource.url=jdbc:h2:mem:baeldung
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-source=metadata
#spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true
spring.jpa.show-sql=false
#hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.id.new_generator_mappings=false
@@ -3,6 +3,7 @@ package com.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.boot.Application;
@@ -7,6 +7,7 @@ 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.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.partialupdate.model.Customer;
@@ -0,0 +1,13 @@
<?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>
<logger name="org.hibernate" level="INFO"/>
</configuration>
@@ -10,8 +10,13 @@ import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FooFixtures {
private static final Logger LOGGER = LoggerFactory.getLogger(FooFixtures.class);
private SessionFactory sessionFactory;
public FooFixtures(final SessionFactory sessionFactory) {
@@ -36,8 +41,9 @@ public class FooFixtures {
foo.setBar(bar);
session.save(foo);
final Foo foo2 = new Foo(null);
if (i % 2 == 0)
if (i % 2 == 0) {
foo2.setName("LuckyFoo" + (i + 120));
}
foo2.setBar(bar);
session.save(foo2);
bar.getFooSet().add(foo);
@@ -47,15 +53,16 @@ public class FooFixtures {
tx.commit();
session.flush();
} catch (final HibernateException he) {
if (tx != null)
if (tx != null) {
tx.rollback();
System.out.println("Not able to open session");
he.printStackTrace();
}
LOGGER.error("Not able to open session", he);
} catch (final Exception e) {
e.printStackTrace();
LOGGER.error(e.getLocalizedMessage(), e);
} finally {
if (session != null)
if (session != null) {
session.close();
}
}
}
@@ -86,15 +93,16 @@ public class FooFixtures {
tx.commit();
session.flush();
} catch (final HibernateException he) {
if (tx != null)
if (tx != null) {
tx.rollback();
System.out.println("Not able to open session");
he.printStackTrace();
}
LOGGER.error("Not able to open session", he);
} catch (final Exception e) {
e.printStackTrace();
LOGGER.error(e.getLocalizedMessage(), e);
} finally {
if (session != null)
if (session != null) {
session.close();
}
}
}
@@ -15,6 +15,8 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -29,6 +31,8 @@ import com.baeldung.spring.config.PersistenceTestConfig;
@SuppressWarnings("unchecked")
public class FooSortingPersistenceIntegrationTest {
private static final Logger LOGGER = LoggerFactory.getLogger(FooSortingPersistenceIntegrationTest.class);
@Autowired
private SessionFactory sessionFactory;
@@ -56,7 +60,7 @@ public class FooSortingPersistenceIntegrationTest {
final Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
LOGGER.debug("Name: {} , Id: {}", foo.getName(), foo.getId());
}
}
@@ -68,7 +72,7 @@ public class FooSortingPersistenceIntegrationTest {
assertNull(fooList.get(fooList.toArray().length - 1).getName());
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
LOGGER.debug("Name: {}, Id: {}", foo.getName(), foo.getId());
}
}
@@ -79,7 +83,7 @@ public class FooSortingPersistenceIntegrationTest {
final List<Foo> fooList = query.list();
assertNull(fooList.get(0).getName());
for (final Foo foo : fooList) {
System.out.println("Name:" + foo.getName());
LOGGER.debug("Name: {}", foo.getName());
}
}
@@ -90,7 +94,7 @@ public class FooSortingPersistenceIntegrationTest {
final Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
LOGGER.debug("Name: {}, Id: {}", foo.getName(), foo.getId());
}
}
@@ -100,7 +104,7 @@ public class FooSortingPersistenceIntegrationTest {
final Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
LOGGER.debug("Name: {}, Id: {}", foo.getName(), foo.getId());
}
}
@@ -110,7 +114,7 @@ public class FooSortingPersistenceIntegrationTest {
final Query query = session.createQuery(hql);
final List<Foo> fooList = query.list();
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
LOGGER.debug("Name: {}, Id: {}", foo.getName(), foo.getId());
}
}
@@ -120,7 +124,7 @@ public class FooSortingPersistenceIntegrationTest {
criteria.addOrder(Order.asc("id"));
final List<Foo> fooList = criteria.list();
for (final Foo foo : fooList) {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
LOGGER.debug("Id: {}, FirstName: {}", foo.getId(), foo.getName());
}
}
@@ -131,7 +135,7 @@ public class FooSortingPersistenceIntegrationTest {
criteria.addOrder(Order.asc("id"));
final List<Foo> fooList = criteria.list();
for (final Foo foo : fooList) {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
LOGGER.debug("Id: {}, FirstName: {}", foo.getId(), foo.getName());
}
}
@@ -142,7 +146,7 @@ public class FooSortingPersistenceIntegrationTest {
final List<Foo> fooList = criteria.list();
assertNull(fooList.get(fooList.toArray().length - 1).getName());
for (final Foo foo : fooList) {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
LOGGER.debug("Id: {}, FirstName: {}", foo.getId(), foo.getName());
}
}
@@ -153,7 +157,7 @@ public class FooSortingPersistenceIntegrationTest {
final List<Foo> fooList = criteria.list();
assertNull(fooList.get(0).getName());
for (final Foo foo : fooList) {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
LOGGER.debug("Id: {}, FirstName: {}", foo.getId(), foo.getName());
}
}
@@ -164,9 +168,9 @@ public class FooSortingPersistenceIntegrationTest {
final List<Bar> barList = query.list();
for (final Bar bar : barList) {
final Set<Foo> fooSet = bar.getFooSet();
System.out.println("Bar Id:" + bar.getId());
LOGGER.debug("Bar Id:{}", bar.getId());
for (final Foo foo : fooSet) {
System.out.println("FooName:" + foo.getName());
LOGGER.debug("FooName:{}", foo.getName());
}
}
}
@@ -1,7 +1,10 @@
package com.baeldung.persistence.service;
import com.baeldung.persistence.hibernate.FooSortingPersistenceIntegrationTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.ContextConfiguration;
@@ -16,6 +19,8 @@ import com.baeldung.spring.config.PersistenceTestConfig;
@ContextConfiguration(classes = { PersistenceTestConfig.class }, loader = AnnotationConfigContextLoader.class)
public class ParentServicePersistenceIntegrationTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ParentServicePersistenceIntegrationTest.class);
@Autowired
private IParentService service;
@@ -37,11 +42,11 @@ public class ParentServicePersistenceIntegrationTest {
final Parent parentEntity = new Parent(childEntity);
service.create(parentEntity);
System.out.println("Child = " + childService.findOne(childEntity.getId()));
System.out.println("Child - parent = " + childService.findOne(childEntity.getId()).getParent());
LOGGER.debug("Child = {}", childService.findOne(childEntity.getId()));
LOGGER.debug("Child - parent = {}", childService.findOne(childEntity.getId()).getParent());
System.out.println("Parent = " + service.findOne(parentEntity.getId()));
System.out.println("Parent - child = " + service.findOne(parentEntity.getId()).getChild());
LOGGER.debug("Parent = {}", service.findOne(parentEntity.getId()));
LOGGER.debug("Parent - child = {}", service.findOne(parentEntity.getId()).getChild());
}
@Test(expected = DataIntegrityViolationException.class)
@@ -166,7 +166,7 @@ public class PersistenceTestConfig {
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.show_sql", "false");
// hibernateProperties.setProperty("hibernate.format_sql", "true");
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
@@ -19,7 +19,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@RunWith(SpringRunner.class)
@DataJpaTest(properties = "spring.sql.init.data-locations=classpath:insert_users.sql")
@DataJpaTest(properties = "spring.sql.init.data-locations=classpath:insert_users.sql", showSql = false)
public class UserRepositoryIntegrationTest {
@Autowired
@@ -17,7 +17,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
@DataJpaTest(properties="spring.sql.init.data-locations=classpath:import_entities.sql")
@DataJpaTest(properties="spring.sql.init.data-locations=classpath:import_entities.sql", showSql = false)
public class ArticleRepositoryIntegrationTest {
@Autowired
@@ -11,7 +11,7 @@
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="show_sql">false</property>
<mapping class="com.baeldung.hibernate.fetching.model.UserEager" />
<mapping class="com.baeldung.hibernate.fetching.model.OrderDetail" />
@@ -11,7 +11,7 @@
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="show_sql">false</property>
<mapping class="com.baeldung.hibernate.fetching.model.UserLazy" />
<mapping class="com.baeldung.hibernate.fetching.model.OrderDetail" />
@@ -1,2 +1,2 @@
spring.jpa.show-sql=true
spring.jpa.show-sql=false
spring.jpa.defer-datasource-initialization=true
@@ -16,8 +16,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(SpringRunner.class)
@DataJpaTest
@DataJpaTest(showSql = false)
@Sql(scripts = "/test-aggregation-data.sql")
public class SpringDataAggregateIntegrationTest {
@@ -26,8 +26,7 @@ import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@DataJpaTest
@DataJpaTest(showSql = false)
@RunWith(SpringRunner.class)
public class PassengerRepositoryIntegrationTest {
@@ -14,7 +14,7 @@ import com.baeldung.entitygraph.model.Item;
import com.baeldung.entitygraph.repository.CharacteristicsRepository;
import com.baeldung.entitygraph.repository.ItemRepository;
@DataJpaTest
@DataJpaTest(showSql = false)
@RunWith(SpringRunner.class)
@Sql(scripts = "/entitygraph-data.sql")
public class EntityGraphIntegrationTest {
@@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
@@ -13,10 +13,11 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@DataJpaTest
@DataJpaTest(showSql = false)
@ActiveProfiles("joins")
public class JpaJoinsIntegrationTest {
@@ -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>
@@ -1,5 +1,7 @@
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;
@@ -12,6 +14,8 @@ import java.util.List;
@SpringBootApplication
public class BookApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(BookApplication.class);
@Autowired
private BookRepository bookRepository;
@@ -25,13 +29,13 @@ public class BookApplication {
int bookCount = 10000;
long start = System.currentTimeMillis();
for(int i = 0; i < bookCount; i++) {
for (int i = 0; i < bookCount; i++) {
bookRepository.save(new Book("Book " + i, "Author " + i));
}
long end = System.currentTimeMillis();
bookRepository.deleteAll();
System.out.println("It took " + (end - start) + "ms to execute save() for " + bookCount + " books");
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++) {
@@ -42,7 +46,7 @@ public class BookApplication {
bookRepository.saveAll(bookList);
end = System.currentTimeMillis();
System.out.println("It took " + (end - start) + "ms to execute saveAll() with " + bookCount + " books\n");
LOGGER.debug("It took {}ms to execute saveAll() with {}} books.", (end - start), bookCount);
}
}
@@ -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>
@@ -1,4 +1,4 @@
spring.jpa.show-sql=true
spring.jpa.show-sql=false
spring.jpa.defer-datasource-initialization=true
#MySql
#spring.datasource.url=jdbc:mysql://localhost:3306/baeldung
@@ -21,7 +21,7 @@ import com.baeldung.boot.domain.Location;
import com.baeldung.boot.domain.Store;
@RunWith(SpringRunner.class)
@DataJpaTest(properties="spring.sql.init.data-locations=classpath:import_entities.sql")
@DataJpaTest(properties="spring.sql.init.data-locations=classpath:import_entities.sql", showSql = false)
public class JpaRepositoriesIntegrationTest {
@Autowired
private LocationRepository locationRepository;
@@ -18,7 +18,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.entity.Passenger;
@DataJpaTest
@DataJpaTest(showSql = false)
@RunWith(SpringRunner.class)
public class PassengerRepositoryIntegrationTest {
@@ -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>
@@ -71,7 +71,6 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
@@ -126,7 +125,6 @@
<properties>
<spring-tx.version>5.2.2.RELEASE</spring-tx.version>
<httpclient.version>4.5.2</httpclient.version>
<h2.version>1.4.200</h2.version>
<reactor-core.version>3.3.1.RELEASE</reactor-core.version>
</properties>
@@ -42,10 +42,6 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
@@ -78,7 +74,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<verbose>true</verbose>
<fork>false</fork>
+1 -1
View File
@@ -45,7 +45,7 @@
</dependencies>
<properties>
<spring-data-solr.version>2.0.5.RELEASE</spring-data-solr.version>
<spring-data-solr.version>4.3.14</spring-data-solr.version>
</properties>
</project>
@@ -9,13 +9,13 @@ import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
@Configuration
@EnableSolrRepositories(basePackages = "com.baeldung.spring.data.solr.repository", namedQueriesLocation = "classpath:solr-named-queries.properties", multicoreSupport = true)
@EnableSolrRepositories(basePackages = "com.baeldung.spring.data.solr.repository", namedQueriesLocation = "classpath:solr-named-queries.properties")
@ComponentScan
public class SolrConfig {
@Bean
public SolrClient solrClient() {
return new HttpSolrClient("http://localhost:8983/solr");
return new HttpSolrClient.Builder("http://localhost:8983/solr").build();
}
@Bean
@@ -1,6 +1,7 @@
package com.baeldung.spring.data.solr.repository;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@@ -11,6 +12,8 @@ import com.baeldung.spring.data.solr.model.Product;
public interface ProductRepository extends SolrCrudRepository<Product, String> {
public Optional<Product> findById(String id);
public List<Product> findByName(String name);
@Query("id:*?0* OR name:*?0*")
@@ -1,4 +1,4 @@
package com.baeldung.spring.data.solr.repo;
package com.baeldung.spring.data.solr.repo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@@ -36,7 +36,7 @@ public class ProductRepositoryLiveTest {
product.setId("P000089998");
product.setName("Desk");
productRepository.save(product);
final Product retrievedProduct = productRepository.findOne(product.getId());
final Product retrievedProduct = productRepository.findById(product.getId()).get();
assertEquals(product.getId(), retrievedProduct.getId());
}
@@ -51,7 +51,7 @@ public class ProductRepositoryLiveTest {
product.setName("Shirt");
productRepository.save(product);
final Product retrievedProduct = productRepository.findOne(product.getId());
final Product retrievedProduct = productRepository.findById(product.getId()).get();
assertEquals(product.getName(), retrievedProduct.getName());
}
@@ -64,7 +64,7 @@ public class ProductRepositoryLiveTest {
productRepository.delete(product);
Product retrievedProduct = productRepository.findOne(product.getId());
Product retrievedProduct = productRepository.findById(product.getId()).get();
assertNull(retrievedProduct);
}
@@ -97,7 +97,7 @@ public class ProductRepositoryLiveTest {
wirelessCharger.setName("Phone Charging Cable");
productRepository.save(wirelessCharger);
Page<Product> result = productRepository.findByCustomQuery("Phone", new PageRequest(0, 10));
Page<Product> result = productRepository.findByCustomQuery("Phone", PageRequest.of(0, 10));
assertEquals(3, result.getNumberOfElements());
}
@@ -118,7 +118,7 @@ public class ProductRepositoryLiveTest {
wirelessCharger.setName("Phone Charging Cable");
productRepository.save(wirelessCharger);
Page<Product> result = productRepository.findByNamedQuery("one", new PageRequest(0, 10));
Page<Product> result = productRepository.findByNamedQuery("one", PageRequest.of(0, 10));
assertEquals(3, result.getNumberOfElements());
}
@@ -74,7 +74,7 @@ public class DynamicUpdateConfig {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", Preconditions.checkNotNull(env.getProperty("hibernate.hbm2ddl.auto")));
properties.setProperty("hibernate.dialect", Preconditions.checkNotNull(env.getProperty("hibernate.dialect")));
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.show_sql", "false");
return properties;
}
}
@@ -6,8 +6,12 @@ import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HibernateUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(HibernateUtil.class);
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
@@ -20,7 +24,7 @@ public class HibernateUtil {
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
return configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.out.println("Initial SessionFactory creation failed." + ex);
LOGGER.debug("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
@@ -6,8 +6,12 @@ import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.baeldung.hibernate.manytomany.model.Employee;
import com.baeldung.hibernate.manytomany.model.Project;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HibernateUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(HibernateUtil.class);
private static SessionFactory sessionFactory;
private static SessionFactory buildSessionFactory() {
@@ -17,25 +21,25 @@ public class HibernateUtil {
configuration.addAnnotatedClass(Employee.class);
configuration.addAnnotatedClass(Project.class);
configuration.configure("manytomany.cfg.xml");
System.out.println("Hibernate Annotation Configuration loaded");
LOGGER.debug("Hibernate Annotation Configuration loaded");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
.build();
System.out.println("Hibernate Annotation serviceRegistry created");
.build();
LOGGER.debug("Hibernate Annotation serviceRegistry created");
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
ex.printStackTrace();
LOGGER.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
if (sessionFactory == null)
if (sessionFactory == null) {
sessionFactory = buildSessionFactory();
}
return sessionFactory;
}
}
@@ -63,7 +63,7 @@ public class PersistenceConfig {
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.show_sql", "false");
return hibernateProperties;
}
@@ -71,7 +71,7 @@ public class PersistenceConfig {
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.show_sql", "false");
// Envers properties
hibernateProperties.setProperty("org.hibernate.envers.audit_table_suffix", env.getProperty("envers.audit_table_suffix"));
@@ -23,7 +23,7 @@
<property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="show_sql">false</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
@@ -10,6 +10,6 @@
<property name="hibernate.connection.username">tutorialuser</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.show_sql">false</property>
</session-factory>
</hibernate-configuration>
@@ -10,7 +10,7 @@
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
</session-factory>
</hibernate-configuration>
@@ -9,6 +9,8 @@ import com.baeldung.spring.jdbc.template.guide.config.SpringJdbcConfig;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.test.context.ContextConfiguration;
@@ -19,6 +21,8 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class)
public class EmployeeDAOIntegrationTest {
private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeDAOIntegrationTest.class);
@Autowired
private EmployeeDAO employeeDao;
@@ -70,7 +74,7 @@ public class EmployeeDAOIntegrationTest {
try {
employeeDao.addEmplyee(7);
} catch (final DuplicateKeyException e) {
System.out.println(e.getMessage());
LOGGER.error(e.getMessage(), e);
Assert.assertTrue(e.getMessage().contains("Custome Exception translator - Integrity contraint voilation."));
}
}
@@ -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>
-1
View File
@@ -76,7 +76,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<forkCount>0</forkCount>
</configuration>
@@ -6,7 +6,7 @@ jdbc.pass=
# hibernate.X
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
@@ -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>
@@ -2,5 +2,5 @@ jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.show_sql=false
hibernate.hbm2ddl.auto=validate

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