JOOQ does not create insert statements in DAO - jooq

I have a table with two columns (both of type text) one of which is a primary key. Other is not null. I use jooq with following generate section:
<generate>
<interfaces>true</interfaces>
<daos>true</daos>
<fluentSetters>true</fluentSetters>
</generate>
The generated DAO does not contain insert methods. I don't see any warnings either.
I am using postgree DB.

There's no need to generate insert methods. The generated DAO classes extend DAOImpl, which already contains generic implementations, such as DAOImpl.insert(P)

Related

Find Unique constraint on a column using jooq

I have a service which consumes the database meta. It consumes the table names and alongwith it the respective column names. Now I can see there is a method called nullable() which can be used to check if the column is nullable or not. Similarly, i want to check if the column allows only unique values or not
Version used: 3.14.15
How to read database meta data with jOOQ
The API to use for this kind of schema introspection is org.jooq.Meta, which you can access via DSLContext.meta(). By default, it is backed by the JDBC DatabaseMetaData API, but you can replace that default to work with any of these instead:
Generated code
Interpreted DDL
XML
You can do that by passing a MetaProvider to your Configuration.
Once you reach a Table, do check these methods:
Table.getKeys() (primary keys and unique keys)
Table.getUniqueKeys() (unique keys only)
Table.getPrimaryKey() (primary key only)

JOOQ Code generation could take into account composite unique constraints

Jooq - 3.13
We are using Jooq Codegen to generate using the org.jooq.meta.extensions.ddl.DDLDatabase generation strategy.
When I have a table ex: Employee with a unique key constraint on say employee_number (this column is not a primary key) it generates a very useful method fetchOneByEmployeeNumber however I cannot say the same about composite keys.
If there is a composite unique key on (department_id, employee_number) then I'd like a method findOneByDepartmentIdEmployeeNumber(Long departmentId, Long employeeNumber). Is this possible? (Obviously I can roll my own method for this purpose but we have quite a few of these constraints and auto generated methods will be very helpful)
This isn't being done right now for DAOs in jOOQ's code generator. I've created a feature request for this: https://github.com/jOOQ/jOOQ/issues/10597
As a workaround, you can extend jOOQ's JavaGenerator::generateDaoClassFooter method to generate your own, see: https://www.jooq.org/doc/latest/manual/code-generation/codegen-custom-code/

Can jOOQ object mapping configured ensure all fields of POJO are set?

I'm mapping jOOQ results into POJOs.
I'd like to avoid having columns of the result not being mapped because of a typo/mismatch between field name and column name.
Is there a way to configure jOOQ to verify each field of the POJO is properly set ?
This cannot be done out of the box, but you can implement a custom RecordMapperProvider that implements the desired checks:
https://www.jooq.org/doc/latest/manual/sql-execution/fetching/pojos-with-recordmapper-provider

How can I obtain the database schema from an existing ActiveRecord.cs file?

I have been given the source code for an existing project that uses SubSonic ORM. My (limited!) understanding is that SubSonic generates code by reverse-engineering the existing database. Unfortunately I don't have the database that was used for this project.
I do have the ActiveRecord.cs file from the last time it was compiled. How could I work out the database schema so I can reproduce the database?
This sounds like SubSonic 3. Here are a couple places to get you started based on me looking through my ActiveRecord.cs file. You might want to create a small database yourself, run SubSonic on it, and see what gets generated in ActiveRecord.cs.
Inside your ActiveRecord.cs file, you'll find one partial class per table. The partial class will inherit from IActiveRecord and will likely be the name of the table.
Inside the class, you'll find a function called "KeyName()" which will return your primary key column name for the table. SubSonic requires a primary key for tables it processes and generates code for.
Look for a region named " Foreign Keys ". If this table has foreign keys, you'll find a property corresponding to each foreign key, something like "public IQueryable OtherTableNames". So this table should have a column named something like "OtherTableNameID"; check the generated partial class for the foreign key table to be sure.
Immediately below the foreign key region, you'll find properties for the non-foreign key columns of this table. You can somewhat guess at the data types of the columns from the property data types (e.g. string might be a char(x) or a varchar(x)).

ExecuteJoinedDataSet but with Where()

I did some searching and see that ExecuteJoinedDataSet will not work with the Where clause in 2.1. If I want to query a table with WHERE, but want the FK objects values to be bindable is the easiest way to just create a custom class(my table has tons of FK references).
Could you give us an example of what kind of query you are trying to write? If you are just trying to return a DataTable without creating a custom class just write your query and use the ExecuteReader which Returns an IDataReader. The IDataReader is bindable and if you need more you can just load it into a DataTable.

Resources