Note that there are some explanatory texts on larger screens.

plurals
  1. PODAO scope in spring-hibernate multi-user web-app?
    text
    copied!<p>I actually saw <a href="https://stackoverflow.com/questions/201022/which-scope-should-a-dao-typically-have"><strong>this question</strong></a>, but couldn't get much from it, so I'll try to be more specific with mine.<br> I have <em>BaseDAO</em> class in my multi-user web-app that looks like this:</p> <pre><code>public abstract class BaseDAO&lt;GenType&gt; { private HibernateOperations hibernateTemplate; protected BaseDAO() {} protected HibernateOperations getHibernateTemplate() { return hibernateTemplate; } public void setHibernateTemplate(HibernateOperations hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } protected void initialize(final Object proxy) throws DataAccessException { hibernateTemplate.initialize(proxy); } public GenType merge(GenType entity) throws DataAccessException { return (GenType)hibernateTemplate.merge(entity); } protected void persist(GenType entity) throws DataAccessException { hibernateTemplate.persist(entity); } public void refresh(GenType entity) throws DataAccessException { hibernateTemplate.refresh(entity); } public void save(GenType entity) throws DataAccessException { hibernateTemplate.save(entity); } public void saveOrUpdate(GenType entity) throws DataAccessException { hibernateTemplate.saveOrUpdate(entity); } public void update(GenType entity) throws DataAccessException { hibernateTemplate.update(entity); } public void delete(GenType entity) throws DataAccessException { hibernateTemplate.delete(entity); } protected void deleteAll(Collection&lt;GenType&gt; entities) throws DataAccessException { hibernateTemplate.deleteAll(entities); } protected GenType get(Class&lt;GenType&gt; entityClass, Serializable id) throws DataAccessException { return (GenType)hibernateTemplate.get(entityClass, id); } } </code></pre> <p>It's basically wrapper around <em>HibernateTemplate</em>. All my other DAOs inherit this class and implement appropriate interfaces, which hold some additional methods (like getBySomeAttribute()). So basically these DAOs have <strong>only</strong> methods. Further more, I have <em>service</em> layer that wraps DAOs. That is, a service class can hold multiple DAOs, and method calls from service layer are intercepted with spring-AOP for auto commit/rollback (transaction demaracation). For example:</p> <pre><code>public class ModelDAO extends BaseDAO&lt;Model&gt; implements IModelDAO { @Override public Model getNew() { return new Model(); } @Override public List&lt;Model&gt; getBySomeAttr() { DetachedCriteria criteria; // define some criteria return getHibernateTemplate().findByCriteria(criteria); } } ... public class ModelService { private ModelDAO modelDAO; private ElementDAO elementDAO; // GET/SET for model and user DAO public void doSomethingWithModel() { modelDAO.doSomething(); elementDAO.doSomethingElse(); } } </code></pre> <p>Config is as follows:</p> <pre><code>&lt;bean id="hibernateTemplate2" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt; &lt;property name="sessionFactory" ref="sessionFactory2" /&gt; &lt;property name="maxResults" value="3000" /&gt; &lt;/bean&gt; &lt;!-- DAO --&gt; &lt;bean id="baseDAO" abstract="true" class="org.irvas.backend.dao_implement.BaseDAO" scope="session"&gt; &lt;property name="hibernateTemplate" ref="hibernateTemplate2" /&gt; &lt;/bean&gt; &lt;bean id="modelDAO" class="org.irvas.backend.dao_implement.ModelDAOImplement" parent="baseDAO" scope="session"&gt; &lt;/bean&gt; &lt;bean id="elementDAO" class="org.irvas.backend.dao_implement.ElementDAOImplement" parent="baseDAO" scope="session"&gt; &lt;/bean&gt; &lt;!-- Service --&gt; &lt;bean id="modelService" class="org.irvas.backend.service_implement.ModelServiceImplement" scope="session"&gt; &lt;property name="modelDAO" ref="modelDAO" /&gt; &lt;property name="elementDAO" ref="elementDAO" /&gt; &lt;/bean&gt; &lt;bean id="elementService" class="org.irvas.backend.service_implement.ElementServiceImplement" scope="session"&gt; &lt;property name="elementDAO" ref="elementDAO" /&gt; &lt;/bean&gt; </code></pre> <p>So, I'm wondering how should I scope DAO/Service beans for multi-user purposes? I am injecting service beans into <strong>prototype</strong> controllesrs for GUI. When I use this configuration (with scope="session"), I get error like:</p> <blockquote> <p>Error creating bean with name 'modelService': java.lang.IllegalArgumentException setAttribute: Non-serializable attribute: modelDAO</p> </blockquote> <p>I'd say that this is thrown from Tomcat's StandardSession.setAttribute(). From this I could conclude that my DAOs should implement Serializable, and what confuses me even more is that I saw exact same code that works without implementing Serializable... </p> <p>If someone could enlighten me what is going on here, and <strong>how</strong> to scope DAOs and Services for this particular case, I'd be very grateful...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload