diff --git a/jooq-spring/.classpath b/jooq-spring/.classpath
deleted file mode 100644
index 9ae7bca0fc..0000000000
--- a/jooq-spring/.classpath
+++ /dev/null
@@ -1,31 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/jooq-spring/.project b/jooq-spring/.project
deleted file mode 100644
index a291146b79..0000000000
--- a/jooq-spring/.project
+++ /dev/null
@@ -1,29 +0,0 @@
-
-
- jooq-spring
-
-
-
-
-
- org.eclipse.jdt.core.javabuilder
-
-
-
-
- org.springframework.ide.eclipse.core.springbuilder
-
-
-
-
- org.eclipse.m2e.core.maven2Builder
-
-
-
-
-
- org.springframework.ide.eclipse.core.springnature
- org.eclipse.jdt.core.javanature
- org.eclipse.m2e.core.maven2Nature
-
-
diff --git a/jooq-spring/src/test/java/com/baeldung/jooq/introduction/ExceptionTranslator.java b/jooq-spring/src/test/java/com/baeldung/jooq/introduction/ExceptionTranslator.java
index 7bee21f077..8312f20c05 100644
--- a/jooq-spring/src/test/java/com/baeldung/jooq/introduction/ExceptionTranslator.java
+++ b/jooq-spring/src/test/java/com/baeldung/jooq/introduction/ExceptionTranslator.java
@@ -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()));
}
}
\ No newline at end of file
diff --git a/jooq-spring/src/test/java/com/baeldung/jooq/introduction/PersistenceContext.java b/jooq-spring/src/test/java/com/baeldung/jooq/introduction/PersistenceContext.java
index ee34c00679..df628f9f78 100644
--- a/jooq-spring/src/test/java/com/baeldung/jooq/introduction/PersistenceContext.java
+++ b/jooq-spring/src/test/java/com/baeldung/jooq/introduction/PersistenceContext.java
@@ -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;
diff --git a/jooq-spring/src/test/java/com/baeldung/jooq/introduction/QueryTest.java b/jooq-spring/src/test/java/com/baeldung/jooq/introduction/QueryTest.java
index bc12dff5a0..68f975dd6d 100644
--- a/jooq-spring/src/test/java/com/baeldung/jooq/introduction/QueryTest.java
+++ b/jooq-spring/src/test/java/com/baeldung/jooq/introduction/QueryTest.java
@@ -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> 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> 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> 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> 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> 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> 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();
}
}
\ No newline at end of file
diff --git a/jooq-spring/src/test/java/com/baeldung/jooq/springboot/Application.java b/jooq-spring/src/test/java/com/baeldung/jooq/springboot/Application.java
index a52f0a0ef3..844a2989a5 100644
--- a/jooq-spring/src/test/java/com/baeldung/jooq/springboot/Application.java
+++ b/jooq-spring/src/test/java/com/baeldung/jooq/springboot/Application.java
@@ -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()));
diff --git a/jooq-spring/src/test/java/com/baeldung/jooq/springboot/SpringBootTest.java b/jooq-spring/src/test/java/com/baeldung/jooq/springboot/SpringBootTest.java
index bf5cc250a8..f9427f30fb 100644
--- a/jooq-spring/src/test/java/com/baeldung/jooq/springboot/SpringBootTest.java
+++ b/jooq-spring/src/test/java/com/baeldung/jooq/springboot/SpringBootTest.java
@@ -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> 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> 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> 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> 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> 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> 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();
}
}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index d6a1d7c034..7a51347af6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,6 +20,7 @@
httpclient
jackson
javaxval
+ jooq-spring
json-path
mockito