【问题标题】:Generate classes from Postgress database with multiple schemas using Cayenne使用 Cayenne 从具有多个模式的 Postgres 数据库生成类
【发布时间】:2018-05-28 11:29:26
【问题描述】:

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

引用schema_a.my_user的表schema_b.booking:

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

【问题讨论】:

    标签: java postgresql apache-cayenne


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!嗯,我需要定期同步;我将尝试使用 Modeler 来推断关系。只是为了澄清,4.1.M2将能够生成跨模式关系?
    • 不客气!我真的认为它会,这是要跟踪的问题:issues.apache.org/jira/browse/CAY-2440
    猜你喜欢
    • 2011-04-09
    • 1970-01-01
    • 2019-01-09
    • 2011-02-06
    • 2019-02-16
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多