Refactor jOOQ-Spring

This commit is contained in:
Grzegorz Piwowarek 2016-06-07 18:05:50 +02:00
parent eadbde5c2d
commit 34abb0eab7
8 changed files with 144 additions and 126 deletions

View File

@ -1,31 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -1,29 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>jooq-spring</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@ -8,12 +8,12 @@ import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
import org.springframework.jdbc.support.SQLExceptionTranslator;
public class ExceptionTranslator extends DefaultExecuteListener {
private static final long serialVersionUID = 649359748808106775L;
@Override
public void exception(ExecuteContext context) {
SQLDialect dialect = context.configuration().dialect();
SQLExceptionTranslator translator = new SQLErrorCodeSQLExceptionTranslator(dialect.name());
context.exception(translator.translate("Access database using jOOQ", context.sql(), context.sqlException()));
}
}

View File

@ -1,6 +1,5 @@
package com.baeldung.jooq.introduction;
import javax.sql.DataSource;
import org.h2.jdbcx.JdbcDataSource;
import org.jooq.SQLDialect;
import org.jooq.impl.DataSourceConnectionProvider;
@ -17,11 +16,14 @@ 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 PersistenceContext {
@Autowired
private Environment environment;

View File

@ -1,8 +1,5 @@
package com.baeldung.jooq.introduction;
import com.baeldung.jooq.introduction.db.public_.tables.Author;
import com.baeldung.jooq.introduction.db.public_.tables.AuthorBook;
import com.baeldung.jooq.introduction.db.public_.tables.Book;
import org.jooq.DSLContext;
import org.jooq.Record3;
import org.jooq.Result;
@ -15,6 +12,9 @@ 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 = PersistenceContext.class)
@ -25,61 +25,98 @@ public class QueryTest {
@Autowired
private DSLContext dsl;
Author author = Author.AUTHOR;
Book book = Book.BOOK;
AuthorBook authorBook = AuthorBook.AUTHOR_BOOK;
@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(authorBook).set(authorBook.AUTHOR_ID, 4).set(authorBook.BOOK_ID, 4).execute();
Result<Record3<Integer, String, Integer>> result = dsl.select(author.ID, author.LAST_NAME, DSL.count()).from(author).join(authorBook).on(author.ID.equal(authorBook.AUTHOR_ID)).join(book).on(authorBook.BOOK_ID.equal(book.ID))
.groupBy(author.LAST_NAME).fetch();
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).fetch();
assertEquals(3, result.size());
assertEquals("Sierra", result.getValue(0, author.LAST_NAME));
assertEquals("Sierra", result.getValue(0, AUTHOR.LAST_NAME));
assertEquals(Integer.valueOf(2), result.getValue(0, DSL.count()));
assertEquals("Schildt", result.getValue(2, author.LAST_NAME));
assertEquals("Schildt", 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(authorBook).set(authorBook.AUTHOR_ID, 4).set(authorBook.BOOK_ID, 5).execute();
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(authorBook).set(authorBook.AUTHOR_ID, 3).set(authorBook.BOOK_ID, 3).execute();
Result<Record3<Integer, String, String>> result = dsl.select(author.ID, author.LAST_NAME, book.TITLE).from(author).join(authorBook).on(author.ID.equal(authorBook.AUTHOR_ID)).join(book).on(authorBook.BOOK_ID.equal(book.ID)).where(author.ID.equal(3))
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));
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(authorBook).set(authorBook.AUTHOR_ID, 4).set(authorBook.BOOK_ID, 5).execute();
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();
Result<Record3<Integer, String, String>> result = dsl.select(author.ID, author.FIRST_NAME, author.LAST_NAME).from(author).fetch();
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));
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();
dsl.delete(BOOK)
.where(BOOK.ID.equal(1))
.execute();
}
}

View File

@ -1,12 +1,10 @@
package com.baeldung.jooq.springboot;
import javax.sql.DataSource;
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.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@ -16,14 +14,14 @@ import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.baeldung.jooq.introduction.ExceptionTranslator;
import javax.sql.DataSource;
@SpringBootApplication
@EnableTransactionManagement
public class Application {
@Autowired
private Environment environment;
private DataSource dataSource;
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
@ -41,7 +39,7 @@ public class Application {
}
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource);
return new DataSourceTransactionManager(dataSource());
}
@Bean
@ -60,6 +58,7 @@ public class Application {
public DefaultConfiguration configuration() {
DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
jooqConfiguration.set(connectionProvider());
jooqConfiguration.set(new DefaultExecuteListenerProvider(exceptionTransformer()));

View File

@ -1,7 +1,5 @@
package com.baeldung.jooq.springboot;
import static org.junit.Assert.*;
import org.jooq.DSLContext;
import org.jooq.Record3;
import org.jooq.Result;
@ -14,72 +12,113 @@ import org.springframework.dao.DataAccessException;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.baeldung.jooq.introduction.db.public_.tables.Author;
import com.baeldung.jooq.introduction.db.public_.tables.AuthorBook;
import com.baeldung.jooq.introduction.db.public_.tables.Book;
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;
@SpringApplicationConfiguration(Application.class)
@Transactional("transactionManager")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringBootTest {
@Autowired
private DSLContext dsl;
Author author = Author.AUTHOR;
Book book = Book.BOOK;
AuthorBook authorBook = AuthorBook.AUTHOR_BOOK;
@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(authorBook).set(authorBook.AUTHOR_ID, 4).set(authorBook.BOOK_ID, 4).execute();
Result<Record3<Integer, String, Integer>> result = dsl.select(author.ID, author.LAST_NAME, DSL.count()).from(author).join(authorBook).on(author.ID.equal(authorBook.AUTHOR_ID)).join(book).on(authorBook.BOOK_ID.equal(book.ID)).groupBy(author.LAST_NAME)
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)
.fetch();
assertEquals(3, result.size());
assertEquals("Sierra", result.getValue(0, author.LAST_NAME));
assertEquals("Sierra", result.getValue(0, AUTHOR.LAST_NAME));
assertEquals(Integer.valueOf(2), result.getValue(0, DSL.count()));
assertEquals("Schildt", result.getValue(2, author.LAST_NAME));
assertEquals("Schildt", 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(authorBook).set(authorBook.AUTHOR_ID, 4).set(authorBook.BOOK_ID, 5).execute();
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(authorBook).set(authorBook.AUTHOR_ID, 3).set(authorBook.BOOK_ID, 3).execute();
Result<Record3<Integer, String, String>> result = dsl.select(author.ID, author.LAST_NAME, book.TITLE).from(author).join(authorBook).on(author.ID.equal(authorBook.AUTHOR_ID)).join(book).on(authorBook.BOOK_ID.equal(book.ID)).where(author.ID.equal(3))
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));
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(authorBook).set(authorBook.AUTHOR_ID, 4).set(authorBook.BOOK_ID, 5).execute();
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();
Result<Record3<Integer, String, String>> result = dsl.select(author.ID, author.FIRST_NAME, author.LAST_NAME).from(author).fetch();
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));
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();
dsl.delete(BOOK)
.where(BOOK.ID.equal(1))
.execute();
}
}

View File

@ -20,6 +20,7 @@
<module>httpclient</module>
<module>jackson</module>
<module>javaxval</module>
<module>jooq-spring</module>
<module>json-path</module>
<module>mockito</module>
<!-- <module>jpa-storedprocedure</module> -->