java - Cannot make a static reference to the non-static method getSessionFactory() from the type HibernateUtil -
tried display data mysql database using hibernate , struts2, can add data database , values getting updated cannot display in jsp page.
error: cannot make static reference non-static method getsessionfactory() type hibernateutil
here pages are..
hibernateutils.java
package com.stg.shdb2.utils; import org.hibernate.sessionfactory; import org.hibernate.cfg.annotationconfiguration; public class hibernateutils { private static final sessionfactory sessionfactory=buildsessionfactory(); private static sessionfactory buildsessionfactory() { try { // create sessionfactory hibernate.cfg.xml return new annotationconfiguration().configure().buildsessionfactory(); } catch (throwable ex) { // make sure log exception, might swallowed system.err.println("initial sessionfactory creation failed." + ex); throw new exceptionininitializererror(ex); } } public static sessionfactory getsessionfactory() { return sessionfactory; } }
dataconnect.java
package com.stg.shdb2.dao; import java.util.list; import net.sf.gilead.core.hibernate.hibernateutil; import org.hibernate.hibernateexception; import org.hibernate.session; import org.hibernate.transaction; import org.apache.cxf.service.invoker.sessionfactory; import com.stg.shdb2.beans.user; import com.stg.shdb2.utils.hibernateutils; public class dataconnect { session sess; public void insertdetails(user user){ try{ sess = (session) hibernateutils.getsessionfactory(); transaction tx = sess.begintransaction(); sess.save(user); system.out.println("data inserted in user_details"); tx.commit(); sess.close(); } catch(exception exep){ exep.printstacktrace(); } } @suppresswarnings("unchecked") public list<user> list() { session session = hibernateutil.getsessionfactory().getcurrentsession(); session.begintransaction(); list<user> user = null; try { user = (list<user>) session.createquery("from user_data").list(); } catch (hibernateexception e) { e.printstacktrace(); session.gettransaction().rollback(); } session.gettransaction().commit(); return user; } }
you need change this:
session session = hibernateutil.getsessionfactory().getcurrentsession();
to this:
session session = hibernateutils.getsessionfactory().getcurrentsession();
the class named hibernateutils
, not hibernateutil
.
Comments
Post a Comment