diff --git a/jOOQ-examples/jOOQ-spring-boot-example/README.md b/jOOQ-examples/jOOQ-spring-boot-example/README.md new file mode 100644 index 0000000000..813f616769 --- /dev/null +++ b/jOOQ-examples/jOOQ-spring-boot-example/README.md @@ -0,0 +1,19 @@ +Thanks for downloading jOOQ. +Please visit http://www.jooq.org for more information. + +This example was inspired by Petri Kainulainen's excellent blog post: +http://www.petrikainulainen.net/programming/jooq/using-jooq-with-spring-configuration/ + +To install and run this example, please check out the complete jOOQ repository first, and use Maven to install the latest SNAPSHOT version of jOOQ: + +``` +$ pwd +/path/to/checkout/dir +$ ls +jOOQ jOOQ-meta jOOQ-codegen ... +$ mvn clean install +... +$ cd jOOQ-examples/jOOQ-spring-boot-example +... +$ mvn clean install +``` diff --git a/jOOQ-examples/jOOQ-spring-boot-example/pom.xml b/jOOQ-examples/jOOQ-spring-boot-example/pom.xml new file mode 100644 index 0000000000..d8419b8ff6 --- /dev/null +++ b/jOOQ-examples/jOOQ-spring-boot-example/pom.xml @@ -0,0 +1,171 @@ + + + 4.0.0 + + org.jooq + jooq-spring-boot-example + 0.0.1-SNAPSHOT + jar + + jOOQ Spring Boot Example + + + org.springframework.boot + spring-boot-starter-parent + 1.1.7.RELEASE + + + + + org.springframework.boot + spring-boot-starter-jdbc + + + + org.jooq + jooq + ${jooq.version} + + + + com.h2database + h2 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + UTF-8 + org.jooq.example.spring.Application + 3.4.2 + 1.8 + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.codehaus.mojo + properties-maven-plugin + 1.0-alpha-2 + + + initialize + + read-project-properties + + + + src/main/resources/application.properties + + + + + + + + + + org.codehaus.mojo + sql-maven-plugin + 1.5 + + + ${maven.test.skip} + always + + + + + create-database-h2 + generate-sources + + execute + + + ${spring.datasource.driverClassName} + ${spring.datasource.url} + ${spring.datasource.username} + ${spring.datasource.password} + + true + + src/main/resources/schema-h2.sql + src/main/resources/data-h2.sql + + + + + + + + com.h2database + h2 + ${h2.version} + + + + + + + org.jooq + jooq-codegen-maven + ${jooq.version} + + + + generate-h2 + generate-sources + + generate + + + + ${spring.datasource.driverClassName} + ${spring.datasource.url} + ${spring.datasource.username} + ${spring.datasource.password} + + + org.jooq.util.DefaultGenerator + + org.jooq.util.h2.H2Database + .* + + true + PUBLIC + + + false + true + true + + + org.jooq.example.db.h2 + target/generated-sources/jooq-h2 + + + + + + + + com.h2database + h2 + ${h2.version} + + + + + + diff --git a/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/Application.java b/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/Application.java new file mode 100644 index 0000000000..7ac82428f9 --- /dev/null +++ b/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/Application.java @@ -0,0 +1,74 @@ +package org.jooq.example.spring; + +import javax.sql.DataSource; + +import org.jooq.ConnectionProvider; +import org.jooq.DSLContext; +import org.jooq.ExecuteListenerProvider; +import org.jooq.SQLDialect; +import org.jooq.TransactionProvider; +import org.jooq.impl.DataSourceConnectionProvider; +import org.jooq.impl.DefaultConfiguration; +import org.jooq.impl.DefaultDSLContext; +import org.jooq.impl.DefaultExecuteListenerProvider; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; + +/** + * @author Thomas Darimont + */ +@Configuration +@ComponentScan +@EnableAutoConfiguration +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + @Bean + public DataSourceTransactionManager transactionManager(DataSource dataSource) { + return new DataSourceTransactionManager(dataSource); + } + + @Bean + public DSLContext dsl(org.jooq.Configuration config) { + return new DefaultDSLContext(config); + } + + @Bean + public ConnectionProvider connectionProvider(DataSource dataSource) { + return new DataSourceConnectionProvider(new TransactionAwareDataSourceProxy(dataSource)); + } + + @Bean + public TransactionProvider transactionProvider() { + return new SpringTransactionProvider(); + } + + @Bean + public ExceptionTranslator exceptionTranslator() { + return new ExceptionTranslator(); + } + + @Bean + public ExecuteListenerProvider executeListenerProvider(ExceptionTranslator exceptionTranslator) { + return new DefaultExecuteListenerProvider(exceptionTranslator); + } + + @Bean + public org.jooq.Configuration jooqConfig(ConnectionProvider connectionProvider, + TransactionProvider transactionProvider, ExecuteListenerProvider executeListenerProvider) { + + return new DefaultConfiguration() // + .derive(connectionProvider) // + .derive(transactionProvider) // + .derive(executeListenerProvider) // + .derive(SQLDialect.H2); + } +} diff --git a/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/BookService.java b/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/BookService.java new file mode 100644 index 0000000000..084846347e --- /dev/null +++ b/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/BookService.java @@ -0,0 +1,60 @@ +/** + * Copyright (c) 2009-2013, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * This work is dual-licensed + * - under the Apache Software License 2.0 (the "ASL") + * - under the jOOQ License and Maintenance Agreement (the "jOOQ License") + * ============================================================================= + * You may choose which license applies to you: + * + * - If you're using this work with Open Source databases, you may choose + * either ASL or jOOQ License. + * - If you're using this work with at least one commercial database, you must + * choose jOOQ License + * + * For more information, please visit http://www.jooq.org/licenses + * + * Apache Software License 2.0: + * ----------------------------------------------------------------------------- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * jOOQ License and Maintenance Agreement: + * ----------------------------------------------------------------------------- + * Data Geekery grants the Customer the non-exclusive, timely limited and + * non-transferable license to install and use the Software under the terms of + * the jOOQ License and Maintenance Agreement. + * + * This library is distributed with a LIMITED WARRANTY. See the jOOQ License + * and Maintenance Agreement for more details: http://www.jooq.org/licensing + */ +package org.jooq.example.spring; + +import org.springframework.transaction.annotation.Transactional; + +/** + * This Book Service (or DAO or Repository) is used by this example to interact with the library's T_BOOK table. + * + * @author Lukas Eder + */ +public interface BookService { + + /** + * Create a new book. + *

