Merge remote-tracking branch 'upstream/master'
This commit is contained in:
3
persistence-modules/apache-bookkeeper/README.md
Normal file
3
persistence-modules/apache-bookkeeper/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Guide to Apache BookKeeper](https://www.baeldung.com/java-apache-bookkeeper)
|
||||
@@ -3,6 +3,7 @@
|
||||
## Core Java Persistence Examples
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Introduction to JDBC](http://www.baeldung.com/java-jdbc)
|
||||
- [Batch Processing in JDBC](http://www.baeldung.com/jdbc-batch-processing)
|
||||
- [Introduction to the JDBC RowSet Interface in Java](http://www.baeldung.com/java-jdbc-rowset)
|
||||
@@ -10,3 +11,5 @@
|
||||
- [Guide to the JDBC ResultSet Interface](https://www.baeldung.com/jdbc-resultset)
|
||||
- [Types of SQL Joins](https://www.baeldung.com/sql-joins)
|
||||
- [Returning the Generated Keys in JDBC](https://www.baeldung.com/jdbc-returning-generated-keys)
|
||||
- [Loading JDBC Drivers](https://www.baeldung.com/java-jdbc-loading-drivers)
|
||||
- [Difference Between Statement and PreparedStatement](https://www.baeldung.com/java-statement-preparedstatement)
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.statmentVsPreparedstatment;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DatasourceFactory {
|
||||
|
||||
private Connection connection;
|
||||
|
||||
public Connection getConnection() throws ClassNotFoundException, SQLException {
|
||||
Class.forName("org.h2.Driver");
|
||||
connection = DriverManager.getConnection("jdbc:h2:mem:db_basic", "SA", "");
|
||||
connection.setAutoCommit(false);
|
||||
return connection;
|
||||
}
|
||||
|
||||
public boolean createTables() throws SQLException {
|
||||
String query = "create table if not exists PERSONS (ID INT, NAME VARCHAR(45))";
|
||||
return connection.createStatement().executeUpdate(query) == 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.statmentVsPreparedstatment;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class PersonEntity {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public PersonEntity(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = 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;
|
||||
PersonEntity that = (PersonEntity) o;
|
||||
return id == that.id && Objects.equals(name, that.name);
|
||||
}
|
||||
|
||||
@Override public int hashCode() {
|
||||
return Objects.hash(id, name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.baeldung.statmentVsPreparedstatment;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class PreparedStatementPersonDao {
|
||||
|
||||
private final Connection connection;
|
||||
|
||||
public PreparedStatementPersonDao(Connection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
public Optional<PersonEntity> getById(int id) throws SQLException {
|
||||
String query = "SELECT id, name FROM persons WHERE id = ?";
|
||||
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
preparedStatement.setInt(1, id);
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
|
||||
if (resultSet.first()) {
|
||||
|
||||
PersonEntity result = new PersonEntity(resultSet.getInt("id"),
|
||||
resultSet.getString("name"));
|
||||
|
||||
return Optional.of(result);
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void insert(PersonEntity personEntity) throws SQLException {
|
||||
|
||||
String query = "INSERT INTO persons(id, name) VALUES( ?, ?)";
|
||||
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
preparedStatement.setInt(1, personEntity.getId());
|
||||
preparedStatement.setString(2, personEntity.getName());
|
||||
preparedStatement.executeUpdate();
|
||||
|
||||
}
|
||||
|
||||
public void insert(List<PersonEntity> personEntities) throws SQLException {
|
||||
String query = "INSERT INTO persons(id, name) VALUES( ?, ?)";
|
||||
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
for (PersonEntity personEntity : personEntities) {
|
||||
preparedStatement.setInt(1, personEntity.getId());
|
||||
preparedStatement.setString(2, personEntity.getName());
|
||||
preparedStatement.addBatch();
|
||||
}
|
||||
preparedStatement.executeBatch();
|
||||
|
||||
}
|
||||
|
||||
public void update(PersonEntity personEntity) throws SQLException {
|
||||
String query = "UPDATE persons SET name = ? WHERE id = ?";
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
preparedStatement.setString(1, personEntity.getName());
|
||||
preparedStatement.setInt(2, personEntity.getId());
|
||||
preparedStatement.executeUpdate();
|
||||
}
|
||||
|
||||
public void deleteById(int id) throws SQLException {
|
||||
String query = "DELETE FROM persons WHERE id = ?";
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
preparedStatement.setInt(1, id);
|
||||
preparedStatement.executeUpdate();
|
||||
}
|
||||
|
||||
public List<PersonEntity> getAll() throws SQLException {
|
||||
String query = "SELECT id, name FROM persons";
|
||||
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
List<PersonEntity> result = new ArrayList<>();
|
||||
while (resultSet.next()) {
|
||||
result.add(new PersonEntity(resultSet.getInt("id"), resultSet.getString("name")));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.baeldung.statmentVsPreparedstatment;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class StatementPersonDao {
|
||||
|
||||
private final Connection connection;
|
||||
|
||||
public StatementPersonDao(Connection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
public Optional<PersonEntity> getById(int id) throws SQLException {
|
||||
String query = "SELECT id, name, FROM persons WHERE id = '" + id + "'";
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet resultSet = statement.executeQuery(query);
|
||||
|
||||
if (resultSet.first()) {
|
||||
PersonEntity result = new PersonEntity(resultSet.getInt("id"),
|
||||
resultSet.getString("name"));
|
||||
return Optional.of(result);
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public void insert(PersonEntity personEntity) throws SQLException {
|
||||
String query = "INSERT INTO persons(id, name) VALUES(" + personEntity.getId() + ", '"
|
||||
+ personEntity.getName() + "')";
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
statement.executeUpdate(query);
|
||||
}
|
||||
|
||||
public void insert(List<PersonEntity> personEntities) throws SQLException {
|
||||
for (PersonEntity personEntity : personEntities) {
|
||||
insert(personEntity);
|
||||
}
|
||||
}
|
||||
|
||||
public void update(PersonEntity personEntity) throws SQLException {
|
||||
|
||||
String query = "UPDATE persons SET name = '" + personEntity.getName() + "' WHERE id = "
|
||||
+ personEntity.getId();
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
statement.executeUpdate(query);
|
||||
|
||||
}
|
||||
|
||||
public void deleteById(int id) throws SQLException {
|
||||
String query = "DELETE FROM persons WHERE id = " + id;
|
||||
Statement statement = connection.createStatement();
|
||||
statement.executeUpdate(query);
|
||||
}
|
||||
|
||||
public List<PersonEntity> getAll() throws SQLException {
|
||||
String query = "SELECT id, name, FROM persons";
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet resultSet = statement.executeQuery(query);
|
||||
List<PersonEntity> result = new ArrayList<>();
|
||||
while (resultSet.next()) {
|
||||
result.add(new PersonEntity(resultSet.getInt("id"), resultSet.getString("name")));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.statmentVsPreparedstatment;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class DatasourceFactoryUnitTest {
|
||||
|
||||
@Test
|
||||
void whenCreateConnectionAndTables_thenConnectionIsOpenAndTableIsCreated()
|
||||
throws SQLException, ClassNotFoundException {
|
||||
DatasourceFactory factory = new DatasourceFactory();
|
||||
Connection connection = factory.getConnection();
|
||||
|
||||
assertFalse(connection.isClosed());
|
||||
assertTrue(factory.createTables());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.baeldung.statmentVsPreparedstatment;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PreparedStatementPersonDaoUnitTest {
|
||||
private PreparedStatementPersonDao dao;
|
||||
|
||||
@BeforeEach
|
||||
void setup() throws SQLException, ClassNotFoundException {
|
||||
DatasourceFactory datasourceFactory = new DatasourceFactory();
|
||||
Connection connection = datasourceFactory.getConnection();
|
||||
datasourceFactory.createTables();
|
||||
dao = new PreparedStatementPersonDao(connection);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInsertAPerson_thenItNeverThrowsAnException() {
|
||||
assertDoesNotThrow(() -> dao.insert(new PersonEntity(1, "john")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInsertAPersonWithQuoteInText_thenItNeverThrowsAnException() {
|
||||
assertDoesNotThrow(() -> dao.insert(new PersonEntity(1, "O'Brien")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGetAPersonById_thenItReturnThePersonInDatabase() throws SQLException {
|
||||
dao.insert(new PersonEntity(1, "john"));
|
||||
|
||||
Optional<PersonEntity> maybeEmployee = dao.getById(1);
|
||||
assertTrue(maybeEmployee.isPresent());
|
||||
|
||||
PersonEntity personEntity = maybeEmployee.get();
|
||||
|
||||
assertEquals(1, personEntity.getId());
|
||||
assertEquals("john", personEntity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInsertAndGetMultiplePersons_thenItNeverThrowsAnException() throws SQLException {
|
||||
assertDoesNotThrow(() -> dao.insert(
|
||||
Arrays.asList(new PersonEntity(1, "john"), new PersonEntity(2, "skit"))));
|
||||
|
||||
List<PersonEntity> result = dao.getAll();
|
||||
|
||||
assertEquals(Arrays.asList(new PersonEntity(1, "john"), new PersonEntity(2, "skit")),
|
||||
result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUpdateAnExistentPerson_thenItReturnsTheUpdatedPerson() throws SQLException {
|
||||
dao.insert(new PersonEntity(1, "john"));
|
||||
dao.update(new PersonEntity(1, "johnny"));
|
||||
|
||||
Optional<PersonEntity> maybePerson = dao.getById(1);
|
||||
|
||||
assertTrue(maybePerson.isPresent());
|
||||
|
||||
PersonEntity personEntity = maybePerson.get();
|
||||
|
||||
assertEquals(1, personEntity.getId());
|
||||
assertEquals("johnny", personEntity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDeleteAPersonById_thenItWillBeAbsentInDatabase() throws SQLException {
|
||||
dao.insert(new PersonEntity(1, "john"));
|
||||
dao.deleteById(1);
|
||||
|
||||
Optional<PersonEntity> maybePerson = dao.getById(1);
|
||||
|
||||
assertFalse(maybePerson.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenAHackerUpdateAPerson_thenItUpdatesTheTargetPerson() throws SQLException {
|
||||
dao.insert(Arrays.asList(new PersonEntity(1, "john"), new PersonEntity(2, "skeet")));
|
||||
dao.update(new PersonEntity(1, "hacker' --"));
|
||||
|
||||
List<PersonEntity> result = dao.getAll();
|
||||
|
||||
assertEquals(Arrays.asList(new PersonEntity(1, "hacker' --"), new PersonEntity(2, "skeet")),
|
||||
result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.baeldung.statmentVsPreparedstatment;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class StatementPersonDaoUnitTest {
|
||||
|
||||
private StatementPersonDao dao;
|
||||
|
||||
@BeforeEach
|
||||
void setup() throws SQLException, ClassNotFoundException {
|
||||
DatasourceFactory datasourceFactory = new DatasourceFactory();
|
||||
Connection connection = datasourceFactory.getConnection();
|
||||
datasourceFactory.createTables();
|
||||
dao = new StatementPersonDao(connection);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInsertAPerson_thenItNeverThrowsAnException() {
|
||||
assertDoesNotThrow(() -> dao.insert(new PersonEntity(1, "john")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInsertAPersonWithQuoteInText_thenItWillThrowAnException() {
|
||||
assertThrows(SQLException.class, () -> dao.insert(new PersonEntity(1, "O'Brien")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGetAPersonById_thenItReturnThePersonInDatabase() throws SQLException {
|
||||
dao.insert(new PersonEntity(1, "john"));
|
||||
|
||||
Optional<PersonEntity> maybeEmployee = dao.getById(1);
|
||||
|
||||
assertTrue(maybeEmployee.isPresent());
|
||||
|
||||
PersonEntity personEntity = maybeEmployee.get();
|
||||
|
||||
assertEquals(1, personEntity.getId());
|
||||
assertEquals("john", personEntity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInsertAndGetMultiplePersons_thenItNeverThrowsAnException() throws SQLException {
|
||||
assertDoesNotThrow(() -> dao.insert(
|
||||
Arrays.asList(new PersonEntity(1, "john"), new PersonEntity(2, "skeet"))));
|
||||
|
||||
List<PersonEntity> result = dao.getAll();
|
||||
|
||||
assertEquals(Arrays.asList(new PersonEntity(1, "john"), new PersonEntity(2, "skeet")),
|
||||
result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUpdateAnExistentPerson_thenItReturnsTheUpdatedPerson() throws SQLException {
|
||||
dao.insert(new PersonEntity(1, "john"));
|
||||
dao.update(new PersonEntity(1, "johnny"));
|
||||
|
||||
Optional<PersonEntity> maybePerson = dao.getById(1);
|
||||
|
||||
assertTrue(maybePerson.isPresent());
|
||||
|
||||
PersonEntity personEntity = maybePerson.get();
|
||||
|
||||
assertEquals(1, personEntity.getId());
|
||||
assertEquals("johnny", personEntity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDeleteAPersonById_thenItWillBeAbsentInDatabase() throws SQLException {
|
||||
dao.insert(new PersonEntity(1, "john"));
|
||||
dao.deleteById(1);
|
||||
|
||||
Optional<PersonEntity> maybePerson = dao.getById(1);
|
||||
|
||||
assertFalse(maybePerson.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenAHackerUpdateAPerson_thenItAllPersonsAreUpdated() throws SQLException {
|
||||
dao.insert(Arrays.asList(new PersonEntity(1, "john"), new PersonEntity(2, "skeet")));
|
||||
dao.update(new PersonEntity(1, "hacker' --"));
|
||||
|
||||
List<PersonEntity> result = dao.getAll();
|
||||
|
||||
assertEquals(Arrays.asList(new PersonEntity(1, "hacker"), new PersonEntity(2, "hacker")),
|
||||
result);
|
||||
}
|
||||
}
|
||||
1
persistence-modules/flyway-repair/README.MD
Normal file
1
persistence-modules/flyway-repair/README.MD
Normal file
@@ -0,0 +1 @@
|
||||
### Relevant Articles:
|
||||
1
persistence-modules/flyway-repair/docker-compose/.env
Normal file
1
persistence-modules/flyway-repair/docker-compose/.env
Normal file
@@ -0,0 +1 @@
|
||||
POSTGRES_PORT=5431
|
||||
@@ -0,0 +1,15 @@
|
||||
version: '3.0'
|
||||
|
||||
services:
|
||||
|
||||
postgres-test:
|
||||
image: postgres:11.5
|
||||
ports:
|
||||
- ${POSTGRES_PORT}:5432
|
||||
env_file: postgres.env
|
||||
networks:
|
||||
- baeldung
|
||||
|
||||
networks:
|
||||
baeldung:
|
||||
driver: bridge
|
||||
@@ -0,0 +1,3 @@
|
||||
POSTGRES_USER=testuser
|
||||
POSTGRES_PASSWORD=password
|
||||
POSTGRES_DB=testdb
|
||||
80
persistence-modules/flyway-repair/pom.xml
Normal file
80
persistence-modules/flyway-repair/pom.xml
Normal file
@@ -0,0 +1,80 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>flyway</artifactId>
|
||||
<name>flyway-repair</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Flyway Repair Demo</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.flywaydb</groupId>
|
||||
<artifactId>flyway-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
|
||||
<profile>
|
||||
<id>h2</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<properties>
|
||||
<spring-boot.run.profiles>h2</spring-boot.run.profiles>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</profile>
|
||||
|
||||
<profile>
|
||||
<id>postgres</id>
|
||||
<properties>
|
||||
<spring-boot.run.profiles>postgres</spring-boot.run.profiles>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</profile>
|
||||
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<flyway.configFiles>src/main/resources/application-${spring-boot.run.profiles}.properties</flyway.configFiles>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.flywaycallbacks;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class FlywayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(FlywayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
spring.flyway.locations=classpath:db/migration,classpath:db/callback
|
||||
@@ -0,0 +1,3 @@
|
||||
flyway.url=jdbc:h2:file:./testdb;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE;MODE=MySQL;DATABASE_TO_UPPER=false;
|
||||
flyway.user=testuser
|
||||
flyway.password=password
|
||||
@@ -0,0 +1,3 @@
|
||||
flyway.url=jdbc:postgresql://127.0.0.1:5431/testdb
|
||||
flyway.user=testuser
|
||||
flyway.password=password
|
||||
@@ -0,0 +1,3 @@
|
||||
spring.datasource.url=${flyway.url}
|
||||
spring.datasource.username=${flyway.user}
|
||||
spring.datasource.password=${flyway.password}
|
||||
@@ -0,0 +1 @@
|
||||
DELETE FROM flyway_schema_history WHERE success=false;
|
||||
@@ -0,0 +1,3 @@
|
||||
create table table_one (
|
||||
id numeric primary key
|
||||
);
|
||||
@@ -0,0 +1,3 @@
|
||||
create table table_two (
|
||||
id numeric primary key
|
||||
);
|
||||
@@ -0,0 +1,3 @@
|
||||
create table table_three (
|
||||
id numeric primary key
|
||||
);
|
||||
@@ -0,0 +1,3 @@
|
||||
create table table_four (
|
||||
id numeric primary key
|
||||
);
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -1,3 +1,5 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Hibernate could not initialize proxy – no Session](https://www.baeldung.com/hibernate-initialize-proxy-exception)
|
||||
- [Hibernate Error “No Persistence Provider for EntityManager”](https://www.baeldung.com/hibernate-no-persistence-provider)
|
||||
- [TransactionRequiredException Error](https://www.baeldung.com/jpa-transaction-required-exception)
|
||||
@@ -11,7 +11,5 @@ This module contains articles specific to use of Hibernate as a JPA implementati
|
||||
- [Criteria API – An Example of IN Expressions](https://www.baeldung.com/jpa-criteria-api-in-expressions)
|
||||
- [One-to-One Relationship in JPA](https://www.baeldung.com/jpa-one-to-one)
|
||||
- [Enabling Transaction Locks in Spring Data JPA](https://www.baeldung.com/java-jpa-transaction-locks)
|
||||
- [TransactionRequiredException Error](https://www.baeldung.com/jpa-transaction-required-exception)
|
||||
- [JPA/Hibernate Persistence Context](https://www.baeldung.com/jpa-hibernate-persistence-context)
|
||||
- [Quick Guide to EntityManager#getReference()](https://www.baeldung.com/jpa-entity-manager-get-reference)
|
||||
- [Hibernate Error “No Persistence Provider for EntityManager”](https://www.baeldung.com/hibernate-no-persistence-provider)
|
||||
|
||||
@@ -98,7 +98,6 @@
|
||||
<cglib.version>3.2.4</cglib.version>
|
||||
<nosqlunit.version>0.10.0</nosqlunit.version>
|
||||
<embedded-redis.version>0.6</embedded-redis.version>
|
||||
<spring-boot.version>2.1.9.RELEASE</spring-boot.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -31,11 +31,6 @@ public class RedisConfig {
|
||||
return new ReactiveRedisTemplate<>(factory, context);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ReactiveRedisTemplate<String, String> reactiveRedisTemplateString(ReactiveRedisConnectionFactory connectionFactory) {
|
||||
return new ReactiveRedisTemplate<>(connectionFactory, RedisSerializationContext.string());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ReactiveKeyCommands keyCommands(final ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
|
||||
return reactiveRedisConnectionFactory.getReactiveConnection()
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.spring.data.redis;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringRedisApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringRedisApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,9 +2,6 @@ package com.baeldung.spring.data.reactive.redis.template;
|
||||
|
||||
|
||||
import com.baeldung.spring.data.reactive.redis.SpringRedisReactiveApplication;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
@@ -13,7 +10,7 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.core.ReactiveListOperations;
|
||||
import org.springframework.data.redis.core.ReactiveRedisTemplate;
|
||||
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
@@ -21,6 +18,8 @@ import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
import redis.embedded.RedisServerBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringRedisReactiveApplication.class)
|
||||
@DirtiesContext(classMode = ClassMode.BEFORE_CLASS)
|
||||
@@ -30,7 +29,7 @@ public class RedisTemplateListOpsIntegrationTest {
|
||||
private static redis.embedded.RedisServer redisServer;
|
||||
|
||||
@Autowired
|
||||
private ReactiveRedisTemplate<String, String> redisTemplate;
|
||||
private ReactiveStringRedisTemplate redisTemplate;
|
||||
|
||||
private ReactiveListOperations<String, String> reactiveListOps;
|
||||
|
||||
|
||||
@@ -1,44 +1,40 @@
|
||||
package com.baeldung.spring.data.redis;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
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.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.spring.data.redis.config.RedisConfig;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
||||
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import redis.embedded.RedisServerBuilder;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = RedisConfig.class)
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringRedisApplication.class)
|
||||
@DirtiesContext(classMode = ClassMode.BEFORE_CLASS)
|
||||
public class RedisMessageListenerIntegrationTest {
|
||||
|
||||
private static redis.embedded.RedisServer redisServer;
|
||||
|
||||
|
||||
@Autowired
|
||||
private RedisMessagePublisher redisMessagePublisher;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void startRedisServer() throws IOException {
|
||||
public static void startRedisServer() {
|
||||
redisServer = new RedisServerBuilder().port(6379).setting("maxmemory 256M").build();
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
|
||||
@AfterClass
|
||||
public static void stopRedisServer() throws IOException {
|
||||
public static void stopRedisServer() {
|
||||
redisServer.stop();
|
||||
}
|
||||
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
- [Spring JdbcTemplate Unit Testing](https://www.baeldung.com/spring-jdbctemplate-testing)
|
||||
- [Using a List of Values in a JdbcTemplate IN Clause](https://www.baeldung.com/spring-jdbctemplate-in-list)
|
||||
- [Transactional Annotations: Spring vs. JTA](https://www.baeldung.com/spring-vs-jta-transactional)
|
||||
- [Test a Mock JNDI Datasource with Spring](https://www.baeldung.com/spring-mock-jndi-datasource)
|
||||
|
||||
Reference in New Issue
Block a user