【问题标题】:Cross-database references are not implemented in HibernateHibernate 中没有实现跨数据库引用
【发布时间】:2023-01-30 01:20:20
【问题描述】:

数据库:Postgres

在我的实体类中,我有这段代码

    @OneToMany(mappedBy = "account", fetch = FetchType.LAZY, cascade = CascadeType.ALL,
    orphanRemoval = true)
    @Where(clause = "balance_as_of = (" +
            "select max(account_balance.balance_as_of) " +
            "from \"schema-name\".account_balance " +
            "where account_balance.id = id)")
    private List<AccountBalance> balances;

在运行时,这段代码被转换为实际的 SQL 查询,即

select balances0_.account_id as account_6_1_0_, balances0_.id as id1_1_0_, balances0_.id as id1_1_1_, balances0_.account_id as account_6_1_1_, balances0_.available_balance as availabl2_1_1_, balances0_.balance_as_of as balance_3_1_1_, balances0_.incoming_balance as incoming4_1_1_, balances0_.outgoing_balance as outgoing5_1_1_ from "schema-name".account_balance balances0_ where ( balances0_.balance_as_of = (select max(account_balance.balance_as_of) from **balances0_."schema-name".account_balance** where account_balance.id = balances0_.id)) and balances0_.account_id=?

然后它抛出错误

org.postgresql.util.PSQLException: ERROR: cross-database references are not implemented: "balances0_.schema-name.account_balance"

我的期望是内部 SQL 应该根据@在哪里注释子句值。不幸的是,在运行时,它添加了一个额外的余额0_在不应该存在的模式名称前面。这个余额0_是外部 SQL 的表别名。

如果我删除“模式名称”,它会说表不存在,因为我认为它指的是公共模式。

知道如何解决这个问题吗?

【问题讨论】:

    标签: hibernate where-clause


    【解决方案1】:

    您可以创建一个隐藏数据库引用的视图,并在您的 where 子句中使用它。

    【讨论】:

      猜你喜欢
      • 2019-01-17
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多