如何让 Apache Cayenne 遵守数据库默认设置?
How do I make Apache Cayenne respect database defaults?
我有一个带有一些默认设置的 postgres 数据库,例如:
CREATE TABLE product (
id serial,
name text,
created_at DEFAULT now(),
price numeric DEFAULT 9.99
);
如何让 Cayenne 在执行 context.newObject(Product.class)
时遵守这些默认设置?
目前我正在连接 onPostAdd
回调,然后在那里设置我的默认值,但我想知道这是否是唯一的方法?我不能以某种方式使用我的数据库默认值来保留它 DRY
吗?
我正在使用 Apache Cayenne 4.1。
不幸的是,onPostAdd
是您最好的选择。这是最坏情况下的 object-relational 阻抗。作为 Cayenne 的作者之一,我很清楚这个问题。希望有一个 DRY 解决方案。
我有一个带有一些默认设置的 postgres 数据库,例如:
CREATE TABLE product (
id serial,
name text,
created_at DEFAULT now(),
price numeric DEFAULT 9.99
);
如何让 Cayenne 在执行 context.newObject(Product.class)
时遵守这些默认设置?
目前我正在连接 onPostAdd
回调,然后在那里设置我的默认值,但我想知道这是否是唯一的方法?我不能以某种方式使用我的数据库默认值来保留它 DRY
吗?
我正在使用 Apache Cayenne 4.1。
不幸的是,onPostAdd
是您最好的选择。这是最坏情况下的 object-relational 阻抗。作为 Cayenne 的作者之一,我很清楚这个问题。希望有一个 DRY 解决方案。