2011-09-21 4 views
1

내 코드 ::최대 절전 모드 세션

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) 최대 절전 모드 예외를 "세션 없음"또는 세션을 닫을 것입니다.

m 잘 작동하지 않을 경우. 하지만 문제는 세션이 열려있는 상태입니다.

해당 엔티티에 액세스하는 방법은 무엇입니까?

+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는 게으른입니다. 아마 당신이 얻는 속성이 느슨하게 적재되고있을 것입니다. 세션이 열릴 때마다 Hibernate는 초기화되거나 접근 될 때 Hibernate가 이들 프로퍼티를 가져 오기 때문에 프로퍼티에 접근 할 수있다.
예컨대 : 당신이 자식 컬렉션 열심히로드되지 않은 사람의 인스턴스를로드 여기

public class Person{ 
    private Set<Child> children; 

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

. 당신이 그것을 접근 할 때 Hibernate는 그것들을 가져온다. 세션이 닫혀있는 경우 IT는 LazyInitializationException

이 및 열망 로딩이 최대 절전 모드 에서은 우선보기 패턴에서 열기 세션을 통해 한눈에 시도도에 대한 게으른로드의 모습 개념을 가지고 발생 웹 응용 프로그램에서 최대 절전 모드를 사용하는 경우.