hibernate中关于sessionFactory中session方法问题

来源:百度知道 编辑:UC知道 时间:2024/05/19 22:57:43
package com.yourcompany;
import java.util.Iterator;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;

public class AdminDAOFactory {
Session session;
Transaction tx;

public void add(Admin admin) throws HibernateException {

try {
session = SessionFactory.currentSession();//说这个方法不对,为什么?
tx = session.beginTransaction();
//Add a new admin
session.save(admin);
tx.commit ();
}catch(HibernateException e){
throw e;
}finally{
if (tx!=null) {
tx.rollback();
}
SessionFactory.closeSession();//这个方法也不对,,,
}
}
}

解决问题后,给分,谁能告诉我怎么办啊?为什么说currentSession和closeSession方法不存在?对session的使用错了吗?该怎么弄啊?
下面的回答我看了,我也知道什么意思,有谁可以给做个SessionFactory,帮忙定义个currentSession()方法啊,我不会做..做个注册页面用的,,,明天要交

因为在这个程序中,你导入的是org.hibernate.SessionFactory;这个类中没有你用的那些静态方法。你这样写是不对的
你自已肯定定义了一个SessionFactory的类(或者是ide帮助你定义的),比如它被定义到了hibernate文件夹,这时你要导入的类应该为
hibernate.SessionFactory,不能导入org.hibernate.SessionFactory.根据hibernate.SessionFactory中定义的方法取得Session,这个方法名是
自己定义的,有可以是SessionFactory.currentSession(),也有可能是SessionFactory.getSession(),就看定义的时候用的是什么方法名了

你到别人那去弄一个这个类就行了.因为这个类并不是hibernate里自带的方法.这个方法一般是自己写的.或者是工具给生成的.

把这个类放到你工程src下的util包里面
确保你的hibernate.cfg.xml文件在src目录下面.
在你调用那块把你的SessionFactory换成我这个类的HibernateSessionFactory
你也可以把我的类名改成你的.怎么样都可以.

package util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link 关于SES~~~ hibernate中关于sessionFactory中session方法问题 关于Hibernate中many-to-one查询的问题? 关于SES的几首老歌 关于Hibernate连接数据库 关于Hibernate中的事务 关于HIBERNATE的疑问~ 进程中smss.ses是什么?? 关于SES里的shoo 求一首关于SES的歌