JOOQ Code generation could take into account composite unique constraints - jooq

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/

Related

JOOQ does not create insert statements in DAO

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)

Extending a JOOQ Table class

I have a 'document' table (very original) that I need to dynamically subset at runtime so that my API consumers can't see data that isn't legal to view given some temporal constraints between the application/database. JOOQ created me a nice auto-gen Document class that represents this table.
Ideally, I'd like to create an anonymous subclass of Document that actually translates to
SELECT document.* FROM document, other_table
WHERE document.id = other_table.doc_id AND other_table.foo = 'bar'
Note that bar is dynamic at runtime hence the desire to extend it anonymously. I can extend the Document class anonymously and everything looks great to my API consumers, but I can't figure out how to actually restrict the data. accept() is final and toSQL doesn't seem to have any effect.
If this isn't possible and I need to extend CustomTable, what method do I override to provide my custom SQL? The JOOQ docs say to override accept(), but that method is marked final in TableImpl, which CustomTable extends from. This is on JOOQ 3.5.3.
Thanks,
Kyle
UPDATE
I built 3.5.4 from source after removing the "final" modifier on TableImpl.accept() and was able to do exactly what I wanted. Given that the docs imply I should be able to override accept perhaps it's just a simple matter of an erroneous final declaration.
Maybe you can implement one of the interfaces
TableLike (and delegate all methods to a JOOQ implementation instance) such as TableImpl (dynamic field using a HashMap to store the Fields?)
Implement the Field interface (and make it dynamic)
Anyway you will need to remind that there are different phases while JOOQ builds the query, binds values, executes it etc. You should probably avoid changing the "foo" Field when starting to build a query.
It's been a while since I worked with JOOQ. My team ended up building a customized JOOQ. Another (dirty) trick to hook into the JOOQ library was to use the same packages, as the protected identifier makes everything visible within the same package as well as to sub classes...

Default primary key setter and getter generated by Service Builder

I'm working with Liferay. When create an entity, we define one column as the primary key. Then I look at the entity model, I see some methods setPrimaryKey, getPrimaryKey as well as the setter and getter of the column that we defined as the primary key before. What is the difference of these methods? Do we need to use both methods once we add new entity into the table or just one
Assuming that your project is built by many people. Some one designs entity and you only code view page. When you want to get or set primary key of entity but you don't know what column. If don't have setPrimaryKey or getPrimaryKey methods, do you look up in the code to find what column is primary key?

Entity Framework 5 Code first custom key logic?

I have an entity (several, actually) that I want to have assigned a key value based on custom logic when they're created. I know I could do it in the entity constructor, but ideally I'd like to do it as part of the data context logic for when a new entity is created.
I found how to turn off autogeneration, but what I'd rather do is replace auto generation w/ my own logic, ideally handled by the C# code (I"ve seen techniques for doing it via stored procs also, that I'd rather not use).
Is this even possible to do centrally?
Autogeneration happens on the server side and is not done by the EF. This is a setting on a table key column. So EF does not generate any keys - if autogeneration is turned on this is the database that generates keys, if autogeneration is off this is the user that is responsible for generating keys. If you don't want to generate keys when saving changes you may want to override SaveChanges and generate keys for all newly added entities.

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)).

Resources