-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: hibernate3 problem!!! plz help
PostPosted: Sun Apr 09, 2006 10:58 pm 
Newbie

Joined: Sat Mar 18, 2006 1:27 am
Posts: 15
how do i execute HQL and SQL querys in hibernate? since the session object of version3 has been changed. i tried something like this, but always get errors. please help


error:
The method getHoney(HoneyModel, Serializable) in the type HoneyDAO is not applicable for the arguments (Class<HoneyModel>, String)



Code:
    <%   
      try {
      HoneyModel honey = new HoneyModel();

      //honey.setId();
      honey.setName("me");
      honey.setTaste("the best123");

      HoneyDAO honeyDAO = new HoneyDAO();

      honeyDAO.saveHoney(honey);
      

      HoneyModel a = new HoneyModel();
      a.setName("me");
      
      HoneyModel b = (HoneyModel)honeyDAO.getHoney(HoneyModel.class, new String("me"));
      
      out.println(b.toString());
      
         
      } catch (org.hibernate.HibernateException e) {
         e.printStackTrace();
      }
         
      
    %>











Code:




public class HoneyDAO extends DBfoundation {

   /**
    *
    */
   public HoneyDAO() {
      super();
      // TODO Auto-generated constructor stub
   }

   public void saveHoney(HoneyModel honey) {

      try {
         this.save(honey);
      } catch (DBException e) {
         throw e;
      }
   }

   public Object getHoney(HoneyModel honey, Serializable id) {
      try {

            return this.get(honey.getClass(), id);
            
      } catch (DBException e) {
         throw e;
      }


   }

}



Code:



public class DBfoundation {

   private Session session;

   /**
    *
    */
   protected DBfoundation() {
      super();
      // TODO Auto-generated constructor stub

      // initialize the session
      session = null;

   }

   protected void openCurrentSession() throws DBException {
      try {
         if (session == null)
            session = HibernateUtil.getCurrentSession();
      } catch (DBException e) {
         throw e;
      }
   }

   protected void closeCurrentSession() throws DBException {
      try {
         if (session != null)
            HibernateUtil.closeSession();
      } catch (DBException e) {
         throw e;
      }
   }


   protected void delete(Object obj) throws DBException {

      try {

         openCurrentSession();

         Transaction tx = session.beginTransaction();

         session.delete(obj);
         tx.commit();
         



      } catch (DBException e) {
         throw e;
      }

   }

   protected void save(Object obj) throws DBException {

      try {

         openCurrentSession();


         Transaction tx = session.beginTransaction();

         session.save(obj);
         tx.commit();




      } catch (DBException e) {
         throw e;
      }

   }

   protected void saveOrUpdate(Object obj) throws DBException {

      try {


         openCurrentSession();

         Transaction tx = session.beginTransaction();

         session.saveOrUpdate(obj);
         tx.commit();




      } catch (DBException e) {
         throw e;
      }

   }

   protected void update(Object obj) throws DBException {

      try {


         openCurrentSession();

         Transaction tx = session.beginTransaction();

         session.update(obj);
         tx.commit();




      } catch (DBException e) {
         throw e;
      }

   }

   protected Object get(Class obj, Serializable id) throws DBException {
      Object result = null;
      try {


         openCurrentSession();

         Transaction tx = session.beginTransaction();

         result = session.get(obj, id);
         tx.commit();




      } catch (DBException e) {
         throw e;
      }

      return result;

   }

   protected List findSQL(String SQLQuery) throws DBException {
      List result = null;
      try {


         openCurrentSession();

         Transaction tx = session.beginTransaction();

         result = session.createSQLQuery(SQLQuery).list();
         
         tx.commit();




      } catch (DBException e) {
         throw e;
      }

      return result;

   }

   protected List findHQL(String HQLQuery) throws DBException {
      List result = null;
      try {


         openCurrentSession();

         Transaction tx = session.beginTransaction();

         result = session.createQuery(HQLQuery).list();
         
         tx.commit();




      } catch (DBException e) {
         throw e;
      }

      return result;

   }
   
   

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 12:48 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That's a java coding bug, not an hibernate issue. Look at the formal parameter list:
Code:
public Object getHoney(HoneyModel honey, Serializable id)
Now look at what you're calling:
Code:
HoneyModel b = (HoneyModel)honeyDAO.getHoney(HoneyModel.class, new String("me"));
See the problem? You're passing in a Class object, but your method wants an instance object. Either change the getHoney method to take a class object, or change your code to pass in an HoneyModel object. Or even better, seeing as "getHoney" can't get Bee objects or Milk objects, change it to take only an id parameter, and hardcode the class in the getHoney method.

BTW, `new String("me")' is completely redundant. It will return "me", so you can save yourself a few VM cycles by just using "me" directly.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.