简体   繁体   English

当使用EntityManager的createQuery()和find()方法时?

[英]When use createQuery() and find() methods of EntityManager?

I would like to know the difference between on these methods. 我想知道这些方法之间的区别。

When use the createQuery() and find() methods of EntityManager ? 使用EntityManager的createQuery()find()方法时?

What the advantages about each of them ? 他们每个人的优势是什么?

Thank you for answer me. 谢谢你的回答。

You use find when you want to look up an entity by primary key. 当您想要通过主键查找实体时,可以使用find That means you know exactly what you're looking for, you just want to pull it out of the database. 这意味着你确切地知道你在寻找什么,你只想把它从数据库中拉出来。

You use createQuery when you want to find entities using criteria or if you want to use a JPQL statement to define what you get back. 如果要使用条件查找实体,或者如果要使用JPQL语句定义返回的内容,则可以使用createQuery So you would use the query when you want to get an entity or collection of entities matching some conditions. 因此,当您想要获得符合某些条件的实体或实体集合时,您将使用该查询。

The createQuery method allows you to create a JPQL statement that will be executed. createQuery方法允许您创建将要执行的JPQL语句。 The JPQL statement allowed is much more dynamic than the one executed by find . 允许的JPQL语句比find执行的语句更具动态性。 For example given the following table: 例如,给出下表:

create table CAT(
   cat_id integer,
   cat_name varchar(40)
)

You could execute a query to find the cat by name. 您可以执行查询以按名称查找cat。

entityManager.createQuery("select c from Cat c where c.name = :name");

The find method only allows you to retreive an object using its primary key. find方法仅允许您使用其主键检索对象。 So to use the find method for the above table: 所以要使用上表的find方法:

entityManager.find(Cat.class, new Integer(1));

In a nutshell, createQuery allows you to retrieve entities in a more dynamic fashion, while find limits you to searching for an entity with a known id. 简而言之, createQuery允许你在一个更加时尚动感检索实体,而find限制你搜索与已知ID的实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JPA EntityManager:“查找”与“ createQuery”和“ getResultList” - JPA EntityManager: 'find' vs. 'createQuery' and 'getResultList' 何时将 EntityManager.find() 与 EntityManager.getReference() 与 JPA 一起使用 - When to use EntityManager.find() vs EntityManager.getReference() with JPA javax.persistence.EntityManager.createQuery:何时使用/何时不使用resultClass - javax.persistence.EntityManager.createQuery: when with / when without resultClass enitityManager.find和entityManager.createQuery有什么区别? - What is the difference between enitityManager.find and entityManager.createQuery? 使用IN的JPA EntityManager createQuery()不起作用 - JPA EntityManager createQuery() with IN not working JPA EntityManager createQuery()错误 - JPA EntityManager createQuery() error entityManager.createQuery()上的AbstractMethodError - AbstractMethodError on entityManager.createQuery() entityManager.createQuery()抛出NullPointerException - entityManager.createQuery() throwing NullPointerException java.lang.NoSuchMethodError:使用jpa / hibernate时javax.persistence.EntityManager.createQuery - java.lang.NoSuchMethodError: javax.persistence.EntityManager.createQuery when using jpa / hibernate EntityManager createQuery 上的 QuerySyntaxException 休眠无效路径 - QuerySyntaxException hibernate invalid path on EntityManager createQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM