Skip to content

Commit 34abb0e

Browse files
author
Grzegorz Piwowarek
committed
Refactor jOOQ-Spring
1 parent eadbde5 commit 34abb0e

8 files changed

Lines changed: 144 additions & 126 deletions

File tree

‎jooq-spring/.classpath‎

Lines changed: 0 additions & 31 deletions
This file was deleted.

‎jooq-spring/.project‎

Lines changed: 0 additions & 29 deletions
This file was deleted.

‎jooq-spring/src/test/java/com/baeldung/jooq/introduction/ExceptionTranslator.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import org.springframework.jdbc.support.SQLExceptionTranslator;
99

1010
public class ExceptionTranslator extends DefaultExecuteListener {
11-
private static final long serialVersionUID = 649359748808106775L;
1211

1312
@Override
1413
public void exception(ExecuteContext context) {
1514
SQLDialect dialect = context.configuration().dialect();
1615
SQLExceptionTranslator translator = new SQLErrorCodeSQLExceptionTranslator(dialect.name());
16+
1717
context.exception(translator.translate("Access database using jOOQ", context.sql(), context.sqlException()));
1818
}
1919
}

‎jooq-spring/src/test/java/com/baeldung/jooq/introduction/PersistenceContext.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.jooq.introduction;
22

3-
import javax.sql.DataSource;
43
import org.h2.jdbcx.JdbcDataSource;
54
import org.jooq.SQLDialect;
65
import org.jooq.impl.DataSourceConnectionProvider;
@@ -17,11 +16,14 @@
1716
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
1817
import org.springframework.transaction.annotation.EnableTransactionManagement;
1918

19+
import javax.sql.DataSource;
20+
2021
@Configuration
2122
@ComponentScan({ "com.baeldung.jooq.introduction.db.public_.tables" })
2223
@EnableTransactionManagement
2324
@PropertySource("classpath:intro_config.properties")
2425
public class PersistenceContext {
26+
2527
@Autowired
2628
private Environment environment;
2729

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.baeldung.jooq.introduction;
22

3-
import com.baeldung.jooq.introduction.db.public_.tables.Author;
4-
import com.baeldung.jooq.introduction.db.public_.tables.AuthorBook;
5-
import com.baeldung.jooq.introduction.db.public_.tables.Book;
63
import org.jooq.DSLContext;
74
import org.jooq.Record3;
85
import org.jooq.Result;
@@ -15,6 +12,9 @@
1512
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1613
import org.springframework.transaction.annotation.Transactional;
1714

15+
import static com.baeldung.jooq.introduction.db.public_.tables.Author.AUTHOR;
16+
import static com.baeldung.jooq.introduction.db.public_.tables.AuthorBook.AUTHOR_BOOK;
17+
import static com.baeldung.jooq.introduction.db.public_.tables.Book.BOOK;
1818
import static org.junit.Assert.assertEquals;
1919

2020
@ContextConfiguration(classes = PersistenceContext.class)
@@ -25,61 +25,98 @@ public class QueryTest {
2525
@Autowired
2626
private DSLContext dsl;
2727

28-
Author author = Author.AUTHOR;
29-
Book book = Book.BOOK;
30-
AuthorBook authorBook = AuthorBook.AUTHOR_BOOK;
31-
3228
@Test
3329
public void givenValidData_whenInserting_thenSucceed() {
34-
dsl.insertInto(author).set(author.ID, 4).set(author.FIRST_NAME, "Herbert").set(author.LAST_NAME, "Schildt").execute();
35-
dsl.insertInto(book).set(book.ID, 4).set(book.TITLE, "A Beginner's Guide").execute();
36-
dsl.insertInto(authorBook).set(authorBook.AUTHOR_ID, 4).set(authorBook.BOOK_ID, 4).execute();
37-
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))
38-
.groupBy(author.LAST_NAME).fetch();
30+
dsl.insertInto(AUTHOR)
31+
.set(AUTHOR.ID, 4)
32+
.set(AUTHOR.FIRST_NAME, "Herbert")
33+
.set(AUTHOR.LAST_NAME, "Schildt")
34+
.execute();
35+
36+
dsl.insertInto(BOOK)
37+
.set(BOOK.ID, 4)
38+
.set(BOOK.TITLE, "A Beginner's Guide")
39+
.execute();
40+
41+
dsl.insertInto(AUTHOR_BOOK)
42+
.set(AUTHOR_BOOK.AUTHOR_ID, 4)
43+
.set(AUTHOR_BOOK.BOOK_ID, 4)
44+
.execute();
45+
46+
final Result<Record3<Integer, String, Integer>> result = dsl.select(AUTHOR.ID, AUTHOR.LAST_NAME, DSL.count())
47+
.from(AUTHOR)
48+
.join(AUTHOR_BOOK).on(AUTHOR.ID.equal(AUTHOR_BOOK.AUTHOR_ID))
49+
.join(BOOK).on(AUTHOR_BOOK.BOOK_ID.equal(BOOK.ID))
50+
.groupBy(AUTHOR.LAST_NAME).fetch();
3951

4052
assertEquals(3, result.size());
41-
assertEquals("Sierra", result.getValue(0, author.LAST_NAME));
53+
assertEquals("Sierra", result.getValue(0, AUTHOR.LAST_NAME));
4254
assertEquals(Integer.valueOf(2), result.getValue(0, DSL.count()));
43-
assertEquals("Schildt", result.getValue(2, author.LAST_NAME));
55+
assertEquals("Schildt", result.getValue(2, AUTHOR.LAST_NAME));
4456
assertEquals(Integer.valueOf(1), result.getValue(2, DSL.count()));
4557
}
4658

