报错:Spring整合Hibernate java.lang.NullPointerException

11 篇文章 0 订阅
Exception in thread "main" java.lang.NullPointerException
at com.ye.service.impl.AddressServiceImpl.addAddress(AddressServiceImpl.java:32)
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:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy9.addAddress(Unknown Source)
at com.ye.test.AddressTest.add(AddressTest.java:38)

at com.ye.test.AddressTest.main(AddressTest.java:27)


原因:持久化类使用了关联属性,但是测试中没被关联的属性。

代码:

public static void addAccout(){
		User user=hibernateDAO.findUserById(1);
		Accout accout1=new Accout();
		accout1.setActNo("123");
		accout1.setBal(2000.0);
		accout1.setUser(user);
		hibernateDAO.addAccout(accout1);
		Accout accout2=new Accout();
		accout2.setActNo("456");
		accout2.setBal(3000.0);
		//设置单向多对一关联关系
		accout2.setUser(user);
		hibernateDAO.addAccout(accout2);
	}
如果将上面代码中的hibernateDAO.addAccout(accout1);删除,就会报错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值