天空是蓝色的

做好软件为中国 #gcc -c helloworld.c -o helloworld.o //编译目标文件 #gcc helloworld.o -o helloworld //编译成可执行exe #helloworld //运行exe
数据加载中……
Hibernate的分页
 Session session = HibernateUtil.currentSession();

Transaction tx 
= session.beginTransaction();
Query query 
= session.createQuery("from Cat");

query.setFirstResult(
0);//开始
query.setMaxResult(10);//最大
for (Iterator it = query.iterate(); it.hasNext();) {
      
//Object obj = it.next();
}

tx.commit();
HibernateUtil.closeSession();

HibernateUtil.java

public class HibernateUtil {
    
private static final SessionFactory sessionFactory;

    
static {
        
try {
            
// Create the SessionFactory
            Configuration cfg = new Configuration().configure();
            sessionFactory 
= cfg.buildSessionFactory();
        }
 catch (Throwable ex) {
            
throw new ExceptionInInitializerError(ex);
        }

    }


    
public static final ThreadLocal session = new ThreadLocal();

    
public static Session currentSession() {
        Session s 
= (Session) session.get();
        
if (s == null{
            
try {
                s 
= sessionFactory.openSession();
            }
 catch (HibernateException e) {
                
// TODO Auto-generated catch block
                e.printStackTrace();
            }

            session.set(s);
        }

        
return s;
    }


    
public static void closeSession() {
        Session s 
= (Session) session.get();
        
if (s != null)
            
try {
                s.close();
            }
 catch (HibernateException e) {
                
// TODO Auto-generated catch block
                e.printStackTrace();
            }

        session.set(
null);
    }


}

posted on 2005-12-21 14:16 bluesky 阅读(518) 评论(0)  编辑  收藏 所属分类: 工作总结


只有注册用户登录后才能发表评论。


网站导航: