简体   繁体   English

JPA是否像在代码中使用实体那样在JPQL中加载实体?

[英]Does JPA load entities in JPQL, as it does when using entities in code?

I have a two tables: Person and House , the mapping is one to one. 我有两个表: PersonHouse ,映射是一对一的。

Now I have to assign the address of Person and House (which can be different) to the same address. 现在,我必须将“ Person和“ Houseaddress (可以不同)分配给相同的地址。

There are more than 5000 records. 有超过5000条记录。 Which will be faster? 哪个会更快? Using Code to update the entities one by one, eg 使用代码一一更新实体,例如

for (id : Ids) {
    Person person = PersonDAO.find(id);
    person.setAddress ("abc");
}

and then doing same with House ; 然后对House做同样的事情;

Or should I use JPQL to update both in two different queries, eg 还是我应该使用JPQL在两个不同的查询中同时更新两者,例如

UPDATE Person p SET p.Address = "abc" WHERE ID IN(.....ID QUERY)

My question is what will be faster? 我的问题是什么会更快? Will the update using JPQL have the same performance, same as that in code? 使用JPQL进行的更新是否具有与代码中相同的性能? Or should I use native query to NOT load the entities, as I only want performance. 还是我应该使用本机查询来不加载实体,因为我只想要性能。

Using the query will be faster (and much more memory efficient), as the query provider will translate the JPQL query to native SQL. 使用查询将更快(并且内存效率更高),因为查询提供程序会将JPQL查询转换为本机SQL。 Also, if you use entities directly, the number of queries made against the database will be siginificantly higher (one select and update for each and every row). 同样,如果直接使用实体,则对数据库的查询数量将大大增加(每行一次选择并更新)。

The native query will be faster, as it doesn't have to translate anything. 本机查询将更快,因为它无需翻译任何内容。

If you want it to be even faster, you can use a PreparedStatement . 如果您希望它更快,可以使用PreparedStatement With the .addBatch() method you add the query to the batch, and with the executeBatch() method you will execute the full batch, minimizing the amount of times being switched between user and kernel mode. 使用.addBatch()方法,您可以将查询添加到批处理中,而使用executeBatch()方法,您将执行完整的批处理,从而最大程度地减少了在用户和内核模式之间切换的时间。

暂无
暂无

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

相关问题 Hibernate是否允许加载/保存链接的实体? - Does Hibernate allow to load/save linked entities? JPA: select 在多态实体中使用 JPQL、eclipselink 并使用多个向下转换加入 inheritance - JPA: select in polymorphic entities with JPQL, eclipselink and joined inheritance using multiple downcast Spring 数据 Jpa + Spring 使用@Query(本机和 JPQL)的投影为相关实体返回 Z37A6259CC0C1DAE29BDZ9A768 - Spring Data Jpa + Spring Projections using @Query (native and JPQL) returns null for related entities 通过使用Criteria API和JPQL找到的实体是否自动在JPA的持久性上下文中找到? - Are Entities, which was found by using the Criteria API and JPQL, automatically in the Persistence context of JPA? 为什么@Transactional 隔离级别在使用 Spring Data JPA 更新实体时不起作用? - Why does @Transactional isolation level have no effect when updating entities with Spring Data JPA? 当实体之间没有关系映射时,在JPA中联接两个表 - Joining two tables in JPA when the entities does not have relation mapping between them 当您有多个实体时,JPA @Version 注释如何工作 - How does JPA @Version annotation works when you have multiple entities 是否有 JPA/JPQL 查询来搜索在 spring 中作为 JSON 传递的实体子集? - Is there a JPA / JPQL query to search for a subset of entities passed as JSON in spring? JPA缓存如何刷新外部修改的实体? - How does JPA cache refreshes entities externally modified? JPA将其实际数据实体存储在哪里? - Where does JPA store its actual data entities?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM