entitymanager.java

来自「用Struts+Hibernate实现一个在线书店系统」· Java 代码 · 共 66 行

JAVA
66
字号
package com.jtf.bookstore.persist;

import com.jtf.bookstore.BaseEntity;

import net.sf.hibernate.Criteria;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

import java.util.ArrayList;
import java.util.List;


public class EntityManager
{
    private Class _cl;

    protected EntityManager(Class cl)
    {
        _cl = cl;
    }

    public List findAll() throws HibernateException
    {
        List rv = new ArrayList();

        Session session = SessionProvider.currentSession();
        Criteria criteria = session.createCriteria(_cl);
        rv = criteria.list();

        return rv;
    }

    public List findAllOrdered(String orderBy, boolean desc)
        throws HibernateException
    {
        String direction = (desc ? "desc" : "asc");
        Session session = SessionProvider.currentSession();
        String qry = "from " + _cl.getName() + " as e order by e." + orderBy +
            " " + direction;
        Query q = session.createQuery(qry);
        List result = q.list();

        return result;
    }

    public BaseEntity findById(int id) throws HibernateException
    {
        Session session = SessionProvider.currentSession();
        BaseEntity rv = (BaseEntity) session.get(_cl, new Integer(id));

        return rv;
    }

    public BaseEntity saveOrUpdate(BaseEntity entity) throws HibernateException
    {
        Session session = SessionProvider.currentSession();
        Transaction tx = session.beginTransaction();
        session.saveOrUpdate(entity);
        tx.commit();

        return entity;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?