javaormapache-cayenne

How do I make Apache Cayenne respect database defaults?


I have a postgres database with some defaults such as:

CREATE TABLE product (
    id serial,
    name text,
    created_at DEFAULT now(),
    price numeric DEFAULT 9.99
);

How do I make Cayenne honour those defaults when I do a context.newObject(Product.class)?

Currently I am hooking into the onPostAdd callback and then setting my defaults there, though I was wondering if that's the only way to do it? Can't I use my database defaults somehow to keep it DRY?

I am using Apache Cayenne 4.1.


Solution

  • Unfortunately onPostAdd is your best bet. This is object-relational impedance at its worst. As one of Cayenne authors I am well aware of this issue. Wish there was a DRY solution.