0

Sorry, but I am repeating my question, because the error has not been rectified. I made service and dao layers as interfaces. I didn't made any hibernate cfg xml file, but I wrote all the hibernate configurations under application context.

this is only a test program.

When I am trying to get the username and and password from entity class, the session factory points to null always. If you need more specific code just mention it.

application Context:

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <context:component-scan base-package="com.company"/>
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>/WEB-INF/*.properties</value>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}" 
        p:username="${jdbc.username}" p:password="${jdbc.password}">

    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.company.entity"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.Dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</prop>

            </props>
        </property>
        <property name="entityInterceptor">
            <bean class="com.company.interceptor.AuditInterceptor"/>
        </property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"/>  
    </bean>
   <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="transactionAttributes">
        <props>
            <prop key="create*">PROPAGATION_REQUIRED</prop>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
    </property>            
    </bean>


    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">

            <value>*DaoImpl</value>
        </property>
       <property name="interceptorNames">
           <value>transactionInterceptor</value>
       </property>
    </bean>

    <import resource="spring-security.xml"/>

</beans>

User Dao Impl:

 @Repository
 public class UserDaoImpl extends AbstractDao  implements UserDao {

 public boolean test(){
    int id=1;
    boolean result;
    try{

        User user=null;
        user=(User) this.sessionFactory.getCurrentSession().get(User.class,id);
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        result=true;
        return result;
     }catch(NullPointerException n){
         System.out.println("inside null pointer exception handler");
         n.printStackTrace();
         result=false;
         return result;
     }

UserServive:

@Service
public class UserServiceImpl implements UserService {

    public boolean test(){
        UserDao userDao=new UserDaoImpl();
        return userDao.test();
    }
}

UserController:

 @Controller
 public class LoginController {

 private Logger logger;

 @Autowired
 UserService userService;

 @RequestMapping(value="/login.do",method=RequestMethod.POST)
 public String login(HttpServletRequest request,HttpServletResponse response){

     boolean result=userService.test();
     if(result=true){
          return "profile";
     }else{
          return "home";
     }
}

Abstract Dao:

public class AbstractDao{
    @Autowired
    protected SessionFactory sessionFactory;
}
Jishnu
  • 25
  • 1
  • 8
  • Your `abstractdao` is not a spring bean and hence how can you `autowire` sessionfactory to it? – Vipin CP Aug 03 '16 at 09:49
  • so how to change it?? – Jishnu Aug 03 '16 at 09:51
  • Try to add @Repository in AbstractDao as well or you can use `HibernateDaoSupport` or `HIbernateTemplate` – Vipin CP Aug 03 '16 at 09:53
  • sorry i just named it as abstract dao.. it is a class that created for retrieving sessionFactory object. i removed the autowire before but nothing happend,... the same error is repeating here is my stacktrace: – Jishnu Aug 03 '16 at 10:01
  • java.lang.NullPointerException at com.company.daoImpl.UserDaoImpl.MastersList(UserDaoImpl.java:35) at com.company.serviceImpl.UserServiceImpl.MastersList(UserServiceImpl.java:18) at com.company.controller.LoginController.login(LoginController.java:44) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) – Jishnu Aug 03 '16 at 10:02
  • Without autowiring that bean will not be available. I post an answer . Try with that – Vipin CP Aug 03 '16 at 10:03
  • i tried one more thing, declared the sessionfactory inside the userDaoImpl – Jishnu Aug 03 '16 at 10:05
  • Have you used autowire in daoimpl and created getters and setters? – Vipin CP Aug 03 '16 at 10:05
  • i got this error after extending hibernateDaosupport org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDaoImpl' defined in file [/* my workspace*/]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required – Jishnu Aug 03 '16 at 10:18

1 Answers1

0

Change your AbstractDao as below

public class AbstractDao extends HibernateDaoSupport
  {
@Autowired
protected SessionFactory sessionFactory;
  }

Then from test() method you can call getSession()or getHibernateTemplate() for sessionFactory

Read this for more info.

Vipin CP
  • 3,642
  • 3
  • 33
  • 55