I have many EntityManager
, one per schema that I have (I use entity-mappings
file to map EMs with schemas). It works.
When I use @NamedQuery
it's working like a charm but when I use @NamedNativeQuery
schema is not used. I have to qualify with it SELECT foo FROM schema.table
.
Is it the right behaviour ?
I think it's not possible to parameter @NamedNativeQuery
to dynamically pass schema (I believe only columns can be dynamics not tables or schemas or anything else) so how can I use @NamedNativeQuery
with dynamic schema please ?
NativeQuery. Native query refers to actual sql queries (referring to actual database objects). These queries are the sql statements which can be directly executed in database using a database client.
JPQL is the most commonly used query language with JPA and Hibernate. It provides an easy way to query data from the database. But it supports only a small subset of the SQL standard, and it also does not support database-specific features. If you want to use any of these features, you need to use a native SQL query.
Named SQL queries are defined using the @NamedNativeQuery annotation. This annotation may be placed on any entity and defines the name of the query as well as the query text. Like JPQL named queries, the name of the query must be unique within the persistence unit.
Imagine having a tool that can automatically detect JPA and Hibernate performance issues.
Prefix your table name with "{h-schema}", e.g.SELECT foo FROM {h-schema}table
(courtesy of getting hibernate default schema name programmatically from session factory?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With