0

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.

gurpreet-
  • 509
  • 8
  • 18

1 Answers1

1

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.

andrus_a
  • 2,528
  • 1
  • 16
  • 10
  • No worries. I have created a [gist that does pickup default values](https://gist.github.com/gurpreet-/8062ba4fff9e43797ecc40bc4852e74d), at the expense of extra queries. Thanks for Cayenne, it's great! – gurpreet- Mar 08 '18 at 00:50