cancel
Showing results for 
Search instead for 
Did you mean: 

Getting current application context

p3d3r0s0
Champ in-the-making
Champ in-the-making
Hey, i was trying to update an sql table i made using hibernate, but i cant seem to get the current ApplicationContext.
Im trying to get the ApplicationContext inside the DbNodeServiceImpl class so that everytime a node is created i can update my counter in the table.

I was trying:

      ApplicationContext ac = ApplicationContextHelper.getApplicationContext();
      TypeHome th = (TypeHome)ac.getBean("TypeHome");
      Type currentType = th.findByQname(typeQName);

but this so far only leads to:
"Failed to create content due to error: A valid SecureContext was not provided in the RequestContext"

Can anyone help?
Regards


Edit: In my case this was solved using:
protected static ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());

and i used to get a row(that i called Type) from my table(named TypeHome):

   public static Type getMyType(String typeQName) {
      TypeHome th = (TypeHome)ctx.getBean("TypeHome");
      Type currentType = th.findByQname(typeQName);
      return currentType;
   }

and finally, in my class TypeHome i used this method to get a row or Type that column name that matched the string qname:

   @SuppressWarnings("unchecked")
   public Type findByQname(String qname) {
      log.debug("finding Type instance by example");
      try {
         List<Type> results = (List<Type>) getSession().createCriteria(
                     "com.ptsi.pedroStuff.Type").add(Restrictions.like("name", qname).ignoreCase())
               .list();
         log.debug("find by example successful, result size: "
               + results.size());
         return results.get(0);
      } catch (RuntimeException re) {
         log.error("find by example failed", re);
         throw re;
      }
   }

since the name column is unique i only need the first result (hence the results.get(0):smileywink:

hope this helps someone.
5 REPLIES 5

savic_prvoslav
Champ on-the-rise
Champ on-the-rise
hi, this class DbNodeServiceImpl  is not to be used
all you need to do is in NodeService .

p3d3r0s0
Champ in-the-making
Champ in-the-making
I appreciate you trying to help, but NodeService is an interface, and i cant change anything there. My biggest problem is how to connect to my database with hibernate.

savic_prvoslav
Champ on-the-rise
Champ on-the-rise
What are you changing?

p3d3r0s0
Champ in-the-making
Champ in-the-making
I want to be able to update an sql table when i create a node. Best way to do it, i believe to be, is inside the createNode method. What i really need is a way to access my database from there using hibernate.

wass
Champ in-the-making
Champ in-the-making
Hey, i was trying to update an sql table i made using hibernate, but i cant seem to get the current ApplicationContext.
Im trying to get the ApplicationContext inside the DbNodeServiceImpl class so that everytime a node is created i can update my counter in the table.

I was trying:

      ApplicationContext ac = ApplicationContextHelper.getApplicationContext();
      TypeHome th = (TypeHome)ac.getBean("TypeHome");
      Type currentType = th.findByQname(typeQName);

but this so far only leads to:
"Failed to create content due to error: A valid SecureContext was not provided in the RequestContext"

Can anyone help?
Regards


Edit: In my case this was solved using:
protected static ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());

and i used to get a row(that i called Type) from my table(named TypeHome):

   public static Type getMyType(String typeQName) {
      TypeHome th = (TypeHome)ctx.getBean("TypeHome");
      Type currentType = th.findByQname(typeQName);
      return currentType;
   }

and finally, in my class TypeHome i used this method to get a row or Type that column name that matched the string qname:

   @SuppressWarnings("unchecked")
   public Type findByQname(String qname) {
      log.debug("finding Type instance by example");
      try {
         List<Type> results = (List<Type>) getSession().createCriteria(
                     "com.ptsi.pedroStuff.Type").add(Restrictions.like("name", qname).ignoreCase())
               .list();
         log.debug("find by example successful, result size: "
               + results.size());
         return results.get(0);
      } catch (RuntimeException re) {
         log.error("find by example failed", re);
         throw re;
      }
   }

since the name column is unique i only need the first result (hence the results.get(0):smileywink:

hope this helps someone.

but how u've got the facesContext? i'm trying to get it but it always comes null.

can u help me?

thanks.