Reputation: 17097
In hibernate, when we do, session.getNamedQuery(QUERY_NAME), we do not need to pass in the name of the hbm.xml file where the named query is stored. The name of the hbm.xml is mentioned in the cfg file.
The question is, if there are 5 hbm.xml files for named queries, and there is a name conflict with respect to the query name(i.e. same QUERY_NAME in all 5 files), how hibernate will resolve it?
Intuitively, it appears before doing session.getNamedQuery(QUERY_NAME), one should need to explicitly load the particular hbm.xml file where the query is written.
Upvotes: 0
Views: 1396
Reputation: 2016
Though this doesn't directly address the question, it's common practice to give your queries names that reflect the package and entity they deal with to avoid naming collisions. Instead of "findUser" you might have something like "com.myproject.domain.User.findByUsernameAndPassword"
Upvotes: 0
Reputation: 80176
Hibernate aggregates all the named queries across all hbm file it has loaded under one logical namespace (think of it as a Map with name as the key but with a duplicate check on the key before put(..)). So the names should be unique across a given session factory. So make sure all your named queries are unique.
Upvotes: 2
Reputation: 242686
In addition to Pangea's answer: if you want to have different version of the same query in different .hbm.xml
files and choose between them, you can do it by calling Configuration.addFile()
/Configuration.addInputStream()
when constructing the SessionFactory
(though it can be done only once during SessionFactory
initialization, so that you can't change them on the fly).
Upvotes: 0