0

How to write complex queries in hibernate? I mean sql queries like inner join or so. If i want to access datas from two tables what do i need to do?

2 Answers 2

1

Regarding how to configure hibernate in order to access MySql, check this example

I dint understand the part where you said "using web services".

You want to do some operations using hibernate, via web services ? If yes, write a method which does the operations on hibernate and simply expose that method.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your valuable help. I was searching exactly this. Hope i can expose as many methods as i want using a single wsdl. Ant these methods can use hibernate to access database. Thanks again
You can expose as many methods as you want. If the problem is solved, accept the answer. Thank you.
0

Your complex query can call this method

 public <T> List<T> getCustomRecordsForSQL(String sqlQueryStr, T instance) {
   Session sess = initSession();
   HIFILogger.logInfo(sqlQueryStr);
   List<T> retval = null;
   try {
      Query query = sess.createSQLQuery(sqlQueryStr)
            .setResultTransformer(Transformers.aliasToBean(instance.getClass()));
      sess.setCacheMode(CacheMode.IGNORE);
      retval = query.list();
      if (retval != null) {
         HIFILogger.logInfo("Record Returned - " + retval.size());
      }
   } catch (HibernateException e) {
      HIFILogger.logInfo(e.getMessage());
      e.printStackTrace(); //FIXME
      throw e;
   } finally {
      if (sess != null) {
         sess.close();
      }
   }
   return retval;
}

The caller method should send the query itself and the class e.g

@Override
public List<Employee> getRecordsForClassName(Integer userID) throws UserGoalException {
    String query = "SELECT Employee.*, Employee.Name, Org.Description FROM Employee,OrgWHERE Employee.GoalID = Org.UserID AND Employee.userID = "
            + userID;
    List<Employee> empGoalList= getCustomRecordsForSQL(query, new Employee());
    return empGoalList;
}

Please ignore the lack of usage of parameters or null checks. It's just for example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.