我想写一个hibernate的基础类,部分代码如下:
[code="java"]
package cn.sanxing.model.util;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
/**
通用的DAO实现方法,提供了持久化操作的基础方法
*
*/
public class HibernateDAO
{
public void saveObject(Object obj)
{
getSession().save(obj);
}
}
[/code]
其中getSession().save(obj);这行一直提示“The Method getSession() is undefined for the type HibernateDAO”
我不是很清楚,请大家帮我看看是什么原因造成的,怎么解决?
你没定义getSession()方法,
HibernateDAO继承HibernateTemplate
新建一个方法
[code="java"]
public Session getSession() throws DatabaseException {
Session session = this.getSession();
return session;
}
[/code]
“The Method getSession() is undefined for the type HibernateDAO”是说你写的HibernateDAO中没有写getSession()方法。
楼主在 public void saveObject(Object obj)中是应该想通过hibernate的session保存obj。因此,首先要获得工厂实例再打开session.
Configuration config=new Configuration().configure();
SessionFactory factory=config.buildSessionFactory();
Session session=factory.openSession();
session.save(obj)