我正在关注
java.lang.NullPointerException at com.xyz.bank.dao.AccountDao.saveAccount(AccountDao.java:32)
com.xyz.bank.service.AccountService.saveNewlyCreatedCustomer(AccountService.java:65)
AccountDao的第32行是Session session = sessionFactory.openSession();
我正在尝试打开会话,但是得到了一个空指针。谁能看看我所做的配置,然后让我知道我要去哪里了。
dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.xyz.bank"></context:component-scan>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- dataSource Bean using DriverManagerDataSource to connect Database -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource"
ref="dataSource"/>
<property name="packagesToScan"
value="com.xyz.bank"/>
<property name="annotatedClasses">
<list>
<value>com.xyz.bank.bean.Account</value>
<value>com.xyz.bank.bean.Customer</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">
create-drop
</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.H2Dialect
</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:~/test"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
AccountDao
@Repository
public class AccountDao {
@Autowired
SessionFactory sessionFactory;
public List<Customer> customers;
public String saveAccount(Account account) {
List<Account> accounts = new ArrayList<>();
System.out.println(account.getBalance());
System.out.println(account.getAccountID());
boolean xyz = accounts.add(account);
System.out.println(xyz);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.save(account);
transaction.commit();
session.close();
return "Saved";
}
}