当你途径我的盛放 2015-11-01 12:20 采纳率: 55.6%
浏览 14012
已采纳

Spring data Jpa批量更新操作

注释掉的方法,速度太慢6000条要四五分钟,没注释掉的方法出现空指针异常,不知道怎么解决。。。。。求大神
异常:

 Exception in thread "main" java.lang.NullPointerException
    at service.UserServiceImpl.updateUser(UserServiceImpl.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at $Proxy23.updateUser(Unknown Source)
    at test.Login.update(Login.java:49)
    at test.Login.main(Login.java:26)

login.java

    public static void  update(UserService userService) throws UserNotFound
    {
//      
//      for(int i=497;i<6497;i++)
//      {
//          User u=new User();
//          u.setId(i);
//          u.setName("ds");
//          userService.updateUser(u);
//      }

        List<User> user=new ArrayList<User>();
        for(int i=497;i<6497;i++)
        {
            User u=new User();
            u.setId(i);
            u.setName("d");
            user.add(u);
        }
        userService.updateUser(user);

    }


UserServiceImpl

    @Autowired
    private UserRepository userRepository;
    private EntityManager em;


    //修改用户
    @Transactional
    public void updateUser(List<User> user) throws UserNotFound {
        //      根据Id查找user
//      User userUpdate=userRepository.findOne(user.getId());
//      if(userUpdate==null)
//          throw new UserNotFound();
//
//      if(userUpdate.getName()!=null)
//          userUpdate.setName(user.getName());
//  
//      userRepository.save(userUpdate);
//      
//      
        for(int i=0;i<user.size();i++)
        {
            em.merge(user.get(i));
            if(i%30==0)
            {
                em.flush();
                em.clear();

            }

        }
    }

  • 写回答

1条回答

  • 当你途径我的盛放 2015-11-01 12:46
    关注

    @PersistenceContext

    private EntityManager em;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建