+ * The implementation of this method has a bug, which causes this method to fail and roll back the transaction. + */ + @Transactional + void create(int id, int authorId, String title); + +} diff --git a/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/DefaultBookService.java b/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/DefaultBookService.java new file mode 100644 index 0000000000..d624e924a5 --- /dev/null +++ b/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/DefaultBookService.java @@ -0,0 +1,27 @@ +package org.jooq.example.spring; + +import static org.jooq.example.db.h2.Tables.BOOK; + +import org.jooq.DSLContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * @author Lukas Eder + */ +@Service +public class DefaultBookService implements BookService { + + @Autowired DSLContext dsl; + + @Override + @Transactional + public void create(int id, int authorId, String title) { + + // This method has a "bug". It creates the same book twice. The second insert + // should lead to a constraint violation, which should roll back the whole transaction + for (int i = 0; i < 2; i++) + dsl.insertInto(BOOK).set(BOOK.ID, id).set(BOOK.AUTHOR_ID, authorId).set(BOOK.TITLE, title).execute(); + } +} diff --git a/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/ExceptionTranslator.java b/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/ExceptionTranslator.java new file mode 100644 index 0000000000..094bfe7d07 --- /dev/null +++ b/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/ExceptionTranslator.java @@ -0,0 +1,36 @@ +package org.jooq.example.spring; + +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; +import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; + +/** + * This class transforms SQLException into a Spring specific DataAccessException. The idea behind this is borrowed from + * Adam Zell's Gist + * + * @author Petri Kainulainen + * @author Adam Zell + * @author Lukas Eder + * @see http://www.petrikainulainen.net/programming/jooq/using-jooq-with-spring-configuration/ + * @see https://gist.github.com/azell/5655888 + */ +public class ExceptionTranslator extends DefaultExecuteListener { + + /** + * Generated UID + */ + private static final long serialVersionUID = -2450323227461061152L; + + @Override + public void exception(ExecuteContext ctx) { + SQLDialect dialect = ctx.configuration().dialect(); + SQLExceptionTranslator translator = (dialect != null) ? new SQLErrorCodeSQLExceptionTranslator(dialect.name()) + : new SQLStateSQLExceptionTranslator(); + + ctx.exception(translator.translate("jOOQ", ctx.sql(), ctx.sqlException())); + } +} diff --git a/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/SpringTransaction.java b/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/SpringTransaction.java new file mode 100644 index 0000000000..1ecdd13dd3 --- /dev/null +++ b/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/SpringTransaction.java @@ -0,0 +1,52 @@ +/** + * Copyright (c) 2009-2013, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * This work is dual-licensed + * - under the Apache Software License 2.0 (the "ASL") + * - under the jOOQ License and Maintenance Agreement (the "jOOQ License") + * ============================================================================= + * You may choose which license applies to you: + * + * - If you're using this work with Open Source databases, you may choose + * either ASL or jOOQ License. + * - If you're using this work with at least one commercial database, you must + * choose jOOQ License + * + * For more information, please visit http://www.jooq.org/licenses + * + * Apache Software License 2.0: + * ----------------------------------------------------------------------------- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * jOOQ License and Maintenance Agreement: + * ----------------------------------------------------------------------------- + * Data Geekery grants the Customer the non-exclusive, timely limited and + * non-transferable license to install and use the Software under the terms of + * the jOOQ License and Maintenance Agreement. + * + * This library is distributed with a LIMITED WARRANTY. See the jOOQ License + * and Maintenance Agreement for more details: http://www.jooq.org/licensing + */ +package org.jooq.example.spring; + +import org.jooq.Transaction; +import org.springframework.transaction.TransactionStatus; + +class SpringTransaction implements Transaction { + final TransactionStatus tx; + + SpringTransaction(TransactionStatus tx) { + this.tx = tx; + } +} diff --git a/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/SpringTransactionProvider.java b/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/SpringTransactionProvider.java new file mode 100644 index 0000000000..44d8b448f0 --- /dev/null +++ b/jOOQ-examples/jOOQ-spring-boot-example/src/main/java/org/jooq/example/spring/SpringTransactionProvider.java @@ -0,0 +1,88 @@ +/** + * Copyright (c) 2009-2013, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * This work is dual-licensed + * - under the Apache Software License 2.0 (the "ASL") + * - under the jOOQ License and Maintenance Agreement (the "jOOQ License") + * ============================================================================= + * You may choose which license applies to you: + * + * - If you're using this work with Open Source databases, you may choose + * either ASL or jOOQ License. + * - If you're using this work with at least one commercial database, you must + * choose jOOQ License + * + * For more information, please visit http://www.jooq.org/licenses + * + * Apache Software License 2.0: + * ----------------------------------------------------------------------------- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * jOOQ License and Maintenance Agreement: + * ----------------------------------------------------------------------------- + * Data Geekery grants the Customer the non-exclusive, timely limited and + * non-transferable license to install and use the Software under the terms of + * the jOOQ License and Maintenance Agreement. + * + * This library is distributed with a LIMITED WARRANTY. See the jOOQ License + * and Maintenance Agreement for more details: http://www.jooq.org/licensing + */ +package org.jooq.example.spring; + +import static org.springframework.transaction.TransactionDefinition.PROPAGATION_NESTED; + +import org.jooq.TransactionContext; +import org.jooq.TransactionProvider; +import org.jooq.tools.JooqLogger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.DefaultTransactionDefinition; + +/** + * An example TransactionProvider implementing the {@link TransactionProvider} contract for use with + * Spring. + * + * @author Lukas Eder + */ +public class SpringTransactionProvider implements TransactionProvider { + + private static final JooqLogger log = JooqLogger.getLogger(SpringTransactionProvider.class); + + @Autowired DataSourceTransactionManager txMgr; + + @Override + public void begin(TransactionContext ctx) { + log.info("Begin transaction"); + + // This TransactionProvider behaves like jOOQ's DefaultTransactionProvider, + // which supports nested transactions using Savepoints + TransactionStatus tx = txMgr.getTransaction(new DefaultTransactionDefinition(PROPAGATION_NESTED)); + ctx.transaction(new SpringTransaction(tx)); + } + + @Override + public void commit(TransactionContext ctx) { + log.info("commit transaction"); + + txMgr.commit(((SpringTransaction) ctx.transaction()).tx); + } + + @Override + public void rollback(TransactionContext ctx) { + log.info("rollback transaction"); + + txMgr.rollback(((SpringTransaction) ctx.transaction()).tx); + } +} diff --git a/jOOQ-examples/jOOQ-spring-boot-example/src/main/resources/application.properties b/jOOQ-examples/jOOQ-spring-boot-example/src/main/resources/application.properties new file mode 100644 index 0000000000..e99917644e --- /dev/null +++ b/jOOQ-examples/jOOQ-spring-boot-example/src/main/resources/application.properties @@ -0,0 +1,12 @@ +#Database Configuration +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.url=jdbc:h2:~/jooq-spring-boot-example +spring.datasource.username=sa +spring.datasource.password= + +#jOOQ Configuration +jooq.sql.dialect=H2 + + +spring.datasource.initialize=false +spring.datasource.continueOnError=true \ No newline at end of file diff --git a/jOOQ-examples/jOOQ-spring-boot-example/src/main/resources/data-h2.sql b/jOOQ-examples/jOOQ-spring-boot-example/src/main/resources/data-h2.sql new file mode 100644 index 0000000000..0dc4b58a4a --- /dev/null +++ b/jOOQ-examples/jOOQ-spring-boot-example/src/main/resources/data-h2.sql @@ -0,0 +1,20 @@ +INSERT INTO author VALUES (next value for s_author_id, 'George', 'Orwell', '1903-06-25', 1903, null); +INSERT INTO author VALUES (next value for s_author_id, 'Paulo', 'Coelho', '1947-08-24', 1947, null); + +INSERT INTO book VALUES (1, 1, null, null, '1984', 1948, 1, 'To know and not to know, to be conscious of complete truthfulness while telling carefully constructed lies, to hold simultaneously two opinions which cancelled out, knowing them to be contradictory and believing in both of them, to use logic against logic, to repudiate morality while laying claim to it, to believe that democracy was impossible and that the Party was the guardian of democracy, to forget, whatever it was necessary to forget, then to draw it back into memory again at the moment when it was needed, and then promptly to forget it again, and above all, to apply the same process to the process itself -- that was the ultimate subtlety; consciously to induce unconsciousness, and then, once again, to become unconscious of the act of hypnosis you had just performed. Even to understand the word ''doublethink'' involved the use of doublethink..', null, 1, '2010-01-01 00:00:00'); +INSERT INTO book VALUES (2, 1, null, null, 'Animal Farm', 1945, 1, null, null, null, '2010-01-01 00:00:00'); +INSERT INTO book VALUES (3, 2, null, null, 'O Alquimista', 1988, 4, null, null, 1, null); +INSERT INTO book VALUES (4, 2, null, null, 'Brida', 1990, 2, null, null, null, null); + +INSERT INTO book_store (name) VALUES + ('Orell Füssli'), + ('Ex Libris'), + ('Buchhandlung im Volkshaus'); + +INSERT INTO book_to_book_store VALUES + ('Orell Füssli', 1, 10), + ('Orell Füssli', 2, 10), + ('Orell Füssli', 3, 10), + ('Ex Libris', 1, 1), + ('Ex Libris', 3, 2), + ('Buchhandlung im Volkshaus', 3, 1); diff --git a/jOOQ-examples/jOOQ-spring-boot-example/src/main/resources/schema-h2.sql b/jOOQ-examples/jOOQ-spring-boot-example/src/main/resources/schema-h2.sql new file mode 100644 index 0000000000..cb329b9077 --- /dev/null +++ b/jOOQ-examples/jOOQ-spring-boot-example/src/main/resources/schema-h2.sql @@ -0,0 +1,57 @@ +DROP TABLE IF EXISTS book_to_book_store; +DROP TABLE IF EXISTS book_store; +DROP TABLE IF EXISTS book; +DROP TABLE IF EXISTS author; + +DROP SEQUENCE IF EXISTS s_author_id; +CREATE SEQUENCE s_author_id START WITH 1; + +CREATE TABLE author ( + id INT NOT NULL, + first_name VARCHAR(50), + last_name VARCHAR(50) NOT NULL, + date_of_birth DATE, + year_of_birth INT, + address VARCHAR(50), + + CONSTRAINT pk_t_author PRIMARY KEY (ID) +); + +CREATE TABLE book ( + id INT NOT NULL, + author_id INT NOT NULL, + co_author_id INT, + details_id INT, + title VARCHAR(400) NOT NULL, + published_in INT, + language_id INT, + content_text CLOB, + content_pdf BLOB, + + rec_version INT, + rec_timestamp TIMESTAMP, + + CONSTRAINT pk_t_book PRIMARY KEY (id), + CONSTRAINT fk_t_book_author_id FOREIGN KEY (author_id) REFERENCES author(id), + CONSTRAINT fk_t_book_co_author_id FOREIGN KEY (co_author_id) REFERENCES author(id) +); + +CREATE TABLE book_store ( + name VARCHAR(400) NOT NULL, + + CONSTRAINT uk_t_book_store_name PRIMARY KEY(name) +); + +CREATE TABLE book_to_book_store ( + book_store_name VARCHAR(400) NOT NULL, + book_id INTEGER NOT NULL, + stock INTEGER, + + CONSTRAINT pk_b2bs PRIMARY KEY(book_store_name, book_id), + CONSTRAINT fk_b2bs_bs_name FOREIGN KEY (book_store_name) + REFERENCES book_store (name) + ON DELETE CASCADE, + CONSTRAINT fk_b2bs_b_id FOREIGN KEY (book_id) + REFERENCES book (id) + ON DELETE CASCADE +); \ No newline at end of file diff --git a/jOOQ-examples/jOOQ-spring-boot-example/src/test/java/org/jooq/example/spring/QueryTest.java b/jOOQ-examples/jOOQ-spring-boot-example/src/test/java/org/jooq/example/spring/QueryTest.java new file mode 100644 index 0000000000..32893b0884 --- /dev/null +++ b/jOOQ-examples/jOOQ-spring-boot-example/src/test/java/org/jooq/example/spring/QueryTest.java @@ -0,0 +1,66 @@ +package org.jooq.example.spring; + +import static java.util.Arrays.asList; +import static org.jooq.example.db.h2.Tables.AUTHOR; +import static org.jooq.example.db.h2.Tables.BOOK; +import static org.jooq.example.db.h2.Tables.BOOK_STORE; +import static org.jooq.example.db.h2.Tables.BOOK_TO_BOOK_STORE; +import static org.jooq.impl.DSL.countDistinct; +import static org.junit.Assert.assertEquals; + +import org.jooq.DSLContext; +import org.jooq.Record3; +import org.jooq.Result; +import org.jooq.example.db.h2.tables.Author; +import org.jooq.example.db.h2.tables.Book; +import org.jooq.example.db.h2.tables.BookStore; +import org.jooq.example.db.h2.tables.BookToBookStore; +import org.jooq.example.db.h2.tables.records.BookRecord; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Lukas Eder + * @author Thomas Darimont + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +public class QueryTest { + + @Autowired DSLContext create; + + @Test + public void testJoin() throws Exception { + // All of these tables were generated by jOOQ's Maven plugin + Book b = BOOK.as("b"); + Author a = AUTHOR.as("a"); + BookStore s = BOOK_STORE.as("s"); + BookToBookStore t = BOOK_TO_BOOK_STORE.as("t"); + + Result> result = create.select(a.FIRST_NAME, a.LAST_NAME, countDistinct(s.NAME)) + .from(a).join(b).on(b.AUTHOR_ID.equal(a.ID)).join(t).on(t.BOOK_ID.equal(b.ID)).join(s) + .on(t.BOOK_STORE_NAME.equal(s.NAME)).groupBy(a.FIRST_NAME, a.LAST_NAME).orderBy(countDistinct(s.NAME).desc()) + .fetch(); + + assertEquals(2, result.size()); + assertEquals("Paulo", result.getValue(0, a.FIRST_NAME)); + assertEquals("George", result.getValue(1, a.FIRST_NAME)); + + assertEquals("Coelho", result.getValue(0, a.LAST_NAME)); + assertEquals("Orwell", result.getValue(1, a.LAST_NAME)); + + assertEquals(Integer.valueOf(3), result.getValue(0, countDistinct(s.NAME))); + assertEquals(Integer.valueOf(2), result.getValue(1, countDistinct(s.NAME))); + } + + @Test + public void testActiveRecords() throws Exception { + Result result = create.selectFrom(BOOK).orderBy(BOOK.ID).fetch(); + + assertEquals(4, result.size()); + assertEquals(asList(1, 2, 3, 4), result.getValues(0)); + } +} diff --git a/jOOQ-examples/jOOQ-spring-boot-example/src/test/java/org/jooq/example/spring/TransactionTest.java b/jOOQ-examples/jOOQ-spring-boot-example/src/test/java/org/jooq/example/spring/TransactionTest.java new file mode 100644 index 0000000000..83f7795d1a --- /dev/null +++ b/jOOQ-examples/jOOQ-spring-boot-example/src/test/java/org/jooq/example/spring/TransactionTest.java @@ -0,0 +1,162 @@ +package org.jooq.example.spring; + +import static org.jooq.example.db.h2.Tables.BOOK; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.concurrent.atomic.AtomicBoolean; + +import org.jooq.DSLContext; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.transaction.TransactionConfiguration; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.DefaultTransactionDefinition; + +/** + * @author Petri Kainulainen + * @author Lukas Eder + * @author Thomas Darimont + * @see http://www.petrikainulainen.net/programming/jooq/using-jooq-with-spring-configuration/ + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +@TransactionConfiguration(transactionManager = "transactionManager") +public class TransactionTest { + + @Autowired DSLContext dsl; + @Autowired DataSourceTransactionManager txMgr; + @Autowired SpringTransactionProvider txProvider; + @Autowired BookService books; + + @After + public void teardown() { + + // Delete all books that were created in any test + dsl.delete(BOOK).where(BOOK.ID.gt(4)).execute(); + } + + @Test + public void testExplicitTransactions() { + boolean rollback = false; + + TransactionStatus tx = txMgr.getTransaction(new DefaultTransactionDefinition()); + try { + + // This is a "bug". The same book is created twice, resulting in a + // constraint violation exception + for (int i = 0; i < 2; i++) + dsl.insertInto(BOOK).set(BOOK.ID, 5).set(BOOK.AUTHOR_ID, 1).set(BOOK.TITLE, "Book 5").execute(); + + Assert.fail(); + } + + // Upon the constraint violation, we explicitly roll back the transaction. + catch (DataAccessException e) { + txMgr.rollback(tx); + rollback = true; + } + + assertEquals(4, dsl.fetchCount(BOOK)); + assertTrue(rollback); + } + + @Test + public void testDeclarativeTransactions() { + boolean rollback = false; + + try { + books.create(5, 1, "Book 5"); + Assert.fail(); + } catch (DataAccessException ignore) { + rollback = true; + } + + assertEquals(4, dsl.fetchCount(BOOK)); + assertTrue(rollback); + } + + @Test + public void testjOOQTransactionsSimple() { + boolean rollback = false; + + try { + dsl.transaction(c -> { + + // This is a "bug". The same book is created twice, resulting in a + // constraint violation exception + for (int i = 0; i < 2; i++) + dsl.insertInto(BOOK).set(BOOK.ID, 5).set(BOOK.AUTHOR_ID, 1).set(BOOK.TITLE, "Book 5").execute(); + + Assert.fail(); + }); + } + + // Upon the constraint violation, the transaction must already have been rolled back + catch (DataAccessException e) { + rollback = true; + } + + assertEquals(4, dsl.fetchCount(BOOK)); + assertTrue(rollback); + } + + @Test + public void testjOOQTransactionsNested() { + AtomicBoolean rollback1 = new AtomicBoolean(false); + AtomicBoolean rollback2 = new AtomicBoolean(false); + + try { + + // If using Spring transactions, we don't need the c1 reference + dsl.transaction(c1 -> { + + // The first insertion will work + dsl.insertInto(BOOK).set(BOOK.ID, 5).set(BOOK.AUTHOR_ID, 1).set(BOOK.TITLE, "Book 5").execute(); + + assertEquals(5, dsl.fetchCount(BOOK)); + + try { + + // Nest transactions using Spring. This should create a savepoint, right here + // If using Spring transactions, we don't need the c2 reference + dsl.transaction(c2 -> { + + // The second insertion shouldn't work + for (int i = 0; i < 2; i++) + dsl.insertInto(BOOK).set(BOOK.ID, 6).set(BOOK.AUTHOR_ID, 1).set(BOOK.TITLE, "Book 6").execute(); + + Assert.fail(); + }); + } + + catch (DataAccessException e) { + rollback1.set(true); + } + + // We should've rolled back to the savepoint + assertEquals(5, dsl.fetchCount(BOOK)); + + throw new org.jooq.exception.DataAccessException("Rollback"); + }); + } + + // Upon the constraint violation, the transaction must already have been rolled back + catch (org.jooq.exception.DataAccessException e) { + assertEquals("Rollback", e.getMessage()); + rollback2.set(true); + } + + assertEquals(4, dsl.fetchCount(BOOK)); + assertTrue(rollback2.get()); + assertTrue(rollback2.get()); + } +} diff --git a/jOOQ-examples/jooq-spring-boot-example/.gitignore b/jOOQ-examples/jooq-spring-boot-example/.gitignore new file mode 100644 index 0000000000..26ae8c908e --- /dev/null +++ b/jOOQ-examples/jooq-spring-boot-example/.gitignore @@ -0,0 +1,5 @@ +.classpath +.project +.settings +/target +/*.iml diff --git a/jOOQ-examples/jooq-spring-boot-example/LICENSE.txt b/jOOQ-examples/jooq-spring-boot-example/LICENSE.txt new file mode 100644 index 0000000000..7549858b7f --- /dev/null +++ b/jOOQ-examples/jooq-spring-boot-example/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com) +All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Other licenses: +----------------------------------------------------------------------------- +Commercial licenses for this work are available. These replace the above +ASL 2.0 and offer limited warranties, support, maintenance, and commercial +database integrations. + +For more information, please visit: http://www.jooq.org/licenses \ No newline at end of file