有谁能解释一下JPA's EntityManager的以下方法的区别吗?
createQuery()
createNamedQuery()
createNativeQuery()
并向我解释在什么情况下我们应该使用哪种方法?
发布于 2015-11-19 12:04:06
发布于 2015-11-19 09:25:59
下面是createNamedQuery的一个示例,它使用@NamedQuery:
@PersistenceContext
public EntityManager em;
...
customers = em.createNamedQuery("findAllCustomersWithName")
.setParameter("custName", "Smith")
.getResultList();
- Low level access, which means that you can optimize and handle the mapping by yourself; with SQL you actually access the database table while with JPQL you access the entity objects;
- Maybe you do not want to learn JPQL if you already know SQL
- You already have the queries written in SQL, and do not have resources/time to port them to JPQL
欲了解更多详情,请访问以下链接:
发布于 2017-11-10 06:32:39
CreateQuery
用于创建JPQLqueries
createNamedQuery
用于在映射文件或注释中定义具有名称的查询,例如:obj.createNamedQuery("entity.findbycode",Something.class)
createNativeQuery
用于执行本机/纯SQL查询。
https://stackoverflow.com/questions/33798493
复制相似问题