Available in versions: Dev (3.22) | Latest (3.21) | 3.20 | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12
Creating new results and records
Supported by ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
The DSLContext.newResult() method creates a new, empty Result, which is a java.util.List that can be populated with records created from DSLContext.newRecord(). When working with generated code, the record type of a table can be used:
// Create an empty result of a generated table's record type
Result<BookRecord> books = create.newResult(BOOK);
// Create a new record and add it to the result
BookRecord book = create.newRecord(BOOK);
book.setId(1);
book.setAuthorId(1);
book.setTitle("1984");
books.add(book);
// Records can also be pre-filled from POJOs or other objects, see Record.from(Object)
books.add(create.newRecord(BOOK, myBook));
Records created this way have their changed flags set for all values that were set, which means that a subsequent UpdatableRecord.store() call will execute an INSERT statement. See the section about CRUD with UpdatableRecords for more details.
Ad-hoc results and records
Results and records are not tied to tables. Just like with ad-hoc projections in SELECT statements, a result or record can be created from an arbitrary set of Field expressions, producing Result<Record1<T1>> up to Result<Record22<T1, ..., T22>> types for up to 22 fields, or Result<Record> for more fields:
// Using generated column references
Result<Record2<Integer, String>> result = create.newResult(AUTHOR.ID, AUTHOR.LAST_NAME);
result.add(create.newRecord(AUTHOR.ID, AUTHOR.LAST_NAME).values(1, "Orwell"));
result.add(create.newRecord(AUTHOR.ID, AUTHOR.LAST_NAME).values(2, "Coelho"));
// Without code generation, using plain SQL column references
Field<Integer> id = field(name("ID"), SQLDataType.INTEGER);
Field<String> title = field(name("TITLE"), SQLDataType.VARCHAR);
Result<Record2<Integer, String>> titles = create.newResult(id, title);
titles.add(create.newRecord(id, title).values(1, "1984"));
// With more than 22 fields, or with a dynamic set of fields
List<Field<?>> fields = ...;
Result<Record> dynamic = create.newResult(fields);
Record record = create.newRecord(fields);
record.set(id, 1);
dynamic.add(record);
Records of user-defined types can be created in a similar way, using DSLContext.newRecord(UDT).
Attachment
Results and records created by a DSLContext are attached to its Configuration by default. This is relevant for operations like UpdatableRecord.store(), but also for any configuration dependent behaviour, such as data type conversion or formatting.
Feedback
Do you have any feedback about this page? We'd love to hear it!