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

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;

    点赞 打赏 评论

相关推荐 更多相似问题