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;
}
}