Named query or root query or query Which one is better in terms of performance?

Which one is better among the following (EJB 3 JPA)

//Request

but). getEntityManager (). createQuery ("select o from user o");

// Named query where findAllUser is defined at Entity level

b) getEntityManager () createNamedQuery ("User.findAllUser") ;. **

// Original request

from). getEntityManager (). createNativeQuery ("SELECT * FROM TBLMUSER");

Please explain to me which approach is better in this case?

+7
java
source share
4 answers
  • CreateQuery ()

    It should be used to create dynamic queries.

    //Example dynamic query StringBuilder builder = new StringBuilder("select e from Employee e"); if (empName != null) { builder.append(" where e.name = ?"); } getEntityManager().createQuery(builder.toString()); 
  • createNamedQuery ()

    It looks like a constant variable and reusable, and you should use it in general db calls, for example; find all users, find by id, etc.

  • createNativeQuery ()

    This is completely dependent on the database supported by the sql script.

    This is useful when a complex query is required and JPQL syntax is not supported.

    But this can affect your application and require more work if the underlying database is changed from one to another. A case like example is if your env development uses MySQL and your env products use Oracle. + And the returned result binding can be complicated if there is more than one result.

+16
source share

Named queries match queries. They are called only to allow their reuse + they can be declared in different places, for example. in class mappings, conf files, etc. (so that you can modify the request without changing the actaul code)

Custom queries are just the right queries, you have to do everything that JPA queries do for you. Linking and quoting values, etc. + They use the independent DBMP syntax (JPQL in your case), therefore changing the database system (allows saq from MySQL to Postgresql or H2) will require less work, since it is not required (not always) to overwrite your own queries.

0
source share

For me, apparently, the first two are better, that is, JPQL Queries - the second means that the object manager will compile the queries (and check them) when loading the persistence unit, while the first will only lead to errors during execution time.

You can also get support in some IDE and support object notation (for example: select b from EntityA a left join a.entityB b ) and some other oddities introduced by the object-relational mapping (e.g. collections, index, etc.) .

On the other hand, use the Original queries in the latter case in a corner JPQL example (for example, a window function, for example select id, partition by (group_id) from table )

0
source share

Internal SQL is not necessarily faster than Hibernate / JPA Query. Hibernate / JPA Query is finally also converted to SQL. In some cases, it may happen that Hibernate / JPA does not generate the most efficient statements, so native SQL may be faster - but with native SQL, your application loses portability from one database to another, so it is usually better to configure Hibernate / JPA Query display and statement HQL to create more efficient SQL statements. On the other hand, with native SQL, you do not have enough Hibernate cache - as a result, in some cases native SQL may be slower than Hibernate / JPA Query.

I don’t have performance, in most cases it doesn’t matter for performance if you load all columns or just the columns you need. When accessing the database, time is lost when searching for a string, and not when transferring data to your application. When you read only the necessary columns.

0
source share

All Articles