Search code examples
javasqlhibernatebulkupdatenative-sql

createQuery vs createNativeQuery, performance difference for update/delete statements


Is there a performance difference between:

entityManager.createQuery("UPDATE MyTable SET coll1 = :someValue").setParameter("someValue").executeUpdate();

and

entityManager.createNativeQuery("UPDATE MyTable SET coll1 = :someValue").setParameter("someValue").executeUpdate();

and if yes, is it high enough to use 1 approach over the another?

I am making a performance comparison between hibernate and entity framework core. In EF core this kind of thing can only be done using native SQL (well, there are third party libs) so i want to know if i should switch out all createQuery().executeUpdate() for createNativeQuery().executeUpdate() on my hibernate project.


Solution

  • As with anything of this nature, you should test on your data and your system.

    However, the createNativeQuery() interface is designed to let you invoke SQL directly, rather than going through the ORM mapping. You have a simple update statement here, so the generated SQL should be remarkably close to the native SQL.

    You are not relying on any underlying features of the database. There might be a little additional overhead in the translation via the ORM -- but you have already accepted that overhead by choosing to use an ORM.

    I would say to stick with the framework, unless testing shows that there is a noticeable loss of performance.