Difference between select * where id =: id and entity manager find ()

I know both

EntityManager.createQuery("select e from Entity e where e.id = :id")

and

EntityManager.find(Entity.class,id)

returns the same result, but I really want to know the difference between the two.

+4
source share
1 answer

They also generate the same SQL code.

In some cases, there was a discussion about the possibility of generating another SQL-code, but nothing was found.

The differences are how they can help you in different cases, but only at the code level:

  • createQueryrequires JPQL / HQL. Any changes to the column name idor object name will affect the query string. The method finddoes not suffer from the same problem.
  • createQuery (Query class, call setParameter method, String ..) .
+1

Source: https://habr.com/ru/post/1695455/


All Articles