0

I have this in my servlet:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/test" />
    <property name="username" value="root" />
    <property name="password" value="12345" />
</bean>

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.tricas.models" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            <prop key="hibernate.default_schema">test</prop>
            <prop key="format_sql">true</prop>
            <prop key="use_sql_comments">true</prop>
            <!--<prop key="hibernate.hbm2ddl.auto">create</prop> -->
        </props>
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

and my DaoImp

@Repository
@Transactional
public class UserDaoImp implements UserDao {

@Autowired
SessionFactory session;

public List<Users> list() {

    return session.getCurrentSession().createQuery("from Users").list();
}

here is my HibernateUtil

private static final SessionFactory sessionFactory;

static {
    try {
        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
        sessionFactory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

And After executing the application I have a NullPointerException:

SEVERE [http-nio-8084-exec-97] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service () for servlet [spring-web] in context with path [/ Holaspringmvc] threw exception [Request processing failed; Nested exception is java.lang.NullPointerException] with root cause Java.lang.NullPointerException At com.tricas.dao.UserDaoImp.list (UserDaoImp.java:32) Please help me.

asg
  • 2,248
  • 3
  • 18
  • 26
John Lopez
  • 127
  • 1
  • 9

2 Answers2

0

Be simpler. Just declare SessionFactory bean

@Bean
public AbstractSessionFactoryBean sessionFactoryBean(){
    AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
    sessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
    return sessionFactoryBean;
}

similar for LocalSessionFactoryBean

btw: did you define component-scan ?

<context:component-scan base-package="<my.base.package>" />
iMysak
  • 2,170
  • 1
  • 22
  • 35
0

I found the error, results that I had to define in the service and in the controller also with @Autowired

Here is my Service.

@Autowired
UserDao usrdao;

//private UserDao usrdao = new UserDaoImp();

@Transactional
public List<Users> getAllUsers() {        
    return usrdao.list();
}

and here is my controller

@Autowired
UserService usrv;

//private UserService usrv = new UserService();

@RequestMapping(value = "/verusuarios", method = RequestMethod.GET)
public String listPersons(Model model) {
    List<Users> list = usrv.getAllUsers();
    model.addAttribute("user", new Users());
    model.addAttribute("list", list);
    return "verusuarios";
}

Additionally I must add to guide me from this answer: answer here

John Lopez
  • 127
  • 1
  • 9