简体   繁体   中英

Cross-database references are not implemented in Hibernate

DB: Postgres

In my entity class I have this code

    @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;

During runtime, this code is converted to actual SQL query which is

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=?

Then it throws the error

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

My expectation was the inner SQL should generate correctly based on the @Where annotation clause value. Unfortunately, during runtime, it added an additional balances0_ in front of the schema name which should not be there. This balances0_ is the table alias of the outer SQL.

If I remove the "schema-name", it says table does not exist as I think it was referring to the public schema.

Any idea how to solve this issue?

You could create a view that hides the database reference and use that in your where clause.

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