Please forgive me, I'm fairly new to this and pretty well clueless. I'm trying to create a pure console app inside an already working spring web project, for testing purposes:
Here is the main class:
@Component
@Transactional
public class Test {
@Autowired
StudentDAO dao;
public static void main(final String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
Test test = ctx.getBean(Test.class);
test.print();
}
private void print() {
Student s = dao.getByID(1);
System.out.println(s.getFirstname());
}
}
StudentDAO:
public interface StudentDAO {
public void add(Student s);
public void delete(Integer id);
public ArrayList<Student> getAll();
public Student getByID(Integer id);
public Map<Integer, String> getMajors();
}
StudentDAOImp:
@Service
public class StudentDAOImp implements StudentDAO {
@Autowired
private SessionFactory sessionFactory;
@Override
public void add(Student s) {
sessionFactory.getCurrentSession().merge(s);
}
@Override
public void delete(Integer id) {
Student s = getByID(id);
sessionFactory.getCurrentSession().delete(s);
}
@Override
public ArrayList<Student> getAll() {
Query query = sessionFactory.getCurrentSession().createQuery("from Student");
List<?> l = query.list(); // query returns a raw list.
ArrayList<Student> studentlist = new ArrayList<Student>();
for(Object o : l) { // objects in list must be cast individually
Student s = (Student) o;
studentlist.add(s);
}
return studentlist;
}
@Override
public Student getByID(Integer id) {
return (Student) sessionFactory.getCurrentSession().get(Student.class, id);
}
@Override
public Map<Integer, String> getMajors() {
Query query = sessionFactory.getCurrentSession().createQuery("from Major");
List<?> l = query.list(); // query returns a raw list.
Map<Integer, String> majorlist = new LinkedHashMap<Integer, String>();
for(Object o : l) { // objects in list must be cast individually
Major m = (Major) o;
majorlist.put(m.getMajorid(), m.getMajor());
}
return majorlist;
}
}
spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:annotation-driven />
<context:annotation-config/>
<context:component-scan base-package="com.eaglecrk.springtraining.dao" />
<context:component-scan base-package="com.eaglecrk.springtraining.test" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost\\SQLEXPRESS:1433;integratedSecurity=true;DatabaseName=StudentDB;" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop
key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.eaglecrk.springtraining.thingies</value>
<value>com.eaglecrk.springtraining.test</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory">
</bean>
</beans>
And the actual error:
Exception in thread "main" java.lang.NullPointerException
at com.eaglecrk.springtraining.test.Test.print(Test.java:26)
at com.eaglecrk.springtraining.test.Test.main(Test.java:22)
The log output:
Jun 13, 2014 1:59:17 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@33dd66fd: startup date [Fri Jun 13 13:59:17 CDT 2014]; root of context hierarchy
Jun 13, 2014 1:59:17 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-config.xml]
Jun 13, 2014 1:59:17 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: com.microsoft.sqlserver.jdbc.SQLServerDriver
Jun 13, 2014 1:59:18 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Jun 13, 2014 1:59:18 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
Jun 13, 2014 1:59:18 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jun 13, 2014 1:59:18 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 13, 2014 1:59:18 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
Jun 13, 2014 1:59:18 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Jun 13, 2014 1:59:18 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Jun 13, 2014 1:59:18 PM org.springframework.orm.hibernate4.HibernateTransactionManager afterPropertiesSet
INFO: Using DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@ed2bc5a] of Hibernate SessionFactory for HibernateTransactionManager
Exception in thread "main" java.lang.NullPointerException
at com.eaglecrk.springtraining.test.Test.print(Test.java:28)
at com.eaglecrk.springtraining.test.Test.main(Test.java:23)