使用 Cayenne 从具有多个模式的 Postgres 数据库生成 类

Generate classes from Postgress database with multiple schemas using Cayenne

我有具有多个模式的 PostgreSQL 数据库,我正在使用 Apache Cayenne 生成 Java classes。问题是 cayenne 会跳过不同模式中表的外键。示例:

Table schema_b.booking 引用 schema_a.my_user:

create table schema_b.booking
(
    id bigserial not null constraint booking_pkey primary key,
    address_id integer not null constraint cde_fk references schema_b.address
    ...,
    created_by integer not null constraint abc_fk references schema_a.my_user
);

生成的 Java class 看起来像:

class Booking {
     private Long id; 
     private Address addressId; //this is OK
     private Integer createdBy; //NOT OK (Integer instead of MyUser)
}

控制台日志显示不同架构中每个 FK 的条目:

[INFO] Skip relation: 'null.schema_a.my_user.id <- null.schema_b.booking.created_by # 1' because it related to objects from other catalog/schema
[INFO]      relation primary key: 'null.schema_a'
[INFO]        primary key entity: 'null.schema_a'
[INFO]      relation foreign key: 'null.schema_b'
[INFO]        foreign key entity: 'null.schema_b'

问题是 Booking#createdBy 不是 MyUser

我搜索过 SO 和官方文档,但没有成功。有什么办法可以做到这一点?我知道另一种选择是将所有表移动到单一模式中,但这对我们的项目来说几乎不可行。

你是对的,Cayenne 只是跳过了跨模式关系。 这看起来像是一个过时的限制,应该被删除。

如果您只这样做一次,您可以在 Cayenne Modeler 中添加这些缺失的关系。如果您需要定期将 Cayenne 模型与您的数据库同步,那么它可能更难克服。 一种选择是完全跳过关系加载 (see docs),并手动创建它们(或使用 Modeler 中的 "Infer relationships" 工具)。在这种情况下,所有其他内容(表格和列)应该同步就好了。另一种选择是等待 4.1.M2 版本的 Cayenne,我相信它很快就会准备就绪(虽然没有确切的日期)