Reputation: 314
I am using hibernate GenriDAO
Here is my code::
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;
}
}
When i commit the transaction and try to access the attributes on the object(i.e pojo) it will give the hibernate exception "no session" or session closed
if m not not commiting its work fine. but the problem is session remains open.
What are the ways to access that entity ???
Upvotes: 1
Views: 3116
Reputation: 12358
Hibernate by default is lazy. Most probably the attributes you are getting are being loaded lazily. Whenever the session is open you can access the properties as Hibernate fetches these properties as and when initialized/accessed.
e.g :
public class Person{
private Set<Child> children;
public Set<Child> getChildren(){
return children;
}
public void setChildren(Set<Child> p0){
this.children=p0;
}
}
Here when you load the instance of a person the children collection isn't loaded eagerly. Hibernate fetches them when you access it.
If the session is closed it throws a LazyInitializationException
Have a look concepts of Lazy Loading and Eager loading in Hibernate also for starters try to glance over the Open Session In View Pattern if you are using Hibernate in a web application.
Upvotes: 0
Reputation: 9868
Hope this helps : 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();
}
}
Upvotes: 1