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

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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失