public interface PKHolder<PKType> {PKType getPrimaryKey();}
--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hello Christian,Yes, this should be possible. I suspect your marker interface should be generic with the primary key type:public interface PKHolder<PKType> {PKType getPrimaryKey();}
Now, use a generator strategy to add the interface to the "implements" section of each relevant POJO:
There is also the possibility to add a configurative generator strategy, but that won't be sufficient in your use-case as you probably need to introspect the primary key type:
Now the last step is to actually implement the marker interface and the getPrimaryKey() method. This can be done by generating a custom code section:
I think with the above pointers, it should be possible to achieve what you want to do. Please, let me know if you encounter any issues or if some detail is unclear.
public interface IdentifiableDomain<PK extends Serializable> {
PK getPrimaryKey();
}
public class DomainGenerator extends JavaGenerator {
@Override
protected void generatePojoClassFooter(TableDefinition table, JavaWriter out) {
UniqueKeyDefinition primaryKey = table.getPrimaryKey();
}
}
Hello Christian,There are two steps to follow.First, you have to introspect the primaryKey's columns via UniqueKeyDefinition.getKeyColumns()Then, you have to find out the applicable Java type via ColumnDefinition.getType().getJavaType(). There you should have everything you need. Perhaps, some debugging through the code generator might also help to find out what API elements will return the most appropriate information. You could even make use of JavaGenerator's internals in order to generate imports for such types.
I'm not sure how you intend to model composite primary keys. It could be done e.g. with a jOOλ Tuple[N] type (see https://github.com/jOOQ/jOOL)
dslContext.selectFrom(rTable).where(pkField.eq(source.primaryKey))
Hi Lukas,thanks for your patience...
I'm not sure how you intend to model composite primary keys. It could be done e.g. with a jOOλ Tuple[N] type (see https://github.com/jOOQ/jOOL)Yep, that's the problem. First, I thought about solving it the JPA-way. But then again, this is not doable: Having a(n embedded) FooBarPk doesn't help when you want to do something like:
dslContext.selectFrom(rTable).where(pkField.eq(source.primaryKey))
Actually, in DAOImpl you use getId() which is generated via your generator. I guess it also works for composite primary keys, right? Is it possible to use this implementation also to implement getPrimaryKey()?
return row(pk).equal((Record) id);
Hi Lukas,
one quick question. Something I saw in your DAOImpl class: you cast a T to a Record (private equal method).
return row(pk).equal((Record) id);
Is this really safe and why? Maybe there's something I don't understand.
It's safe because the implicit contract on the type <T> is for:- Single column primary keys to be of the type T of the column- Composite primary keys to be of the type Record[N]<T1, T2, ..., T[N]>It's a contract, but it cannot be easily expressed in code. The code generator will adhere to this contract. Custom DAO implementations might not, but as many other jOOQ APIs, DAO is not meant for random implementation by users, so it's... "safe" :)