【问题标题】:The difference between select * where id = :id and entity manager find()select * where id = :id 和实体管理器 find() 的区别
【发布时间】:2018-09-06 22:20:52
【问题描述】:

我都知道

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

和:

EntityManager.find(Entity.class,id)

返回相同的结果,但我真的很想知道它们之间的区别。

【问题讨论】:

  • 如果实体已经加载到一级缓存中,em.find() 不会执行任何查询。

标签: hibernate jpa eclipselink hql jpql


【解决方案1】:

它们也产生相同的 SQL。

有一个discussion关于在某些情况下生成不同 SQL 的可能性,但没有发现任何不同。

区别在于它们在不同情况下如何为您提供帮助,但仅限于代码级别:

  • createQuery 需要 JPQL/HQL 才能工作。 id 列名称或实体名称的任何更改都会影响查询字符串。 find 方法不会遇到同样的问题。
  • 您需要更多样板代码才能使用 createQueryQuery 类、调用 setParameter 方法、查询字符串等)。

【讨论】:

    猜你喜欢
    • 2012-01-04
    • 1970-01-01
    • 2019-08-22
    • 2014-01-27
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    相关资源
    最近更新 更多