Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage @NamedNativeQuery and schema

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 ?

like image 279
Olivier J. Avatar asked Dec 19 '12 21:12

Olivier J.


People also ask

What does NativeQuery mean?

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.

What is the difference between JPQL and native query?

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.

Where is NamedNativeQuery defined?

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.

What is Createnativequery in JPA?

Imagine having a tool that can automatically detect JPA and Hibernate performance issues.


1 Answers

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

like image 81
James Avatar answered Sep 21 '22 11:09

James