JAVA-3542: Moved spring-jooq inside persistence-modules
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/src/main/java/com/baeldung/jooq/introduction/db
|
||||
@@ -0,0 +1,11 @@
|
||||
## Spring jOOQ
|
||||
|
||||
This module contains articles about Spring with jOOQ
|
||||
|
||||
### Relevant Articles:
|
||||
- [Spring Boot Support for jOOQ](https://www.baeldung.com/spring-boot-support-for-jooq)
|
||||
- [Introduction to jOOQ with Spring](https://www.baeldung.com/jooq-with-spring)
|
||||
|
||||
In order to fix the error "Plugin execution not covered by lifecycle configuration: org.jooq:jooq-codegen-maven:3.7.3:generate (execution: default, phase: generate-sources)", right-click on the error message and choose "Mark goal generated as ignore in pom.xml". Until version 1.4.x, the maven-plugin-plugin was covered by the default lifecycle mapping that ships with m2e.
|
||||
|
||||
Since version 1.5.x, the m2e default lifecycle mapping no longer covers the maven-plugin-plugin.
|
||||
@@ -0,0 +1,212 @@
|
||||
<?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>spring-jooq</artifactId>
|
||||
<name>spring-jooq</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<!-- Import dependency management from Spring Boot -->
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- jOOQ -->
|
||||
<dependency>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>jooq</artifactId>
|
||||
<version>${org.jooq.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Database Access -->
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jooq</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Testing -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
<!-- Because we couldn't cope with multiple concurrent H2 instances -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<forkCount>0</forkCount>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>properties-maven-plugin</artifactId>
|
||||
<version>${properties-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>initialize</phase>
|
||||
<goals>
|
||||
<goal>read-project-properties</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<files>
|
||||
<file>src/main/resources/intro_config.properties</file>
|
||||
</files>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>sql-maven-plugin</artifactId>
|
||||
<version>${sql-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>initialize</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<driver>${db.driver}</driver>
|
||||
<url>${db.url}</url>
|
||||
<username>${db.username}</username>
|
||||
<password>${db.password}</password>
|
||||
<srcFiles>
|
||||
<srcFile>src/main/resources/intro_schema.sql</srcFile>
|
||||
</srcFiles>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>jooq-codegen-maven</artifactId>
|
||||
<version>${org.jooq.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>generate</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<jdbc>
|
||||
<driver>${db.driver}</driver>
|
||||
<url>${db.url}</url>
|
||||
<user>${db.username}</user>
|
||||
<password>${db.password}</password>
|
||||
</jdbc>
|
||||
<generator>
|
||||
<target>
|
||||
<packageName>com.baeldung.jooq.introduction.db</packageName>
|
||||
<directory>src/main/java</directory>
|
||||
</target>
|
||||
<database>
|
||||
<inputCatalog></inputCatalog>
|
||||
</database>
|
||||
</generator>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself. -->
|
||||
<plugin>
|
||||
<groupId>org.eclipse.m2e</groupId>
|
||||
<artifactId>lifecycle-mapping</artifactId>
|
||||
<version>${lifecycle-mapping.version}</version>
|
||||
<configuration>
|
||||
<lifecycleMappingMetadata>
|
||||
<pluginExecutions>
|
||||
<pluginExecution>
|
||||
<pluginExecutionFilter>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>
|
||||
jooq-codegen-maven
|
||||
</artifactId>
|
||||
<versionRange>
|
||||
[3.7.3,)
|
||||
</versionRange>
|
||||
<goals>
|
||||
<goal>generate</goal>
|
||||
</goals>
|
||||
</pluginExecutionFilter>
|
||||
<action>
|
||||
<ignore></ignore>
|
||||
</action>
|
||||
</pluginExecution>
|
||||
</pluginExecutions>
|
||||
</lifecycleMappingMetadata>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<org.jooq.version>3.12.4</org.jooq.version>
|
||||
<lifecycle-mapping.version>1.0.0</lifecycle-mapping.version>
|
||||
<sql-maven-plugin.version>1.5</sql-maven-plugin.version>
|
||||
<properties-maven-plugin.version>1.0.0</properties-maven-plugin.version>
|
||||
<h2.version>1.4.198</h2.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,3 @@
|
||||
spring.datasource.url=jdbc:h2:~/jooq
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
@@ -0,0 +1,8 @@
|
||||
#Database Configuration
|
||||
db.driver=org.h2.Driver
|
||||
db.url=jdbc:h2:~/jooq
|
||||
db.username=sa
|
||||
db.password=
|
||||
|
||||
#SQL Dialect
|
||||
jooq.sql.dialect=H2
|
||||
@@ -0,0 +1,34 @@
|
||||
DROP TABLE IF EXISTS author_book, author, book;
|
||||
|
||||
CREATE TABLE author (
|
||||
id INT NOT NULL PRIMARY KEY,
|
||||
first_name VARCHAR(50),
|
||||
last_name VARCHAR(50) NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE book (
|
||||
id INT NOT NULL PRIMARY KEY,
|
||||
title VARCHAR(100) NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE author_book (
|
||||
author_id INT NOT NULL,
|
||||
book_id INT NOT NULL,
|
||||
|
||||
PRIMARY KEY (author_id, book_id),
|
||||
CONSTRAINT fk_ab_author FOREIGN KEY (author_id) REFERENCES author (id)
|
||||
ON UPDATE CASCADE ON DELETE CASCADE,
|
||||
CONSTRAINT fk_ab_book FOREIGN KEY (book_id) REFERENCES book (id)
|
||||
);
|
||||
|
||||
INSERT INTO author VALUES
|
||||
(1, 'Kathy', 'Sierra'),
|
||||
(2, 'Bert', 'Bates'),
|
||||
(3, 'Bryan', 'Basham');
|
||||
|
||||
INSERT INTO book VALUES
|
||||
(1, 'Head First Java'),
|
||||
(2, 'Head First Servlets and JSP'),
|
||||
(3, 'OCA/OCP Java SE 7 Programmer');
|
||||
|
||||
INSERT INTO author_book VALUES (1, 1), (1, 3), (2, 1);
|
||||
@@ -0,0 +1,13 @@
|
||||
<?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>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.jooq.introduction;
|
||||
|
||||
import org.jooq.ExecuteContext;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.impl.DefaultExecuteListener;
|
||||
|
||||
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
|
||||
import org.springframework.jdbc.support.SQLExceptionTranslator;
|
||||
|
||||
public class ExceptionTranslator extends DefaultExecuteListener {
|
||||
|
||||
@Override
|
||||
public void exception(ExecuteContext context) {
|
||||
SQLDialect dialect = context.configuration().dialect();
|
||||
SQLExceptionTranslator translator = new SQLErrorCodeSQLExceptionTranslator(dialect.thirdParty().springDbName());
|
||||
|
||||
context.exception(translator.translate("Access database using jOOQ", context.sql(), context.sqlException()));
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package com.baeldung.jooq.introduction;
|
||||
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.impl.DataSourceConnectionProvider;
|
||||
import org.jooq.impl.DefaultConfiguration;
|
||||
import org.jooq.impl.DefaultDSLContext;
|
||||
import org.jooq.impl.DefaultExecuteListenerProvider;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan({ "com.baeldung.jooq.introduction.db.public_.tables" })
|
||||
@EnableTransactionManagement
|
||||
@PropertySource("classpath:intro_config.properties")
|
||||
public class PersistenceContextIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
JdbcDataSource dataSource = new JdbcDataSource();
|
||||
|
||||
dataSource.setUrl(environment.getRequiredProperty("db.url"));
|
||||
dataSource.setUser(environment.getRequiredProperty("db.username"));
|
||||
dataSource.setPassword(environment.getRequiredProperty("db.password"));
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TransactionAwareDataSourceProxy transactionAwareDataSource() {
|
||||
return new TransactionAwareDataSourceProxy(dataSource());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSourceTransactionManager transactionManager() {
|
||||
return new DataSourceTransactionManager(dataSource());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSourceConnectionProvider connectionProvider() {
|
||||
return new DataSourceConnectionProvider(transactionAwareDataSource());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ExceptionTranslator exceptionTransformer() {
|
||||
return new ExceptionTranslator();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DefaultDSLContext dsl() {
|
||||
return new DefaultDSLContext(configuration());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DefaultConfiguration configuration() {
|
||||
DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
|
||||
jooqConfiguration.set(connectionProvider());
|
||||
jooqConfiguration.set(new DefaultExecuteListenerProvider(exceptionTransformer()));
|
||||
|
||||
String sqlDialectName = environment.getRequiredProperty("jooq.sql.dialect");
|
||||
SQLDialect dialect = SQLDialect.valueOf(sqlDialectName);
|
||||
jooqConfiguration.set(dialect);
|
||||
|
||||
return jooqConfiguration;
|
||||
}
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
package com.baeldung.jooq.introduction;
|
||||
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.Record3;
|
||||
import org.jooq.Result;
|
||||
import org.jooq.impl.DSL;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import static com.baeldung.jooq.introduction.db.public_.tables.Author.AUTHOR;
|
||||
import static com.baeldung.jooq.introduction.db.public_.tables.AuthorBook.AUTHOR_BOOK;
|
||||
import static com.baeldung.jooq.introduction.db.public_.tables.Book.BOOK;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@ContextConfiguration(classes = PersistenceContextIntegrationTest.class)
|
||||
@Transactional(transactionManager = "transactionManager")
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class QueryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private DSLContext dsl;
|
||||
|
||||
@Test
|
||||
public void givenValidData_whenInserting_thenSucceed() {
|
||||
dsl.insertInto(AUTHOR)
|
||||
.set(AUTHOR.ID, 4)
|
||||
.set(AUTHOR.FIRST_NAME, "Herbert")
|
||||
.set(AUTHOR.LAST_NAME, "Schildt")
|
||||
.execute();
|
||||
|
||||
dsl.insertInto(BOOK)
|
||||
.set(BOOK.ID, 4)
|
||||
.set(BOOK.TITLE, "A Beginner's Guide")
|
||||
.execute();
|
||||
|
||||
dsl.insertInto(AUTHOR_BOOK)
|
||||
.set(AUTHOR_BOOK.AUTHOR_ID, 4)
|
||||
.set(AUTHOR_BOOK.BOOK_ID, 4)
|
||||
.execute();
|
||||
|
||||
final Result<Record3<Integer, String, Integer>> result = dsl.select(AUTHOR.ID, AUTHOR.LAST_NAME, DSL.count())
|
||||
.from(AUTHOR)
|
||||
.join(AUTHOR_BOOK).on(AUTHOR.ID.equal(AUTHOR_BOOK.AUTHOR_ID))
|
||||
.join(BOOK).on(AUTHOR_BOOK.BOOK_ID.equal(BOOK.ID))
|
||||
.groupBy(AUTHOR.LAST_NAME)
|
||||
.orderBy(AUTHOR.LAST_NAME.desc())
|
||||
.fetch();
|
||||
|
||||
assertEquals(3, result.size());
|
||||
assertEquals("Sierra", result.getValue(0, AUTHOR.LAST_NAME));
|
||||
assertEquals(Integer.valueOf(2), result.getValue(0, DSL.count()));
|
||||
assertEquals("Bates", result.getValue(2, AUTHOR.LAST_NAME));
|
||||
assertEquals(Integer.valueOf(1), result.getValue(2, DSL.count()));
|
||||
}
|
||||
|
||||
@Test(expected = DataAccessException.class)
|
||||
public void givenInvalidData_whenInserting_thenFail() {
|
||||
dsl.insertInto(AUTHOR_BOOK).set(AUTHOR_BOOK.AUTHOR_ID, 4).set(AUTHOR_BOOK.BOOK_ID, 5).execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidData_whenUpdating_thenSucceed() {
|
||||
dsl.update(AUTHOR)
|
||||
.set(AUTHOR.LAST_NAME, "Baeldung")
|
||||
.where(AUTHOR.ID.equal(3))
|
||||
.execute();
|
||||
|
||||
dsl.update(BOOK)
|
||||
.set(BOOK.TITLE, "Building your REST API with Spring")
|
||||
.where(BOOK.ID.equal(3)).execute();
|
||||
|
||||
dsl.insertInto(AUTHOR_BOOK)
|
||||
.set(AUTHOR_BOOK.AUTHOR_ID, 3)
|
||||
.set(AUTHOR_BOOK.BOOK_ID, 3)
|
||||
.execute();
|
||||
|
||||
final Result<Record3<Integer, String, String>> result = dsl.select(AUTHOR.ID, AUTHOR.LAST_NAME, BOOK.TITLE)
|
||||
.from(AUTHOR)
|
||||
.join(AUTHOR_BOOK).on(AUTHOR.ID.equal(AUTHOR_BOOK.AUTHOR_ID))
|
||||
.join(BOOK).on(AUTHOR_BOOK.BOOK_ID.equal(BOOK.ID))
|
||||
.where(AUTHOR.ID.equal(3))
|
||||
.fetch();
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(Integer.valueOf(3), result.getValue(0, AUTHOR.ID));
|
||||
assertEquals("Baeldung", result.getValue(0, AUTHOR.LAST_NAME));
|
||||
assertEquals("Building your REST API with Spring", result.getValue(0, BOOK.TITLE));
|
||||
}
|
||||
|
||||
@Test(expected = DataAccessException.class)
|
||||
public void givenInvalidData_whenUpdating_thenFail() {
|
||||
dsl.update(AUTHOR_BOOK)
|
||||
.set(AUTHOR_BOOK.AUTHOR_ID, 4)
|
||||
.set(AUTHOR_BOOK.BOOK_ID, 5)
|
||||
.execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidData_whenDeleting_thenSucceed() {
|
||||
dsl.delete(AUTHOR)
|
||||
.where(AUTHOR.ID.lt(3))
|
||||
.execute();
|
||||
|
||||
final Result<Record3<Integer, String, String>> result = dsl.select(AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
|
||||
.from(AUTHOR)
|
||||
.fetch();
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("Bryan", result.getValue(0, AUTHOR.FIRST_NAME));
|
||||
assertEquals("Basham", result.getValue(0, AUTHOR.LAST_NAME));
|
||||
}
|
||||
|
||||
@Test(expected = DataAccessException.class)
|
||||
public void givenInvalidData_whenDeleting_thenFail() {
|
||||
dsl.delete(BOOK)
|
||||
.where(BOOK.ID.equal(1))
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.jooq.springboot;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableTransactionManagement
|
||||
public class Application {
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.jooq.springboot;
|
||||
|
||||
import com.baeldung.jooq.introduction.ExceptionTranslator;
|
||||
import org.jooq.impl.DataSourceConnectionProvider;
|
||||
import org.jooq.impl.DefaultConfiguration;
|
||||
import org.jooq.impl.DefaultDSLContext;
|
||||
import org.jooq.impl.DefaultExecuteListenerProvider;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class InitialConfiguration {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Bean
|
||||
public DataSourceConnectionProvider connectionProvider() {
|
||||
return new DataSourceConnectionProvider(new TransactionAwareDataSourceProxy(dataSource));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DefaultDSLContext dsl() {
|
||||
return new DefaultDSLContext(configuration());
|
||||
}
|
||||
|
||||
public DefaultConfiguration configuration() {
|
||||
DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
|
||||
|
||||
jooqConfiguration.set(connectionProvider());
|
||||
jooqConfiguration.set(new DefaultExecuteListenerProvider(new ExceptionTranslator()));
|
||||
|
||||
return jooqConfiguration;
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
package com.baeldung.jooq.springboot;
|
||||
|
||||
import static com.baeldung.jooq.introduction.db.public_.tables.Author.AUTHOR;
|
||||
import static com.baeldung.jooq.introduction.db.public_.tables.AuthorBook.AUTHOR_BOOK;
|
||||
import static com.baeldung.jooq.introduction.db.public_.tables.Book.BOOK;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.Record3;
|
||||
import org.jooq.Result;
|
||||
import org.jooq.impl.DSL;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baeldung.jooq.introduction.PersistenceContextIntegrationTest;
|
||||
|
||||
@ContextConfiguration(classes = PersistenceContextIntegrationTest.class)
|
||||
@Transactional(transactionManager = "transactionManager")
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class SpringBootIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private DSLContext dsl;
|
||||
|
||||
@Test
|
||||
public void givenValidData_whenInserting_thenSucceed() {
|
||||
dsl.insertInto(AUTHOR)
|
||||
.set(AUTHOR.ID, 4)
|
||||
.set(AUTHOR.FIRST_NAME, "Herbert")
|
||||
.set(AUTHOR.LAST_NAME, "Schildt")
|
||||
.execute();
|
||||
|
||||
dsl.insertInto(BOOK)
|
||||
.set(BOOK.ID, 4)
|
||||
.set(BOOK.TITLE, "A Beginner's Guide")
|
||||
.execute();
|
||||
|
||||
dsl.insertInto(AUTHOR_BOOK)
|
||||
.set(AUTHOR_BOOK.AUTHOR_ID, 4)
|
||||
.set(AUTHOR_BOOK.BOOK_ID, 4)
|
||||
.execute();
|
||||
|
||||
final Result<Record3<Integer, String, Integer>> result = dsl.select(AUTHOR.ID, AUTHOR.LAST_NAME, DSL.count())
|
||||
.from(AUTHOR).join(AUTHOR_BOOK).on(AUTHOR.ID.equal(AUTHOR_BOOK.AUTHOR_ID))
|
||||
.join(BOOK).on(AUTHOR_BOOK.BOOK_ID.equal(BOOK.ID))
|
||||
.groupBy(AUTHOR.LAST_NAME)
|
||||
.orderBy(AUTHOR.LAST_NAME.desc())
|
||||
.fetch();
|
||||
|
||||
assertEquals(3, result.size());
|
||||
assertEquals("Sierra", result.getValue(0, AUTHOR.LAST_NAME));
|
||||
assertEquals(Integer.valueOf(2), result.getValue(0, DSL.count()));
|
||||
assertEquals("Bates", result.getValue(2, AUTHOR.LAST_NAME));
|
||||
assertEquals(Integer.valueOf(1), result.getValue(2, DSL.count()));
|
||||
}
|
||||
|
||||
@Test(expected = DataAccessException.class)
|
||||
public void givenInvalidData_whenInserting_thenFail() {
|
||||
dsl.insertInto(AUTHOR_BOOK)
|
||||
.set(AUTHOR_BOOK.AUTHOR_ID, 4)
|
||||
.set(AUTHOR_BOOK.BOOK_ID, 5)
|
||||
.execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidData_whenUpdating_thenSucceed() {
|
||||
dsl.update(AUTHOR)
|
||||
.set(AUTHOR.LAST_NAME, "Baeldung")
|
||||
.where(AUTHOR.ID.equal(3))
|
||||
.execute();
|
||||
|
||||
dsl.update(BOOK)
|
||||
.set(BOOK.TITLE, "Building your REST API with Spring")
|
||||
.where(BOOK.ID.equal(3))
|
||||
.execute();
|
||||
|
||||
dsl.insertInto(AUTHOR_BOOK)
|
||||
.set(AUTHOR_BOOK.AUTHOR_ID, 3)
|
||||
.set(AUTHOR_BOOK.BOOK_ID, 3)
|
||||
.execute();
|
||||
|
||||
final Result<Record3<Integer, String, String>> result = dsl.select(AUTHOR.ID, AUTHOR.LAST_NAME, BOOK.TITLE)
|
||||
.from(AUTHOR).join(AUTHOR_BOOK).on(AUTHOR.ID.equal(AUTHOR_BOOK.AUTHOR_ID))
|
||||
.join(BOOK).on(AUTHOR_BOOK.BOOK_ID.equal(BOOK.ID))
|
||||
.where(AUTHOR.ID.equal(3))
|
||||
.fetch();
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(Integer.valueOf(3), result.getValue(0, AUTHOR.ID));
|
||||
assertEquals("Baeldung", result.getValue(0, AUTHOR.LAST_NAME));
|
||||
assertEquals("Building your REST API with Spring", result.getValue(0, BOOK.TITLE));
|
||||
}
|
||||
|
||||
@Test(expected = DataAccessException.class)
|
||||
public void givenInvalidData_whenUpdating_thenFail() {
|
||||
dsl.update(AUTHOR_BOOK)
|
||||
.set(AUTHOR_BOOK.AUTHOR_ID, 4)
|
||||
.set(AUTHOR_BOOK.BOOK_ID, 5)
|
||||
.execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidData_whenDeleting_thenSucceed() {
|
||||
dsl.delete(AUTHOR)
|
||||
.where(AUTHOR.ID.lt(3))
|
||||
.execute();
|
||||
|
||||
final Result<Record3<Integer, String, String>> result = dsl.select(AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
|
||||
.from(AUTHOR).fetch();
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("Bryan", result.getValue(0, AUTHOR.FIRST_NAME));
|
||||
assertEquals("Basham", result.getValue(0, AUTHOR.LAST_NAME));
|
||||
}
|
||||
|
||||
@Test(expected = DataAccessException.class)
|
||||
public void givenInvalidData_whenDeleting_thenFail() {
|
||||
dsl.delete(BOOK)
|
||||
.where(BOOK.ID.equal(1))
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
spring.datasource.url=jdbc:h2:~/jooq;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
Reference in New Issue
Block a user