Java – hibernate named queries and their performance advantages?

As stated in the hibernate document, the purpose of named queries is to purge HQL from different locations in the project to a single location in an XML (in the case of declared methods) This means that there is no need to recompile when the query is modified, but the reload of the session factory is necessary, which means that in most cases, the server starts when the query object is cached But in the case of annotations, I need to define named queries at the entity level So here you need to compile again My problem is that query help is also mentioned in terms of performance Here is my understanding: –

1) When I use named queries, only the query objects are cached in the L2 cache When I say just query objects, this means that only query syntax is cached, not query results Am I right? If its rights may only be useful in the case of HQL becoz, we can avoid converting HQL into native queries. Each query is triggered and has some good time performance

However, if we use native SQL, named queries do not provide this advantage because no conversion occurs in this case

Therefore, the main advantage of named query is to make the central repository of SQLs Yes, in the case of HQL, it can save us some translation time for native SQL, but remember that query objects will be used in the JVM life cycle and will consume some memory So some people trade here

Solution

Named queries have two small advantages:

>Check their syntax when creating session factories so that applications fail quickly when errors occur (this may indicate that your application lacks some unit tests) > they can be accessed and used from multiple places (this may indicate a design problem anyway)

They also have a disadvantage: when using named queries to read or debug code, you cannot immediately view the executing query without searching its definition

The rest really don't matter:

>Compared with the cost of actually executing queries, the cost of converting HQL queries to SQL is negligible > the memory cost of caching queries is very small Remember, hibernate needs to have all entity metadata in memory anyway

I prefer to define queries in the code that uses it and unit test them This makes the code more readable and powerful

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>