2011-09-21 43 views
1

我使用休眠GenriDAOHibernate的Session

這裏是我的代碼::

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)它會給休眠異常「無會話」或會話關閉

如果米不提交其工作正常。 但問題是會話保持打開狀態。

訪問該實體的方式有哪些?

+0

當您發佈代碼時,請格式化代碼(我這次完成了) – kunal

回答

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(); 
    } 

} 
0

休眠默認爲。很可能您正在獲得的屬性正在被懶惰地加載。每當會話打開時,您可以訪問屬性,因爲Hibernate會在初始化/訪問時獲取這些屬性。
e.g:

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。

相關問題