New versions: | Old versions: 2.6
The Factory class
Applies to ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
jOOQ exposes a lot of interfaces and hides most implementation facts from client code. The reasons for this are:
- Interface-driven design. This allows for modelling queries in a fluent API most efficiently
- Reduction of complexity for client code.
- API guarantee. You only depend on the exposed interfaces, not concrete (potentially dialect-specific) implementations.
The org.jooq.impl.Factory class is the main class from where you will create all jOOQ objects. The Factory serves two types of purposes:
- It serves as a static factory for table expressions, column expressions (or "fields"), conditional expressions and many other QueryParts.
- It implements org.jooq.Configuration, an object that configures jOOQ's behaviour when executing queries (see SQL execution for more details). Factories allow for creating SQL statements that are already "configured" and ready for execution.
With jOOQ 2.0, static factory methods have been introduced in order to make client code look more like SQL. Ideally, when working with jOOQ, you will simply static import all methods from the Factory class:
import static org.jooq.impl.Factory.*;
Note, that when working with Eclipse, you could also add the Factory to your favourites. This will allow to access functions even more fluently:
concat(trim(FIRST_NAME), trim(LAST_NAME)); // ... which is in fact the same as: Factory.concat(Factory.trim(FIRST_NAME), Factory.trim(LAST_NAME));
As any Configuration object, a Factory can be supplied with these objects:
- org.jooq.SQLDialect : The dialect of your database. This may be any of the currently supported database types (see SQL Dialect for more details)
- java.sql.Connection : An optional JDBC Connection that will be re-used for the whole lifecycle of your Factory (see Connection vs. DataSource for more details)
- java.sql.DataSource : An optional JDBC DataSource that will be re-used for the whole lifecycle of your Factory. If you prefer using DataSources over Connections, jOOQ will internally fetch new Connections from your DataSource, conveniently closing them again after query execution. This is particularly useful in Java EE or Spring contexts (see Connection vs. DataSource for more details)
- org.jooq.conf.Settings : An optional runtime configuration (see Custom Settings for more details)
// The Factory is "configured" with a Connection and a SQLDialect Factory create = new Factory(connection, dialect); // This select statement contains an internal reference to the Factory: Select<?> select = create.selectOne(); // Using the internally referenced Factory, the select statement can now be executed: Result<?> result = select.fetch();