BAEL-4520 Getting Started with jOOQ (#10086)

* BAEL-4520 Getting Started with jOOQ

* add generated sources

* remove maven files

* create tests

Co-authored-by: Krzysztof Majewski <krzysztof.majewski.km1@contractors.roche.com>
This commit is contained in:
Krzysztof Majewski 2020-09-25 23:39:32 +02:00 committed by GitHub
parent 35a9131a72
commit 32f4eeef02
13 changed files with 1397 additions and 0 deletions

View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>jooq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jooq-examples</name>
<packaging>jar</packaging>
<description>jOOQ Examples</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>persistence-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>3.13.4</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta</artifactId>
<version>3.13.4</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
<version>3.13.4</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.16</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,57 @@
package com.baeldung.jooq;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SelectFieldOrAsterisk;
import org.jooq.Table;
import org.jooq.UpdatableRecord;
import java.util.Map;
public class Crud {
public static <R extends UpdatableRecord<R>> void save(UpdatableRecord<R> record) {
record.store();
}
public static Result<Record> getAll(DSLContext context, Table<? extends Record> table) {
return context.select()
.from(table)
.fetch();
}
public static Result<Record> getFields(DSLContext context, Table<? extends Record> table, SelectFieldOrAsterisk... fields) {
return context.select(fields)
.from(table)
.fetch();
}
public static <R extends Record> R getOne(DSLContext context, Table<R> table, Condition condition) {
return context.fetchOne(table, condition);
}
public static <T> void update(DSLContext context, Table<? extends Record> table, Map<Field<T>, T> values, Condition condition) {
context.update(table)
.set(values)
.where(condition)
.execute();
}
public static <R extends UpdatableRecord<R>> void update(UpdatableRecord<R> record) {
record.update();
}
public static void delete(DSLContext context, Table<? extends Record> table, Condition condition) {
context.delete(table)
.where(condition)
.execute();
}
public static <R extends UpdatableRecord<R>> void delete(UpdatableRecord<R> record) {
record.delete();
}
}

View File

@ -0,0 +1,117 @@
package com.baeldung.jooq;
import com.baeldung.jooq.model.tables.Article;
import com.baeldung.jooq.model.tables.Author;
import com.baeldung.jooq.model.tables.records.ArticleRecord;
import com.baeldung.jooq.model.tables.records.AuthorRecord;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import static com.baeldung.jooq.Crud.delete;
import static com.baeldung.jooq.Crud.getAll;
import static com.baeldung.jooq.Crud.getFields;
import static com.baeldung.jooq.Crud.getOne;
import static com.baeldung.jooq.Crud.save;
import static com.baeldung.jooq.Crud.update;
public class CrudExamples {
public void crudExamples() throws SQLException {
String userName = "username";
String password = "password";
String url = "jdbc:postgresql://db_url:5432/baeldung_database";
Connection conn = DriverManager.getConnection(url, userName, password);
DSLContext context = DSL.using(conn, SQLDialect.POSTGRES);
createValues(context);
readValues(context);
updateValues(context);
deleteValues(context);
}
private void createValues(DSLContext context) {
ArticleRecord article = context.newRecord(Article.ARTICLE);
article.setId(2);
article.setTitle("jOOQ examples");
article.setDescription("A few examples of jOOQ CRUD operations");
article.setAuthorId(1);
save(article);
AuthorRecord author = context.newRecord(Author.AUTHOR);
author.setId(1);
author.setFirstName("John");
author.setLastName("Smith");
author.setAge(40);
save(author);
}
private void readValues(DSLContext context) {
Result<Record> authors = getAll(
context,
Author.AUTHOR
);
authors.forEach(author -> {
Integer id = author.getValue(Author.AUTHOR.ID);
String firstName = author.getValue(Author.AUTHOR.FIRST_NAME);
String lastName = author.getValue(Author.AUTHOR.LAST_NAME);
Integer age = author.getValue(Author.AUTHOR.AGE);
System.out.printf("Author %s %s has id: %d and age: %d%n", firstName, lastName, id, age);
});
Result<Record> articles = getFields(
context,
Author.AUTHOR,
Article.ARTICLE.ID, Article.ARTICLE.TITLE
);
AuthorRecord author = getOne(
context,
Author.AUTHOR,
Author.AUTHOR.ID.eq(1)
);
}
private void updateValues(DSLContext context) {
HashMap<Field<String>, String> fieldsToUpdate = new HashMap<>();
fieldsToUpdate.put(Author.AUTHOR.FIRST_NAME, "David");
fieldsToUpdate.put(Author.AUTHOR.LAST_NAME, "Brown");
update(
context,
Author.AUTHOR,
fieldsToUpdate,
Author.AUTHOR.ID.eq(1)
);
ArticleRecord article = context.fetchOne(Article.ARTICLE, Article.ARTICLE.ID.eq(1));
article.setTitle("A New Article Title");
update(
article
);
}
private void deleteValues(DSLContext context) {
delete(
context,
Article.ARTICLE,
Article.ARTICLE.ID.eq(1)
);
AuthorRecord author = context.fetchOne(Author.AUTHOR, Author.AUTHOR.ID.eq(1));
delete(author);
}
}

View File

@ -0,0 +1,44 @@
/*
* This file is generated by jOOQ.
*/
package com.baeldung.jooq.model;
import org.jooq.Schema;
import org.jooq.impl.CatalogImpl;
import java.util.Arrays;
import java.util.List;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class DefaultCatalog extends CatalogImpl {
private static final long serialVersionUID = 1035293962;
/**
* The reference instance of <code>DEFAULT_CATALOG</code>
*/
public static final DefaultCatalog DEFAULT_CATALOG = new DefaultCatalog();
/**
* The schema <code>public</code>.
*/
public final Public PUBLIC = Public.PUBLIC;
/**
* No further instances allowed
*/
private DefaultCatalog() {
super("");
}
@Override
public final List<Schema> getSchemas() {
return Arrays.<Schema>asList(
Public.PUBLIC);
}
}

View File

@ -0,0 +1,48 @@
/*
* This file is generated by jOOQ.
*/
package com.baeldung.jooq.model;
import com.baeldung.jooq.model.tables.Article;
import com.baeldung.jooq.model.tables.Author;
import com.baeldung.jooq.model.tables.records.ArticleRecord;
import com.baeldung.jooq.model.tables.records.AuthorRecord;
import org.jooq.ForeignKey;
import org.jooq.TableField;
import org.jooq.UniqueKey;
import org.jooq.impl.Internal;
/**
* A class modelling foreign key relationships and constraints of tables of
* the <code>public</code> schema.
*/
@SuppressWarnings({"all", "unchecked", "rawtypes"})
public class Keys {
// -------------------------------------------------------------------------
// UNIQUE and PRIMARY KEY definitions
// -------------------------------------------------------------------------
public static final UniqueKey<ArticleRecord> ARTICLE_PKEY = UniqueKeys0.ARTICLE_PKEY;
public static final UniqueKey<AuthorRecord> AUTHOR_PKEY = UniqueKeys0.AUTHOR_PKEY;
// -------------------------------------------------------------------------
// FOREIGN KEY definitions
// -------------------------------------------------------------------------
public static final ForeignKey<ArticleRecord, AuthorRecord> ARTICLE__XXX = ForeignKeys0.ARTICLE__XXX;
// -------------------------------------------------------------------------
// [#1459] distribute members to avoid static initialisers > 64kb
// -------------------------------------------------------------------------
private static class UniqueKeys0 {
public static final UniqueKey<ArticleRecord> ARTICLE_PKEY = Internal.createUniqueKey(Article.ARTICLE, "article_pkey", new TableField[]{Article.ARTICLE.ID}, true);
public static final UniqueKey<AuthorRecord> AUTHOR_PKEY = Internal.createUniqueKey(Author.AUTHOR, "author_pkey", new TableField[]{Author.AUTHOR.ID}, true);
}
private static class ForeignKeys0 {
public static final ForeignKey<ArticleRecord, AuthorRecord> ARTICLE__XXX = Internal.createForeignKey(Keys.AUTHOR_PKEY, Article.ARTICLE, "xxx", new TableField[]{Article.ARTICLE.AUTHOR_ID}, true);
}
}

View File

@ -0,0 +1,59 @@
/*
* This file is generated by jOOQ.
*/
package com.baeldung.jooq.model;
import com.baeldung.jooq.model.tables.Article;
import com.baeldung.jooq.model.tables.Author;
import org.jooq.Catalog;
import org.jooq.Table;
import org.jooq.impl.SchemaImpl;
import java.util.Arrays;
import java.util.List;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({"all", "unchecked", "rawtypes"})
public class Public extends SchemaImpl {
private static final long serialVersionUID = -2049410122;
/**
* The reference instance of <code>public</code>
*/
public static final Public PUBLIC = new Public();
/**
* The table <code>public.article</code>.
*/
public final Article ARTICLE = Article.ARTICLE;
/**
* The table <code>public.author</code>.
*/
public final Author AUTHOR = Author.AUTHOR;
/**
* No further instances allowed
*/
private Public() {
super("public", null);
}
@Override
public Catalog getCatalog() {
return DefaultCatalog.DEFAULT_CATALOG;
}
@Override
public final List<Table<?>> getTables() {
return Arrays.<Table<?>>asList(
Article.ARTICLE,
Author.AUTHOR
);
}
}

View File

@ -0,0 +1,25 @@
/*
* This file is generated by jOOQ.
*/
package com.baeldung.jooq.model;
import com.baeldung.jooq.model.tables.Article;
import com.baeldung.jooq.model.tables.Author;
/**
* Convenience access to all tables in public
*/
@SuppressWarnings({"all", "unchecked", "rawtypes"})
public class Tables {
/**
* The table <code>public.article</code>.
*/
public static final Article ARTICLE = Article.ARTICLE;
/**
* The table <code>public.author</code>.
*/
public static final Author AUTHOR = Author.AUTHOR;
}

View File

@ -0,0 +1,159 @@
/*
* This file is generated by jOOQ.
*/
package com.baeldung.jooq.model.tables;
import com.baeldung.jooq.model.Keys;
import com.baeldung.jooq.model.Public;
import com.baeldung.jooq.model.tables.records.ArticleRecord;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Row4;
import org.jooq.Schema;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableOptions;
import org.jooq.UniqueKey;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import java.util.Arrays;
import java.util.List;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Article extends TableImpl<ArticleRecord> {
private static final long serialVersionUID = -1401275800;
/**
* The reference instance of <code>public.article</code>
*/
public static final Article ARTICLE = new Article();
/**
* The class holding records for this type
*/
@Override
public Class<ArticleRecord> getRecordType() {
return ArticleRecord.class;
}
/**
* The column <code>public.article.id</code>.
*/
public final TableField<ArticleRecord, Integer> ID = createField(DSL.name("id"), org.jooq.impl.SQLDataType.INTEGER.nullable(false), this, "");
/**
* The column <code>public.article.title</code>.
*/
public final TableField<ArticleRecord, String> TITLE = createField(DSL.name("title"), org.jooq.impl.SQLDataType.VARCHAR(255).nullable(false), this, "");
/**
* The column <code>public.article.description</code>.
*/
public final TableField<ArticleRecord, String> DESCRIPTION = createField(DSL.name("description"), org.jooq.impl.SQLDataType.VARCHAR(255), this, "");
/**
* The column <code>public.article.author_id</code>.
*/
public final TableField<ArticleRecord, Integer> AUTHOR_ID = createField(DSL.name("author_id"), org.jooq.impl.SQLDataType.INTEGER, this, "");
/**
* Create a <code>public.article</code> table reference
*/
public Article() {
this(DSL.name("article"), null);
}
/**
* Create an aliased <code>public.article</code> table reference
*/
public Article(String alias) {
this(DSL.name(alias), ARTICLE);
}
/**
* Create an aliased <code>public.article</code> table reference
*/
public Article(Name alias) {
this(alias, ARTICLE);
}
private Article(Name alias, Table<ArticleRecord> aliased) {
this(alias, aliased, null);
}
private Article(Name alias, Table<ArticleRecord> aliased, Field<?>[] parameters) {
super(alias, null, aliased, parameters, DSL.comment(""), TableOptions.table());
}
public <O extends Record> Article(Table<O> child, ForeignKey<O, ArticleRecord> key) {
super(child, key, ARTICLE);
}
@Override
public Schema getSchema() {
return Public.PUBLIC;
}
@Override
public UniqueKey<ArticleRecord> getPrimaryKey() {
return Keys.ARTICLE_PKEY;
}
@Override
public List<UniqueKey<ArticleRecord>> getKeys() {
return Arrays.<UniqueKey<ArticleRecord>>asList(Keys.ARTICLE_PKEY);
}
@Override
public List<ForeignKey<ArticleRecord, ?>> getReferences() {
return Arrays.<ForeignKey<ArticleRecord, ?>>asList(Keys.ARTICLE__XXX);
}
public Author author() {
return new Author(this, Keys.ARTICLE__XXX);
}
@Override
public Article as(String alias) {
return new Article(DSL.name(alias), this);
}
@Override
public Article as(Name alias) {
return new Article(alias, this);
}
/**
* Rename this table
*/
@Override
public Article rename(String name) {
return new Article(DSL.name(name), null);
}
/**
* Rename this table
*/
@Override
public Article rename(Name name) {
return new Article(name, null);
}
// -------------------------------------------------------------------------
// Row4 type methods
// -------------------------------------------------------------------------
@Override
public Row4<Integer, String, String, Integer> fieldsRow() {
return (Row4) super.fieldsRow();
}
}

View File

@ -0,0 +1,149 @@
/*
* This file is generated by jOOQ.
*/
package com.baeldung.jooq.model.tables;
import com.baeldung.jooq.model.Keys;
import com.baeldung.jooq.model.Public;
import com.baeldung.jooq.model.tables.records.AuthorRecord;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Row4;
import org.jooq.Schema;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableOptions;
import org.jooq.UniqueKey;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import java.util.Arrays;
import java.util.List;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Author extends TableImpl<AuthorRecord> {
private static final long serialVersionUID = -798376522;
/**
* The reference instance of <code>public.author</code>
*/
public static final Author AUTHOR = new Author();
/**
* The class holding records for this type
*/
@Override
public Class<AuthorRecord> getRecordType() {
return AuthorRecord.class;
}
/**
* The column <code>public.author.id</code>.
*/
public final TableField<AuthorRecord, Integer> ID = createField(DSL.name("id"), org.jooq.impl.SQLDataType.INTEGER.nullable(false), AUTHOR, "");
/**
* The column <code>public.author.first_name</code>.
*/
public final TableField<AuthorRecord, String> FIRST_NAME = createField(DSL.name("first_name"), org.jooq.impl.SQLDataType.VARCHAR(255), this, "");
/**
* The column <code>public.author.last_name</code>.
*/
public final TableField<AuthorRecord, String> LAST_NAME = createField(DSL.name("last_name"), org.jooq.impl.SQLDataType.VARCHAR(255), this, "");
/**
* The column <code>public.author.age</code>.
*/
public final TableField<AuthorRecord, Integer> AGE = createField(DSL.name("age"), org.jooq.impl.SQLDataType.INTEGER, this, "");
/**
* Create a <code>public.author</code> table reference
*/
public Author() {
this(DSL.name("author"), null);
}
/**
* Create an aliased <code>public.author</code> table reference
*/
public Author(String alias) {
this(DSL.name(alias), AUTHOR);
}
/**
* Create an aliased <code>public.author</code> table reference
*/
public Author(Name alias) {
this(alias, AUTHOR);
}
private Author(Name alias, Table<AuthorRecord> aliased) {
this(alias, aliased, null);
}
private Author(Name alias, Table<AuthorRecord> aliased, Field<?>[] parameters) {
super(alias, null, aliased, parameters, DSL.comment(""), TableOptions.table());
}
public <O extends Record> Author(Table<O> child, ForeignKey<O, AuthorRecord> key) {
super(child, key, AUTHOR);
}
@Override
public Schema getSchema() {
return Public.PUBLIC;
}
@Override
public UniqueKey<AuthorRecord> getPrimaryKey() {
return Keys.AUTHOR_PKEY;
}
@Override
public List<UniqueKey<AuthorRecord>> getKeys() {
return Arrays.<UniqueKey<AuthorRecord>>asList(Keys.AUTHOR_PKEY);
}
@Override
public Author as(String alias) {
return new Author(DSL.name(alias), this);
}
@Override
public Author as(Name alias) {
return new Author(alias, this);
}
/**
* Rename this table
*/
@Override
public Author rename(String name) {
return new Author(DSL.name(name), null);
}
/**
* Rename this table
*/
@Override
public Author rename(Name name) {
return new Author(name, null);
}
// -------------------------------------------------------------------------
// Row4 type methods
// -------------------------------------------------------------------------
@Override
public Row4<Integer, String, String, Integer> fieldsRow() {
return (Row4) super.fieldsRow();
}
}

View File

@ -0,0 +1,218 @@
/*
* This file is generated by jOOQ.
*/
package com.baeldung.jooq.model.tables.records;
import com.baeldung.jooq.model.tables.Article;
import org.jooq.Field;
import org.jooq.Record1;
import org.jooq.Record4;
import org.jooq.Row4;
import org.jooq.impl.UpdatableRecordImpl;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class ArticleRecord extends UpdatableRecordImpl<ArticleRecord> implements Record4<Integer, String, String, Integer> {
private static final long serialVersionUID = 1297442421;
/**
* Setter for <code>public.article.id</code>.
*/
public void setId(Integer value) {
set(0, value);
}
/**
* Getter for <code>public.article.id</code>.
*/
public Integer getId() {
return (Integer) get(0);
}
/**
* Setter for <code>public.article.title</code>.
*/
public void setTitle(String value) {
set(1, value);
}
/**
* Getter for <code>public.article.title</code>.
*/
public String getTitle() {
return (String) get(1);
}
/**
* Setter for <code>public.article.description</code>.
*/
public void setDescription(String value) {
set(2, value);
}
/**
* Getter for <code>public.article.description</code>.
*/
public String getDescription() {
return (String) get(2);
}
/**
* Setter for <code>public.article.author_id</code>.
*/
public void setAuthorId(Integer value) {
set(3, value);
}
/**
* Getter for <code>public.article.author_id</code>.
*/
public Integer getAuthorId() {
return (Integer) get(3);
}
// -------------------------------------------------------------------------
// Primary key information
// -------------------------------------------------------------------------
@Override
public Record1<Integer> key() {
return (Record1) super.key();
}
// -------------------------------------------------------------------------
// Record4 type implementation
// -------------------------------------------------------------------------
@Override
public Row4<Integer, String, String, Integer> fieldsRow() {
return (Row4) super.fieldsRow();
}
@Override
public Row4<Integer, String, String, Integer> valuesRow() {
return (Row4) super.valuesRow();
}
@Override
public Field<Integer> field1() {
return Article.ARTICLE.ID;
}
@Override
public Field<String> field2() {
return Article.ARTICLE.TITLE;
}
@Override
public Field<String> field3() {
return Article.ARTICLE.DESCRIPTION;
}
@Override
public Field<Integer> field4() {
return Article.ARTICLE.AUTHOR_ID;
}
@Override
public Integer component1() {
return getId();
}
@Override
public String component2() {
return getTitle();
}
@Override
public String component3() {
return getDescription();
}
@Override
public Integer component4() {
return getAuthorId();
}
@Override
public Integer value1() {
return getId();
}
@Override
public String value2() {
return getTitle();
}
@Override
public String value3() {
return getDescription();
}
@Override
public Integer value4() {
return getAuthorId();
}
@Override
public ArticleRecord value1(Integer value) {
setId(value);
return this;
}
@Override
public ArticleRecord value2(String value) {
setTitle(value);
return this;
}
@Override
public ArticleRecord value3(String value) {
setDescription(value);
return this;
}
@Override
public ArticleRecord value4(Integer value) {
setAuthorId(value);
return this;
}
@Override
public ArticleRecord values(Integer value1, String value2, String value3, Integer value4) {
value1(value1);
value2(value2);
value3(value3);
value4(value4);
return this;
}
// -------------------------------------------------------------------------
// Constructors
// -------------------------------------------------------------------------
/**
* Create a detached ArticleRecord
*/
public ArticleRecord() {
super(Article.ARTICLE);
}
/**
* Create a detached, initialised ArticleRecord
*/
public ArticleRecord(Integer id, String title, String description, Integer authorId) {
super(Article.ARTICLE);
set(0, id);
set(1, title);
set(2, description);
set(3, authorId);
}
}

View File

@ -0,0 +1,217 @@
/*
* This file is generated by jOOQ.
*/
package com.baeldung.jooq.model.tables.records;
import com.baeldung.jooq.model.tables.Author;
import org.jooq.Field;
import org.jooq.Record1;
import org.jooq.Record4;
import org.jooq.Row4;
import org.jooq.impl.UpdatableRecordImpl;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class AuthorRecord extends UpdatableRecordImpl<AuthorRecord> implements Record4<Integer, String, String, Integer> {
private static final long serialVersionUID = -2120822720;
/**
* Setter for <code>public.author.id</code>.
*/
public void setId(Integer value) {
set(0, value);
}
/**
* Getter for <code>public.author.id</code>.
*/
public Integer getId() {
return (Integer) get(0);
}
/**
* Setter for <code>public.author.first_name</code>.
*/
public void setFirstName(String value) {
set(1, value);
}
/**
* Getter for <code>public.author.first_name</code>.
*/
public String getFirstName() {
return (String) get(1);
}
/**
* Setter for <code>public.author.last_name</code>.
*/
public void setLastName(String value) {
set(2, value);
}
/**
* Getter for <code>public.author.last_name</code>.
*/
public String getLastName() {
return (String) get(2);
}
/**
* Setter for <code>public.author.age</code>.
*/
public void setAge(Integer value) {
set(3, value);
}
/**
* Getter for <code>public.author.age</code>.
*/
public Integer getAge() {
return (Integer) get(3);
}
// -------------------------------------------------------------------------
// Primary key information
// -------------------------------------------------------------------------
@Override
public Record1<Integer> key() {
return (Record1) super.key();
}
// -------------------------------------------------------------------------
// Record4 type implementation
// -------------------------------------------------------------------------
@Override
public Row4<Integer, String, String, Integer> fieldsRow() {
return (Row4) super.fieldsRow();
}
@Override
public Row4<Integer, String, String, Integer> valuesRow() {
return (Row4) super.valuesRow();
}
@Override
public Field<Integer> field1() {
return Author.AUTHOR.ID;
}
@Override
public Field<String> field2() {
return Author.AUTHOR.FIRST_NAME;
}
@Override
public Field<String> field3() {
return Author.AUTHOR.LAST_NAME;
}
@Override
public Field<Integer> field4() {
return Author.AUTHOR.AGE;
}
@Override
public Integer component1() {
return getId();
}
@Override
public String component2() {
return getFirstName();
}
@Override
public String component3() {
return getLastName();
}
@Override
public Integer component4() {
return getAge();
}
@Override
public Integer value1() {
return getId();
}
@Override
public String value2() {
return getFirstName();
}
@Override
public String value3() {
return getLastName();
}
@Override
public Integer value4() {
return getAge();
}
@Override
public AuthorRecord value1(Integer value) {
setId(value);
return this;
}
@Override
public AuthorRecord value2(String value) {
setFirstName(value);
return this;
}
@Override
public AuthorRecord value3(String value) {
setLastName(value);
return this;
}
@Override
public AuthorRecord value4(Integer value) {
setAge(value);
return this;
}
@Override
public AuthorRecord values(Integer value1, String value2, String value3, Integer value4) {
value1(value1);
value2(value2);
value3(value3);
value4(value4);
return this;
}
// -------------------------------------------------------------------------
// Constructors
// -------------------------------------------------------------------------
/**
* Create a detached AuthorRecord
*/
public AuthorRecord() {
super(Author.AUTHOR);
}
/**
* Create a detached, initialised AuthorRecord
*/
public AuthorRecord(Integer id, String firstName, String lastName, Integer age) {
super(Author.AUTHOR);
set(0, id);
set(1, firstName);
set(2, lastName);
set(3, age);
}
}

View File

@ -0,0 +1,257 @@
package com.baeldung.jooq;
import com.baeldung.jooq.model.tables.Article;
import com.baeldung.jooq.model.tables.Author;
import com.baeldung.jooq.model.tables.records.ArticleRecord;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import static com.baeldung.jooq.Crud.delete;
import static com.baeldung.jooq.Crud.getAll;
import static com.baeldung.jooq.Crud.getFields;
import static com.baeldung.jooq.Crud.getOne;
import static com.baeldung.jooq.Crud.save;
import static com.baeldung.jooq.Crud.update;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class CrudLiveTest {
static DSLContext context;
@BeforeClass
public static void setup() throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:h2:mem:tes;INIT=CREATE SCHEMA IF NOT EXISTS \"public\"");
context = DSL.using(conn, SQLDialect.H2);
context.createTable(Author.AUTHOR)
.columns(
Author.AUTHOR.ID,
Author.AUTHOR.FIRST_NAME,
Author.AUTHOR.LAST_NAME,
Author.AUTHOR.AGE
)
.execute();
context.createTable(Article.ARTICLE)
.columns(
Article.ARTICLE.ID,
Article.ARTICLE.TITLE,
Article.ARTICLE.DESCRIPTION,
Article.ARTICLE.AUTHOR_ID
)
.execute();
}
@Before
public void cleanup() {
context.truncateTable(Article.ARTICLE)
.execute();
context.truncateTable(Author.AUTHOR)
.execute();
}
@Test
public void givenArticleRecord_whenSave_thenNewRecordInDb() {
// given
ArticleRecord article = article();
// when
save(article);
// then
ArticleRecord savedArticle = getOne(
context,
Article.ARTICLE,
Article.ARTICLE.ID.eq(1)
);
assertEquals(savedArticle.getId().intValue(), 1);
assertEquals(savedArticle.getTitle(), "jOOQ examples");
assertEquals(savedArticle.getDescription(), "A few examples of jOOQ CRUD operations");
assertEquals(savedArticle.getAuthorId().intValue(), 1);
}
@Test
public void givenArticleRecord_whenGetAll_thenValidOneRecord() {
// given
ArticleRecord article = article();
save(article);
// when
Result<Record> articles = getAll(
context,
Article.ARTICLE
);
// then
assertEquals(articles.size(), 1);
Record record = articles.get(0);
ArticleRecord savedArticle = record.into(Article.ARTICLE);
assertEquals(savedArticle.getId().intValue(), 1);
assertEquals(savedArticle.getTitle(), "jOOQ examples");
assertEquals(savedArticle.getDescription(), "A few examples of jOOQ CRUD operations");
assertEquals(savedArticle.getAuthorId().intValue(), 1);
}
@Test
public void givenArticleRecord_whenGetOnlyTitles_thenValidOneValue() {
// given
ArticleRecord article = article();
save(article);
// when
Result<Record> articles = getFields(
context,
Article.ARTICLE,
Article.ARTICLE.TITLE
);
// then
assertEquals(articles.size(), 1);
Record record = articles.get(0);
ArticleRecord savedArticle = record.into(Article.ARTICLE);
assertNull(savedArticle.getId());
assertEquals(savedArticle.getTitle(), "jOOQ examples");
assertNull(savedArticle.getDescription());
assertNull(savedArticle.getAuthorId());
}
@Test
public void givenArticleRecord_whenGetOne_thenValidRecord() {
// given
ArticleRecord article = article();
save(article);
// when
ArticleRecord savedArticle = getOne(
context,
Article.ARTICLE,
Article.ARTICLE.ID.eq(1)
);
// then
assertEquals(savedArticle.getId().intValue(), 1);
assertEquals(savedArticle.getTitle(), "jOOQ examples");
assertEquals(savedArticle.getDescription(), "A few examples of jOOQ CRUD operations");
assertEquals(savedArticle.getAuthorId().intValue(), 1);
}
@Test
public void givenArticleRecord_whenUpdateById_thenChangedValuesDbTable() {
// given
ArticleRecord article = article();
save(article);
HashMap<Field<String>, String> updateFields = new HashMap<>();
updateFields.put(Article.ARTICLE.TITLE, "new title");
updateFields.put(Article.ARTICLE.DESCRIPTION, "new description");
// when
update(
context,
Article.ARTICLE,
updateFields,
Article.ARTICLE.ID.eq(1)
);
// then
ArticleRecord updatedArticle = getOne(
context,
Article.ARTICLE,
Article.ARTICLE.ID.eq(1)
);
assertEquals(updatedArticle.getId().intValue(), 1);
assertEquals(updatedArticle.getTitle(), "new title");
assertEquals(updatedArticle.getDescription(), "new description");
assertEquals(updatedArticle.getAuthorId().intValue(), 1);
}
@Test
public void givenArticleRecord_whenUpdate_thenChangedValuesDbTable() {
// given
ArticleRecord article = article();
save(article);
article.setTitle("new title");
article.setDescription("new description");
// when
update(article);
// then
ArticleRecord updatedArticle = getOne(
context,
Article.ARTICLE,
Article.ARTICLE.ID.eq(1)
);
assertEquals(updatedArticle.getId().intValue(), 1);
assertEquals(updatedArticle.getTitle(), "new title");
assertEquals(updatedArticle.getDescription(), "new description");
assertEquals(updatedArticle.getAuthorId().intValue(), 1);
}
@Test
public void givenArticleRecord_whenDelete_thenEmptyDbTable() {
// given
ArticleRecord article = article();
save(article);
// when
delete(article);
// then
Result<Record> articles = getAll(
context,
Article.ARTICLE
);
assertTrue(articles.isEmpty());
}
@Test
public void givenArticleRecord_whenDeleteById_thenEmptyDbTable() {
// given
ArticleRecord article = article();
save(article);
// when
delete(
context,
Article.ARTICLE,
Article.ARTICLE.ID.eq(1)
);
// then
Result<Record> articles = getAll(
context,
Article.ARTICLE
);
assertTrue(articles.isEmpty());
}
private ArticleRecord article() {
ArticleRecord article = context.newRecord(Article.ARTICLE);
article.setId(1);
article.setTitle("jOOQ examples");
article.setDescription("A few examples of jOOQ CRUD operations");
article.setAuthorId(1);
return article;
}
}

View File

@ -39,6 +39,7 @@
<module>java-jpa-2</module> <!-- long running -->
<module>java-mongodb</module> <!-- long running -->
<module>jnosql</module> <!-- long running -->
<module>jooq</module>
<module>jpa-hibernate-cascade-type</module>
<module>liquibase</module>
<module>orientdb</module>