简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM