I found another solution without spending as much memory as generating one class per KEY . I am generating one class that looks something like this:
public class References { // First, initialise all unique keys public static final UniqueKey<TAuthorRecord> SysPk_14655 = createUniqueKey(TAuthor.T_AUTHOR, TAuthor.ID); // Then initialise all foreign keys public static final Reference<TBookRecord, TAuthorRecord> SysFk_14666 = createReference(SysPk_14655, TBook.T_BOOK, TBook.AUTHOR_ID); public static final Reference<TBookRecord, TAuthorRecord> SysFk_14667 = createReference(SysPk_14655, TBook.T_BOOK, TBook.CO_AUTHOR_ID); // Factory method for unique keys protected static <R extends Record> UniqueKey<R> createUniqueKey(Table<R> table, TableField<R, ?>... fields) { // Factory method for foreign keys referencing unique keys protected static <R extends Record, U extends Record> Reference<R, U> createReference(UniqueKey<U> key, Table<R> table, TableField<R, ?>... fields) { }
Actual tables from the generated table classes can reference and use the above keys. I looked at the JPA annotations suggested by BobG in one of his comments. But I did not find them very useful for description:
- Multi-field keys (
@IdClass needs a type as a parameter, and I want to avoid this type) - Multi-field links (how to do this?)
- Multiple links from one table to another using different keys
- Unique keys that share many properties with a primary key.
Some comments mention why I should create such a generator, because there are many installed frameworks. I am doing this for http://www.jooq.org . And I feel that jOOQ fills the gap in today's database abstraction capabilities.
source share