注釋掉的方法,速度太慢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();
}
}
}
@PersistenceContext
private EntityManager em;