[#2891] Improved jOOQ-spring-example to better fit into the manual

This commit is contained in:
Lukas Eder 2014-01-06 18:06:07 +01:00
parent f64081f0b3
commit c19fc7605f
7 changed files with 107 additions and 125 deletions

View File

@ -18,12 +18,30 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<bonecp.version>0.8.0.RELEASE</bonecp.version>
<org.springframework.version>3.2.3.RELEASE</org.springframework.version>
<org.jooq.version>3.3.0-SNAPSHOT</org.jooq.version>
</properties>
<dependencies>
<!-- Database access -->
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>${org.jooq.version}</version>
</dependency>
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.168</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
@ -35,88 +53,39 @@
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>${org.jooq.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.168</version>
</dependency>
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
<version>${bonecp.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Spring (transitive dependencies are not listed explicitly) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<version>1.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.4.9</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<!-- The resources element will replace available properties in all matching
resources. We use this to provide H2 connection configuration to various
configuration elements -->
<resources>
<resource>
<filtering>true</filtering>
@ -128,6 +97,10 @@
</resources>
<plugins>
<!-- We're using the properties plugin to load external properties into Maven.
See this excellent blog post for an explanation:
http://www.petrikainulainen.net/programming/tips-and-tricks/creating-profile-specific-configuration-files-with-maven/ -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
@ -153,9 +126,7 @@
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>
<!-- common configuration shared by all executions -->
<configuration>
<!--all executions are ignored if -Dmaven.test.skip=true -->
<skip>${maven.test.skip}</skip>
<forkMode>always</forkMode>
</configuration>
@ -190,7 +161,7 @@
</dependencies>
</plugin>
<!-- The jOOQ code generator plugin for Postgres / Sybase ASE / MySQL -->
<!-- The jOOQ code generator plugin -->
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>

View File

@ -40,7 +40,7 @@
*/
package org.jooq.example.spring.impl;
import static org.jooq.example.db.h2.Tables.T_BOOK;
import static org.jooq.example.db.h2.Tables.BOOK;
import org.jooq.DSLContext;
import org.jooq.example.spring.BookService;
@ -63,10 +63,10 @@ public class DefaultBookService implements BookService {
// 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(T_BOOK)
.set(T_BOOK.ID, id)
.set(T_BOOK.AUTHOR_ID, authorId)
.set(T_BOOK.TITLE, title)
dsl.insertInto(BOOK)
.set(BOOK.ID, id)
.set(BOOK.AUTHOR_ID, authorId)
.set(BOOK.TITLE, title)
.execute();
}
}

View File

@ -1,6 +1,6 @@
#Database Configuration
db.driver=org.h2.Driver
db.url=jdbc:h2:~/maven-test
db.url=jdbc:h2:~/jooq-spring-example
db.username=sa
db.password=

View File

@ -1,11 +1,12 @@
DROP TABLE IF EXISTS t_book_to_book_store;
DROP TABLE IF EXISTS t_book_store;
DROP TABLE IF EXISTS t_book;
DROP TABLE IF EXISTS t_author;
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 t_author (
CREATE TABLE author (
id INT NOT NULL,
first_name VARCHAR(50),
last_name VARCHAR(50) NOT NULL,
@ -16,7 +17,7 @@ CREATE TABLE t_author (
CONSTRAINT pk_t_author PRIMARY KEY (ID)
);
CREATE TABLE t_book (
CREATE TABLE book (
id INT NOT NULL,
author_id INT NOT NULL,
co_author_id INT,
@ -31,44 +32,44 @@ CREATE TABLE t_book (
rec_timestamp TIMESTAMP,
CONSTRAINT pk_t_book PRIMARY KEY (id),
CONSTRAINT fk_t_book_author_id FOREIGN KEY (author_id) REFERENCES t_author(id),
CONSTRAINT fk_t_book_co_author_id FOREIGN KEY (co_author_id) REFERENCES t_author(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 t_book_store (
CREATE TABLE book_store (
name VARCHAR(400) NOT NULL,
CONSTRAINT uk_t_book_store_name PRIMARY KEY(name)
);
CREATE TABLE t_book_to_book_store (
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 t_book_store (name)
REFERENCES book_store (name)
ON DELETE CASCADE,
CONSTRAINT fk_b2bs_b_id FOREIGN KEY (book_id)
REFERENCES t_book (id)
REFERENCES book (id)
ON DELETE CASCADE
);
INSERT INTO t_author VALUES (next value for s_author_id, 'George', 'Orwell', '1903-06-25', 1903, null);
INSERT INTO t_author VALUES (next value for s_author_id, 'Paulo', 'Coelho', '1947-08-24', 1947, null);
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 t_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 t_book VALUES (2, 1, null, null, 'Animal Farm', 1945, 1, null, null, null, '2010-01-01 00:00:00');
INSERT INTO t_book VALUES (3, 2, null, null, 'O Alquimista', 1988, 4, null, null, 1, null);
INSERT INTO t_book VALUES (4, 2, null, null, 'Brida', 1990, 2, null, null, null, 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 t_book_store (name) VALUES
INSERT INTO book_store (name) VALUES
('Orell Füssli'),
('Ex Libris'),
('Buchhandlung im Volkshaus');
INSERT INTO t_book_to_book_store VALUES
INSERT INTO book_to_book_store VALUES
('Orell Füssli', 1, 10),
('Orell Füssli', 2, 10),
('Orell Füssli', 3, 10),

View File

@ -3,40 +3,39 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- This is needed if you want to use the @Transactional annotation -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Using BoneCP as a connection pooling library.
Replace this with your preferred DataSource implementation -->
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<!-- These properties are replaced by Maven "resources" -->
<property name="driverClass" value="${db.driver}"/>
<property name="jdbcUrl" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<!-- Using Spring JDBC for transaction management -->
<!-- Configure Spring's transaction manager to use a DataSource -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Configure jOOQ's ConnectionProvider to use Spring's TransactionAwareDataSourceProxy,
which can dynamically discover the transaction context -->
<bean id="transactionAwareDataSource"
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg ref="dataSource" />
</bean>
<!-- Bridging Spring JDBC data sources to jOOQ's ConnectionProvider -->
<bean class="org.jooq.impl.DataSourceConnectionProvider" name="connectionProvider">
<constructor-arg ref="transactionAwareDataSource" />
</bean>
<!-- Configure the DSL object, optionally overriding jOOQ Exceptions with Spring Exceptions -->
<bean id="dsl" class="org.jooq.impl.DefaultDSLContext">
<constructor-arg ref="config" />
</bean>
@ -62,5 +61,6 @@
<constructor-arg index="7"><null /></constructor-arg>
</bean>
<!-- This is the "business-logic" -->
<bean id="books" class="org.jooq.example.spring.impl.DefaultBookService"/>
</beans>

View File

@ -42,20 +42,20 @@ package org.jooq.example;
import static java.util.Arrays.asList;
import static junit.framework.Assert.assertEquals;
import static org.jooq.example.db.h2.Tables.T_AUTHOR;
import static org.jooq.example.db.h2.Tables.T_BOOK;
import static org.jooq.example.db.h2.Tables.T_BOOK_STORE;
import static org.jooq.example.db.h2.Tables.T_BOOK_TO_BOOK_STORE;
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 org.jooq.DSLContext;
import org.jooq.Record3;
import org.jooq.Result;
import org.jooq.example.db.h2.tables.TAuthor;
import org.jooq.example.db.h2.tables.TBook;
import org.jooq.example.db.h2.tables.TBookStore;
import org.jooq.example.db.h2.tables.TBookToBookStore;
import org.jooq.example.db.h2.tables.records.TBookRecord;
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;
@ -75,10 +75,11 @@ public class QueryTest {
@Test
public void testJoin() throws Exception {
TBook b = T_BOOK.as("b");
TAuthor a = T_AUTHOR.as("a");
TBookStore s = T_BOOK_STORE.as("s");
TBookToBookStore t = T_BOOK_TO_BOOK_STORE.as("t");
// 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<Record3<String, String, Integer>> result =
create.select(a.FIRST_NAME, a.LAST_NAME, countDistinct(s.NAME))
@ -103,7 +104,7 @@ public class QueryTest {
@Test
public void testActiveRecords() throws Exception {
Result<TBookRecord> result = create.selectFrom(T_BOOK).orderBy(T_BOOK.ID).fetch();
Result<BookRecord> result = create.selectFrom(BOOK).orderBy(BOOK.ID).fetch();
assertEquals(4, result.size());
assertEquals(asList(1, 2, 3, 4), result.getValues(0));

View File

@ -40,7 +40,7 @@
*/
package org.jooq.example;
import static org.jooq.example.db.h2.Tables.T_BOOK;
import static org.jooq.example.db.h2.Tables.BOOK;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import junit.framework.Assert;
@ -80,26 +80,35 @@ public class TransactionTest {
public void teardown() {
// Delete all books that were created in any test
dsl.delete(T_BOOK).where(T_BOOK.ID.gt(4)).execute();
dsl.delete(BOOK).where(BOOK.ID.gt(4)).execute();
}
@Test
public void testExplicitTransactions() {
boolean rollback = false;
// Execute some jOOQ queries in an explicit transaction
// ----------------------------------------------------
TransactionStatus tx = txMgr.getTransaction(new DefaultTransactionDefinition());
try {
books.create(5, 1, "Book 5");
// 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(T_BOOK));
assertEquals(4, dsl.fetchCount(BOOK));
assertTrue(rollback);
}
@ -115,7 +124,7 @@ public class TransactionTest {
rollback = true;
}
assertEquals(4, dsl.fetchCount(T_BOOK));
assertEquals(4, dsl.fetchCount(BOOK));
assertTrue(rollback);
}
}