Hibernate Session

时间:2011-09-21 13:49:47

标签: hibernate

我正在使用hibernate GenriDAO

这是我的代码::

 private Class<T> persistentClass;
 public Class<T> getPersistentClass() {
         return persistentClass;
 }
 public GenericHibernateDAO(Class<T> persistentClass ){
         this.persistentClass=persistentClass;
 }
 public T findById(long id) {
         SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
         Session session=sessionFactory.getCurrentSession();
         Transaction transaction = null;

         T entity=null;
         try {
                 transaction = session.beginTransaction();
                 entity=(T)session.get(getPersistentClass(), id);
                 //        transaction.commit();
         } catch (HibernateException e) {
         //        transaction.rollback();
                 e.printStackTrace();
         } finally {
         //        transaction = null;
         }
         return entity;
 }

}

当我提交事务并尝试访问对象上的属性(即pojo)时,它会给hibernate异常“no session”或会话关闭

如果我还没有将其工作做好。 但问题是会议仍然开放。

访问该实体有哪些方法?

2 个答案:

答案 0 :(得分:1)

希望这会有所帮助:http://community.jboss.org/wiki/GenericDataAccessObjects

public abstract class GenericHibernateDAO<T, ID extends Serializable>
        implements GenericDAO<T, ID> {

    private Class<T> persistentClass;
    private Session session;

    public GenericHibernateDAO() {
        this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
                                .getGenericSuperclass()).getActualTypeArguments()[0];
     }

    @SuppressWarnings("unchecked")
    public void setSession(Session s) {
        this.session = s;
    }

    protected Session getSession() {
        if (session == null)
            throw new IllegalStateException("Session has not been set on DAO before usage");
        return session;
    }

    public Class<T> getPersistentClass() {
        return persistentClass;
    }

    @SuppressWarnings("unchecked")
    public T findById(ID id, boolean lock) {
        T entity;
        if (lock)
            entity = (T) getSession().load(getPersistentClass(), id, LockMode.UPGRADE);
        else
            entity = (T) getSession().load(getPersistentClass(), id);

        return entity;
    }

    @SuppressWarnings("unchecked")
    public List<T> findAll() {
        return findByCriteria();
    }

    @SuppressWarnings("unchecked")
    public List<T> findByExample(T exampleInstance, String[] excludeProperty) {
        Criteria crit = getSession().createCriteria(getPersistentClass());
        Example example =  Example.create(exampleInstance);
        for (String exclude : excludeProperty) {
            example.excludeProperty(exclude);
        }
        crit.add(example);
        return crit.list();
    }

    @SuppressWarnings("unchecked")
    public T makePersistent(T entity) {
        getSession().saveOrUpdate(entity);
        return entity;
    }

    public void makeTransient(T entity) {
        getSession().delete(entity);
    }

    public void flush() {
        getSession().flush();
    }

    public void clear() {
        getSession().clear();
    }

    /**
     * Use this inside subclasses as a convenience method.
     */
    @SuppressWarnings("unchecked")
    protected List<T> findByCriteria(Criterion... criterion) {
        Criteria crit = getSession().createCriteria(getPersistentClass());
        for (Criterion c : criterion) {
            crit.add(c);
        }
        return crit.list();
   }

}

答案 1 :(得分:0)

默认情况下,休眠是懒惰。很可能你得到的属性是懒惰加载。每当会话打开时,您都可以访问属性,因为Hibernate在初始化/访问时会获取这些属性 例如:

public class Person{ 
  private  Set<Child> children;

  public Set<Child> getChildren(){
          return children;
  }
  public void setChildren(Set<Child> p0){
      this.children=p0;
  }
}

这里当你加载一个人的实例时,不会急切地加载子集合。 Hibernate在您访问它时会获取它们。 如果会话已关闭,则会抛出LazyInitializationException

在Hibernate中查看 延迟加载 急切加载 的概念 如果您在Web应用程序中使用Hibernate,那么对于初学者来说,请尝试浏览 在视图模式中打开会话