This is really common probelm in Hibernate. The following code snapit is my DAO model
public class Parts implements Concept
{
@OneToMany(mappedBy="parts",cascade = CascadeType.ALL,fetch= FetchType.EAGER)
public Set < keydimension > getKeyDimensions()
{
if(keyDimensions == null)
keyDimensions = new HashSet();
return keyDimensions;
}
}
When I try to update a attribute in the Part object, the hibernate report me the error with Exception could not initialize proxy - no Session. There are also a lot of discussion avaliable on the internet. But I still spend some time to deal with the problem.
Here is the answer, in my DaoClass
public void changePartsStatus(Parts parts, int status)
{
Parts newPart = (Parts)this.getHibernateTemplate().get(Parts.class, parts.getObjid());
newPart.setStatus(status);
this.getHibernateTemplate().merge(newPart);
}
Then the problem gone.
沒有留言:
張貼留言