0

I'm facing a strange issue during a DAO implementation call because the sessionFactory and session are null, but if i call it trough different page is working. I really appreciate your help.

this is the class where i'm getting the null in the PATH variable

    @ManagedBean(name = "testService")
    @ApplicationScoped
    @Repository
    public class Test {

@Autowired
ConfigDAOImpl cfgDAO = new ConfigDAOImpl();

public boolean getSecureConnection() {
    String path = cfgDAO.getValueIfEnabled("xml_export_path");
    System.out.println( "*********** "+path+"**********" );
    return true;
}

}

this is the class where the same call is working properly

    @ManagedBean(name = "documentService")
    @ApplicationScoped
    @Repository
    public class DocumentService {

    @Autowired
ConfigDAOImpl cfgDAO = new ConfigDAOImpl();

    public TreeNode createDocuments() {
    TreeNode root = new DefaultTreeNode(new Document("Files", "", "-"), null);
    String path = cfgDAO.getValueIfEnabled("xml_export_path");

    if(!path.contains("exception") || !path.equals("config setting is missing"))
        walk(path, root);

    return root;
    }

this is the configDAOImpl

@Repository
public class ConfigDAOImpl {

private static final Logger logger = 
LoggerFactory.getLogger(ConfigDAOImpl.class);

public ConfigDAOImpl() {

}

private Session session;

@Autowired
private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
    return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    System.out.println("Sessionfactory value " + sessionFactory);
    this.sessionFactory = sessionFactory;
}

private Session getSession(SessionFactory sessionFactory){
     Session session = null;
     try{
         session = sessionFactory.getCurrentSession();
     }catch(HibernateException hex){
         hex.printStackTrace();
     }
     if(session == null){
         session = sessionFactory.openSession();
     }
    return session;
    }

   @Transactional
   public String getValueIfEnabled(String cfg_key) {

    try{
        this.session = getSession(sessionFactory);
        Config cfg =  (Config) session.get(Config.class, cfg_key);
        if (cfg.getCfgstatus().equals("true"))
            return cfg.getCfgvalue();
        else
            return "config setting is disabled";
        }

        catch( Exception  ex){

             return "exception with key "+cfg_key + 
                    " " + ex.toString();

        }
}

this below is the spring cfg file

<beans:bean id="configDAOImpl" class="com.test.db.dao.ConfigDAOImpl">
    <beans:property name="sessionFactory" ref="hibernate5AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="configService" class="com.test.db.service.ConfigServiceImpl">
    <beans:property name="configDAO" ref="configDAOImpl"></beans:property>
</beans:bean>
Matt Vegas
  • 453
  • 5
  • 17
  • Of course it is `null`. you are doing `new ConfigDAOImpl()` yourself. Remove that. Next to that `@Autowired` in JSF managed bean will not work you have to use `@ManagedProperty` for those. Next to that your DAO is also dangerously flawed. – M. Deinum Jul 18 '17 at 06:07
  • thanks for the reply, i cleaned the code as you mentioned, and i tried to use als that @ManagedProperty... but from jsf to the bean i'm able to contact that using: @ManagedProperty("#{configService}") private ConfigService service; public void setService(ConfigService service) { this.service = service; } System.out.println(service.getValueIfEnabled("xml_export_path")); – Matt Vegas Jul 18 '17 at 08:10

0 Answers0