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>