JAVA-3542: Moved spring-jooq inside persistence-modules
This commit is contained in:
@@ -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