更新JPA实体中的所有对象 [英] Update all objects in JPA entity

查看:257
本文介绍了更新JPA实体中的所有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新ProfileEntity中的所有4000个对象,但出现以下异常:

I'm trying to update all my 4000 Objects in ProfileEntity but I am getting the following exception:

javax.persistence.QueryTimeoutException: The datastore operation timed out, or the data was temporarily unavailable.

这是我的代码:

public synchronized static void  setX4all() 
{

    em = EMF.get().createEntityManager();

    Query query = em.createQuery("SELECT p FROM ProfileEntity p");
    List<ProfileEntity> usersList = query.getResultList();

    int a,b,x;
    for (ProfileEntity profileEntity : usersList) 
    {
        a = profileEntity.getA();
        b = profileEntity.getB();
        x = func(a,b);
        profileEntity.setX(x);

        em.getTransaction().begin();        
        em.persist(profileEntity);        
        em.getTransaction().commit();

    }

    em.close();

}

我猜想我花太长时间来查询ProfileEntity中的所有记录. 我该怎么办?

I'm guessing that I take too long to query all of the records from ProfileEntity. How should I do it?

  • 我正在使用Google App Engine,因此无法进行UPDATE查询.

已编辑18/10
在这2天里,我尝试过:
按照Thanos Makris的建议使用Backends,但是陷入了僵局.您可以在此处看到我的问题.
阅读Map-Reduce上的DataNucleus建议,但确实迷路了.

Edited 18/10
In this 2 days I tried:
using Backends as Thanos Makris suggested but got to a dead end. You can see my question here.
reading DataNucleus suggestion on Map-Reduce but really got lost.

我正在寻找一个不同的方向.由于我只会执行一次此更新,因此也许我可以每200个对象左右进行一次手动更新.
是否可以查询前200个对象,之后查询第二200个对象,依此类推?

I'm looking for a different direction. Since I only going to do this update once, Maybe I can update manually every 200 objects or so.
Is it possible to to query for the first 200 objects and after it the second 200 objects and so on?

推荐答案

鉴于您的情况,我建议运行本机更新查询:

Given your scenario, I would advice to run a native update query:

 Query query = em.createNativeQuery("update ProfileEntity pe set pe.X = 'x'");
 query.executeUpdate();

请注意:此处的查询字符串为SQL,即update **table_name** set ....

Please note: Here the query string is SQL i.e. update **table_name** set ....

这会更好.

这篇关于更新JPA实体中的所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