diff --git a/jOOQ-examples/jOOQ-javaee-example/.classpath b/jOOQ-examples/jOOQ-javaee-example/.classpath index ab6a8507a6..ab6a433f29 100644 --- a/jOOQ-examples/jOOQ-javaee-example/.classpath +++ b/jOOQ-examples/jOOQ-javaee-example/.classpath @@ -12,13 +12,6 @@ - - - - - - - diff --git a/jOOQ-examples/jOOQ-javaee-example/.settings/org.eclipse.core.resources.prefs b/jOOQ-examples/jOOQ-javaee-example/.settings/org.eclipse.core.resources.prefs index 5c5bb0bdd5..e3dd18fdb9 100644 --- a/jOOQ-examples/jOOQ-javaee-example/.settings/org.eclipse.core.resources.prefs +++ b/jOOQ-examples/jOOQ-javaee-example/.settings/org.eclipse.core.resources.prefs @@ -2,5 +2,4 @@ eclipse.preferences.version=1 encoding//src/main/java=UTF-8 encoding//src/main/resources=UTF-8 encoding//src/main/webapp/library.xhtml=UTF-8 -encoding//src/test/java=UTF-8 encoding/=UTF-8 diff --git a/jOOQ-examples/jOOQ-javaee-example/README.md b/jOOQ-examples/jOOQ-javaee-example/README.md index 3de0f40e04..2f593eb001 100644 --- a/jOOQ-examples/jOOQ-javaee-example/README.md +++ b/jOOQ-examples/jOOQ-javaee-example/README.md @@ -14,3 +14,14 @@ $ cd jOOQ-examples/jOOQ-javaee-example ... $ mvn clean install ``` + +After the above, you should find a `jooq-javaee-example.war` file in + +``` +$ pwd +/path/to/checkout/dir +$ cd jOOQ-examples/jOOQ-javaee-example/target +... +``` + +You can deploy this war file in your WildFly AS or any other application server. The example will use an embedded H2 database, which should be pre-filled with the library example H2 database. It uses a non-managed `DataSource`, which is configured and consumed directly by the application itself. \ No newline at end of file diff --git a/jOOQ-examples/jOOQ-javaee-example/pom.xml b/jOOQ-examples/jOOQ-javaee-example/pom.xml index 239421d930..808b6f37a4 100644 --- a/jOOQ-examples/jOOQ-javaee-example/pom.xml +++ b/jOOQ-examples/jOOQ-javaee-example/pom.xml @@ -72,11 +72,6 @@ jooq ${org.jooq.version} - - commons-dbcp - commons-dbcp - 1.4 - com.h2database h2 diff --git a/jOOQ-examples/jOOQ-javaee-example/src/main/java/org/jooq/example/javaee/controller/Authors.java b/jOOQ-examples/jOOQ-javaee-example/src/main/java/org/jooq/example/javaee/controller/Library.java similarity index 71% rename from jOOQ-examples/jOOQ-javaee-example/src/main/java/org/jooq/example/javaee/controller/Authors.java rename to jOOQ-examples/jOOQ-javaee-example/src/main/java/org/jooq/example/javaee/controller/Library.java index 0890b08e42..0013b971b1 100644 --- a/jOOQ-examples/jOOQ-javaee-example/src/main/java/org/jooq/example/javaee/controller/Authors.java +++ b/jOOQ-examples/jOOQ-javaee-example/src/main/java/org/jooq/example/javaee/controller/Library.java @@ -62,36 +62,74 @@ import org.jooq.SortField; import org.jooq.Table; import org.jooq.TableField; import org.jooq.UpdatableRecord; +import org.jooq.example.db.h2.tables.Author; import org.jooq.example.db.h2.tables.records.AuthorRecord; import org.jooq.example.db.h2.tables.records.BookRecord; -import org.jooq.example.javaee.ejb.AuthorsEJB; +import org.jooq.example.javaee.ejb.LibraryEJB; import org.jooq.impl.DSL; /** * A bean to be used from JSF pages. + *