4759
@Test(expected = DataAccessException.class)
4860
public void givenInvalidData_whenInserting_thenFail() {
49-
dsl.insertInto(authorBook).set(authorBook.AUTHOR_ID, 4).set(authorBook.BOOK_ID, 5).execute();
61+
dsl.insertInto(AUTHOR_BOOK).set(AUTHOR_BOOK.AUTHOR_ID, 4).set(AUTHOR_BOOK.BOOK_ID, 5).execute();
5062
}
5163

5264
@Test
5365
public void givenValidData_whenUpdating_thenSucceed() {
54-
dsl.update(author).set(author.LAST_NAME, "Baeldung").where(author.ID.equal(3)).execute();
55-
dsl.update(book).set(book.TITLE, "Building your REST API with Spring").where(book.ID.equal(3)).execute();
56-
dsl.insertInto(authorBook).set(authorBook.AUTHOR_ID, 3).set(authorBook.BOOK_ID, 3).execute();
57-
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))
66+
dsl.update(AUTHOR)
67+
.set(AUTHOR.LAST_NAME, "Baeldung")
68+
.where(AUTHOR.ID.equal(3))
69+
.execute();
70+
71+
dsl.update(BOOK)
72+
.set(BOOK.TITLE, "Building your REST API with Spring")
73+
.where(BOOK.ID.equal(3)).execute();
74+
75+
dsl.insertInto(AUTHOR_BOOK)
76+
.set(AUTHOR_BOOK.AUTHOR_ID, 3)
77+
.set(AUTHOR_BOOK.BOOK_ID, 3)
78+
.execute();
79+
80+
final Result<Record3<Integer, String, String>> result = dsl.select(AUTHOR.ID, AUTHOR.LAST_NAME, BOOK.TITLE)
81+
.from(AUTHOR)
82+
.join(AUTHOR_BOOK).on(AUTHOR.ID.equal(AUTHOR_BOOK.AUTHOR_ID))
83+
.join(BOOK).on(AUTHOR_BOOK.BOOK_ID.equal(BOOK.ID))
84+
.where(AUTHOR.ID.equal(3))
5885
.fetch();
5986

6087
assertEquals(1, result.size());
61-
assertEquals(Integer.valueOf(3), result.getValue(0, author.ID));
62-
assertEquals("Baeldung", result.getValue(0, author.LAST_NAME));
63-
assertEquals("Building your REST API with Spring", result.getValue(0, book.TITLE));
88+
assertEquals(Integer.valueOf(3), result.getValue(0, AUTHOR.ID));
89+
assertEquals("Baeldung", result.getValue(0, AUTHOR.LAST_NAME));
90+
assertEquals("Building your REST API with Spring", result.getValue(0, BOOK.TITLE));
6491
}
6592

6693
@Test(expected = DataAccessException.class)
6794
public void givenInvalidData_whenUpdating_thenFail() {
68-
dsl.update(authorBook).set(authorBook.AUTHOR_ID, 4).set(authorBook.BOOK_ID, 5).execute();
95+
dsl.update(AUTHOR_BOOK)
96+
.set(AUTHOR_BOOK.AUTHOR_ID, 4)
97+
.set(AUTHOR_BOOK.BOOK_ID, 5)
98+
.execute();
6999
}
70100

71101
@Test
72102
public void givenValidData_whenDeleting_thenSucceed() {
73-
dsl.delete(author).where(author.ID.lt(3)).execute();
74-
Result<Record3<Integer, String, String>> result = dsl.select(author.ID, author.FIRST_NAME, author.LAST_NAME).from(author).fetch();
103+
dsl.delete(AUTHOR)
104+
.where(AUTHOR.ID.lt(3))
105+
.execute();
106+
107+
final Result<Record3<Integer, String, String>> result = dsl.select(AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
108+
.from(AUTHOR)
109+
.fetch();
75110

76111
assertEquals(1, result.size());
77-
assertEquals("Bryan", result.getValue(0, author.FIRST_NAME));
78-
assertEquals("Basham", result.getValue(0, author.LAST_NAME));
112+
assertEquals("Bryan", result.getValue(0, AUTHOR.FIRST_NAME));
113+
assertEquals("Basham", result.getValue(0, AUTHOR.LAST_NAME));
79114
}
80115

81116
@Test(expected = DataAccessException.class)
82117
public void givenInvalidData_whenDeleting_thenFail() {
83-
dsl.delete(book).where(book.ID.equal(1)).execute();
118+
dsl.delete(BOOK)
119+
.where(BOOK.ID.equal(1))
120+
.execute();
84121
}
85122
}

‎jooq-spring/src/test/java/com/baeldung/jooq/springboot/Application.java‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package com.baeldung.jooq.springboot;
22

3-
import javax.sql.DataSource;
4-
3+
import com.baeldung.jooq.introduction.ExceptionTranslator;
54
import org.jooq.impl.DataSourceConnectionProvider;
65
import org.jooq.impl.DefaultConfiguration;
76
import org.jooq.impl.DefaultDSLContext;
87
import org.jooq.impl.DefaultExecuteListenerProvider;
9-
108
import org.springframework.beans.factory.annotation.Autowired;
119
import org.springframework.boot.autoconfigure.SpringBootApplication;
1210
import org.springframework.context.annotation.Bean;
@@ -16,14 +14,14 @@
1614
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
1715
import org.springframework.transaction.annotation.EnableTransactionManagement;
1816

19-
import com.baeldung.jooq.introduction.ExceptionTranslator;
17+
import javax.sql.DataSource;
2018

2119
@SpringBootApplication
2220
@EnableTransactionManagement
2321
public class Application {
22+
2423
@Autowired
2524
private Environment environment;
26-
private DataSource dataSource;
2725

2826
public DataSource dataSource() {
2927
DriverManagerDataSource dataSource = new DriverManagerDataSource();
@@ -41,7 +39,7 @@ public TransactionAwareDataSourceProxy transactionAwareDataSource() {
4139
}
4240

4341
public DataSourceTransactionManager transactionManager() {
44-
return new DataSourceTransactionManager(dataSource);
42+
return new DataSourceTransactionManager(dataSource());
4543
}
4644

4745
@Bean
@@ -60,6 +58,7 @@ public DefaultDSLContext dsl() {
6058

6159
public DefaultConfiguration configuration() {
6260
DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
61+
6362
jooqConfiguration.set(connectionProvider());
6463
jooqConfiguration.set(new DefaultExecuteListenerProvider(exceptionTransformer()));
6564

0 commit comments

Comments
 (0)