From 2011118290c80a7f1e8d7ff885037db54d64bb56 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Sat, 25 Aug 2012 17:27:52 +0200 Subject: [PATCH] [#1657] Reorganise the manual (159 / 174) --- .../src/main/resources/manual-2.5.xml | 105 +++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/jOOQ-website/src/main/resources/manual-2.5.xml b/jOOQ-website/src/main/resources/manual-2.5.xml index 0863563daf..77cade3d57 100644 --- a/jOOQ-website/src/main/resources/manual-2.5.xml +++ b/jOOQ-website/src/main/resources/manual-2.5.xml @@ -7796,7 +7796,110 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy {
Master data and enumeration tables - + +

Enumeration tables

+

+ Only MySQL and Postgres databases support true ENUM types natively. Some other RDBMS allow you to map the concept of an ENUM data type to a CHECK constraint, but those constraints can contain arbitrary SQL. With jOOQ, you can "simulate" ENUM types by declaring a table as a "master data table" in the configuration. At code-generation time, this table will be treated specially, and a Java enum type is generated from its data. +

+ +

Configure master data tables

+

+ As previously discussed, you can configure master data tables as follows: +

+ + + + + + + [a table name] + + + [a column name] + + + [a column name] + + + [ ... ... ] + + ]]> + +

+ The results of this will be a Java enum that looks similar to this: +

+ + { + + /** + * English + */ + en(1, "en", "English"), + + /** + * Deutsch + */ + de(2, "de", "Deutsch"), + + /** + * Français + */ + fr(3, "fr", "Français"), + + /** + * null + */ + pt(4, "pt", null), + ; + + private final Integer id; + private final String cd; + private final String description; + + // [ ... constructor and getters for the above properties ] +}]]> + +

+ In the above example, you can see how the configured primary key is mapped to the id member, the configured literal column is mapped to the cd member and the configured description member is mapped to the description member and output as Javadoc. In other words, T_LANGUAGE is a table with 4 rows and at least three columns. +

+ +

+ The general contract is that there must be +

+
    +
  • A single-column primary key column of character or integer type
  • +
  • An optional unique literal column of character or integer type (otherwise, the primary key is used as enum literal)
  • +
  • An optional description column of any type
  • +
+ +

Using MasterDataTypes

+

+ The point of MasterDataTypes in jOOQ is that they behave exactly like true ENUM types. When the above T_LANGUAGE table is referenced by T_BOOK, instead of generating foreign key navigation methods and a LANGUAGE_ID Field<Integer>, a Field<TLanguage> is generated: +

+ + { + + // [...] + public static final TableField LANGUAGE_ID = + new TableFieldImpl( /* ... */ ); +}]]> + +

+ Which can then be used in the TBookRecord directly: +

+ + { + + // [...] + public TLanguage getLanguageId() { // [...] + public void setLanguageId(TLanguage value) { // [...] +}]]> + +

When to use MasterDataTypes

+

+ You can use master data types when you're actually mapping master data to a Java enum. When the underlying table changes frequently, those updates will not be reflected by the statically generated code. Also, be aware that it will be difficult to perform actual JOIN operations on the underlying table with jOOQ, once the master data type is generated. +

+