+ * The bean is session scoped such that we can have session-based caches of + * database content such as authors or books in this bean. In this simple + * example, we haven't gone into concurrency situations where multiple sessions + * update the library database at the same time, in case of which we might need + * to turn on optimistic locking in jOOQ. * * @author Lukas Eder */ -@Named("authors") +@Named("library") @SessionScoped -public class Authors implements Serializable { +public class Library implements Serializable { private static final long serialVersionUID = 1L; @EJB - private AuthorsEJB ejb; + private LibraryEJB ejb; - // Caches from the DB + // Various caches from the DB + // ------------------------------------------------------------------------- + + /** + * An empty {@link AuthorRecord} that can be used to insert new authors. + */ private AuthorRecord newAuthor; + + /** + * An empty {@link BookRecord} that can be used to insert new books. + */ private BookRecord newBook; + + /** + * The reference to the {@link Record} that is currently being edited. + */ private Record edit; + + /** + * The sort field for each {@link Table}. + */ private Map, SortField> sort = Stream.of(AUTHOR.ID, BOOK.ID).collect( toMap(f -> f.getTable(), f -> f.asc())); + /** + * A cache of all {@link AuthorRecord}s currently in the database. + */ private Result authors; + + /** + * A copy of {@link #authors} that is always sorted alphanumerically. + */ private Result authorsAlphanumeric; + + /** + * A cache of all {@link BookRecord}s currently in the database. + */ private Result books; + // Data access methods + // ------------------------------------------------------------------------- + public Result getAuthors() { if (authors == null) authors = ejb.fetchAuthors(sort.get(AUTHOR)); @@ -113,10 +151,6 @@ public class Authors implements Serializable { return getAuthors().intoMap(AUTHOR.ID); } - public Map> getAuthorColumns() { - return getColumns(AUTHOR); - } - public Result getBooks() { if (books == null) books = ejb.fetchBooks(sort.get(BOOK)); @@ -124,36 +158,70 @@ public class Authors implements Serializable { return books; } - public Map> getBookColumns() { - return getColumns(BOOK); - } - - private Map> getColumns(Table t) { - return Stream.of(t.fields()).collect(toMap(f -> f.getName(), f -> f)); - } - - public Record getEdit() { - return edit; - } - - public Map> getSort() { - return sort.entrySet().stream().collect(toMap(e -> e.getKey().getName(), e -> e.getValue())); - } - public AuthorRecord getNewAuthor() { if (newAuthor == null) - newAuthor = ejb.newAuthor(); + newAuthor = DSL.using(H2).newRecord(AUTHOR); return newAuthor; } public BookRecord getNewBook() { if (newBook == null) - newBook = ejb.newBook(); + newBook = DSL.using(H2).newRecord(BOOK); return newBook; } + // UI state methods + // ------------------------------------------------------------------------- + + /** + * The record being edited. + */ + public Record getEdit() { + return edit; + } + + /** + * A map containing column name -> column pairs of the + * {@link Author} table. + */ + public Map> getAuthorColumns() { + return getColumns(AUTHOR); + } + + /** + * A map containing column name -> column pairs of the + * {@link Author} table. + */ + public Map> getBookColumns() { + return getColumns(BOOK); + } + + /** + * Get a map containing table name -> sort field pairs. + */ + public Map> getSort() { + return sort.entrySet().stream().collect(toMap(e -> e.getKey().getName(), e -> e.getValue())); + } + + private Map> getColumns(Table t) { + return Stream.of(t.fields()).collect(toMap(f -> f.getName(), f -> f)); + } + + private void reset() { + authors = null; + authorsAlphanumeric = null; + books = null; + edit = null; + } + + // Actions + // ------------------------------------------------------------------------- + + /** + * Sort a table by a new field. + */ public void sort(TableField field) { SortField previous = sort.get(field.getTable()); sort.put(field.getTable(), @@ -167,26 +235,26 @@ public class Authors implements Serializable { reset(); } + /** + * Mark a record as the one being currently edited. + */ public void edit(UpdatableRecord record) { edit = record; } + /** + * Save a record back to the database. + */ public void save(UpdatableRecord author) { ejb.store(author); reset(); } + /** + * Delete a record from the database. + */ public void delete(UpdatableRecord author) { ejb.delete(author); reset(); } - - private void reset() { - authors = null; - authorsAlphanumeric = null; - books = null; - edit = null; - newAuthor = ejb.newAuthor(); - newBook = ejb.newBook(); - } } diff --git a/jOOQ-examples/jOOQ-javaee-example/src/main/java/org/jooq/example/javaee/ejb/AuthorsEJB.java b/jOOQ-examples/jOOQ-javaee-example/src/main/java/org/jooq/example/javaee/ejb/LibraryEJB.java similarity index 91% rename from jOOQ-examples/jOOQ-javaee-example/src/main/java/org/jooq/example/javaee/ejb/AuthorsEJB.java rename to jOOQ-examples/jOOQ-javaee-example/src/main/java/org/jooq/example/javaee/ejb/LibraryEJB.java index 17ab154007..66cb438f8e 100644 --- a/jOOQ-examples/jOOQ-javaee-example/src/main/java/org/jooq/example/javaee/ejb/AuthorsEJB.java +++ b/jOOQ-examples/jOOQ-javaee-example/src/main/java/org/jooq/example/javaee/ejb/LibraryEJB.java @@ -45,7 +45,7 @@ import static org.jooq.example.db.h2.Tables.AUTHOR; import static org.jooq.example.db.h2.Tables.BOOK; import javax.annotation.Resource; -import javax.ejb.Stateful; +import javax.ejb.Stateless; import javax.sql.DataSource; import org.jooq.Result; @@ -55,20 +55,18 @@ import org.jooq.example.db.h2.tables.records.AuthorRecord; import org.jooq.example.db.h2.tables.records.BookRecord; import org.jooq.impl.DSL; -@Stateful -public class AuthorsEJB { +/** + * A session bean that uses the configured {@link DataSource} to interact with + * the embedded H2 database. + * + * @author Lukas Eder + */ +@Stateless +public class LibraryEJB { @Resource(lookup="java:jboss/datasources/jooq-javaee-example") private DataSource ds; - public AuthorRecord newAuthor() { - return DSL.using(ds, H2).newRecord(AUTHOR); - } - - public BookRecord newBook() { - return DSL.using(ds, H2).newRecord(BOOK); - } - public Result fetchAuthors(SortField sort) { return DSL.using(ds, H2) .selectFrom(AUTHOR) @@ -84,10 +82,12 @@ public class AuthorsEJB { } public void store(UpdatableRecord record) { + DSL.using(ds, H2).attach(record); record.store(); } public void delete(UpdatableRecord record) { + DSL.using(ds, H2).attach(record); record.delete(); } diff --git a/jOOQ-examples/jOOQ-javaee-example/src/main/resources/db-h2.sql b/jOOQ-examples/jOOQ-javaee-example/src/main/resources/db-h2.sql index f9cc3971d5..8815595f41 100644 --- a/jOOQ-examples/jOOQ-javaee-example/src/main/resources/db-h2.sql +++ b/jOOQ-examples/jOOQ-javaee-example/src/main/resources/db-h2.sql @@ -1,5 +1,3 @@ -DROP TABLE IF EXISTS book_to_book_store; -DROP TABLE IF EXISTS book_store; DROP TABLE IF EXISTS book; DROP TABLE IF EXISTS author; @@ -23,43 +21,10 @@ CREATE TABLE book ( CONSTRAINT fk_t_book_author_id FOREIGN KEY (author_id) REFERENCES author(id) ON DELETE CASCADE, ); -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 -); - INSERT INTO author VALUES (DEFAULT, 'George', 'Orwell', '1903-06-25'); INSERT INTO author VALUES (DEFAULT, 'Paulo', 'Coelho', '1947-08-24'); INSERT INTO book VALUES (DEFAULT, 1, '1984', 1948, 1); INSERT INTO book VALUES (DEFAULT, 1, 'Animal Farm', 1945, 1); INSERT INTO book VALUES (DEFAULT, 2, 'O Alquimista', 1988, 4); -INSERT INTO book VALUES (DEFAULT, 2, 'Brida', 1990, 2); - -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); +INSERT INTO book VALUES (DEFAULT, 2, 'Brida', 1990, 2); \ No newline at end of file diff --git a/jOOQ-examples/jOOQ-javaee-example/src/main/webapp/css/example.css b/jOOQ-examples/jOOQ-javaee-example/src/main/webapp/css/example.css index d85bbdcd3a..af8634d49a 100644 --- a/jOOQ-examples/jOOQ-javaee-example/src/main/webapp/css/example.css +++ b/jOOQ-examples/jOOQ-javaee-example/src/main/webapp/css/example.css @@ -6,7 +6,7 @@ body { } h1, h2, h3 { - padding: 20px; + padding: 10px; margin: 0; color: #eee; } @@ -26,7 +26,7 @@ h3 { td, th { text-align: left; min-width: 50px; - padding: 20px; + padding: 10px; } input { @@ -35,7 +35,7 @@ input { input[type=submit] { min-width: 50px; - margin-right: 20px; + margin-right: 10px; } .div-50 { diff --git a/jOOQ-examples/jOOQ-javaee-example/src/main/webapp/library.xhtml b/jOOQ-examples/jOOQ-javaee-example/src/main/webapp/library.xhtml index d591211f59..6019947de0 100644 --- a/jOOQ-examples/jOOQ-javaee-example/src/main/webapp/library.xhtml +++ b/jOOQ-examples/jOOQ-javaee-example/src/main/webapp/library.xhtml @@ -19,13 +19,13 @@

Authors

- + - + - - + + @@ -34,48 +34,48 @@ - + - - + + - - + + - + - + - - + + - - + + - + Actions - - + + - + - + @@ -84,13 +84,13 @@

Books

- + - + - - + + @@ -100,45 +100,45 @@ Author - - - + + + - - + + - + - - + + - - + + - + Actions - - + + - + - +