Manage @NamedNativeQuery and schema

∥☆過路亽.° 提交于 2019-12-01 20:20:53

问题


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 ?


回答1:


Excerpts from documentation :

  • NamedNativeQuery : Specifies a named native SQL query. Query names are scoped to the persistence unit.
  • NamedQuery : Specifies a static, named query in the Java Persistence query language. Query names are scoped to the persistence unit.

It isn't specified directly that NamedNativeQuery is static, but both are same scoped & can't be altered afterwards & it's the desired behaviour.

Named queries are mean to be accessed by multiple modules - application wide, identified by unique name, so they are static & constant. You can try building a query string dynamically & can create a native query from it, instead of named native query.




回答2:


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



来源:https://stackoverflow.com/questions/13961855/manage-namednativequery-and-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!