org.springframework.orm.toplink.support
Class TopLinkDaoSupport

java.lang.Object
  extended by org.springframework.orm.toplink.support.TopLinkDaoSupport
All Implemented Interfaces:
InitializingBean

public abstract class TopLinkDaoSupport
extends Object
implements InitializingBean

Convenient super class for TopLink data access objects.

Requires a SessionFactory to be set, providing a TopLinkTemplate based on it to subclasses. Can alternatively be initialized directly via a TopLinkTemplate, to reuse the latter's settings like SessionFactory, exception translator, etc.

This base class is mainly intended for TopLinkTemplate usage but can also be used when working with SessionFactoryUtils directly, e.g. in combination with TopLinkInterceptor-managed Sessions. Convenience getSession and releaseSession methods are provided for that usage style.


Field Summary
protected  Log logger
           
 
Constructor Summary
TopLinkDaoSupport()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
protected  DataAccessException convertTopLinkAccessException(oracle.toplink.exceptions.TopLinkException ex)
          Convert the given TopLinkException to an appropriate exception from the org.springframework.dao hierarchy.
protected  TopLinkTemplate createTopLinkTemplate(SessionFactory sessionFactory)
          Create a TopLinkTemplate for the given SessionFactory.
protected  oracle.toplink.sessions.Session getSession()
          Get a TopLink Session, either from the current transaction or a new one.
protected  oracle.toplink.sessions.Session getSession(boolean allowCreate)
          Get a TopLink Session, either from the current transaction or a new one.
 SessionFactory getSessionFactory()
          Return the TopLink SessionFactory used by this DAO.
 TopLinkTemplate getTopLinkTemplate()
          Return the TopLinkTemplate for this DAO, pre-initialized with the SessionFactory or set explicitly.
protected  void initDao()
          Subclasses can override this for custom initialization behavior.
protected  void releaseSession(oracle.toplink.sessions.Session session)
          Close the given TopLink Session, created via this DAO's SessionFactory, if it isn't bound to the thread.
 void setSessionFactory(SessionFactory sessionFactory)
          Set the TopLink SessionFactory to be used by this DAO.
 void setTopLinkTemplate(TopLinkTemplate topLinkTemplate)
          Set the TopLinkTemplate for this DAO explicitly, as an alternative to specifying a SessionFactory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final Log logger
Constructor Detail

TopLinkDaoSupport

public TopLinkDaoSupport()
Method Detail

setSessionFactory

public final void setSessionFactory(SessionFactory sessionFactory)
Set the TopLink SessionFactory to be used by this DAO. Will automatically create a TopLinkTemplate for the given SessionFactory.

See Also:
createTopLinkTemplate(org.springframework.orm.toplink.SessionFactory), setTopLinkTemplate(org.springframework.orm.toplink.TopLinkTemplate)

createTopLinkTemplate

protected TopLinkTemplate createTopLinkTemplate(SessionFactory sessionFactory)
Create a TopLinkTemplate for the given SessionFactory. Only invoked if populating the DAO with a SessionFactory reference!

Can be overridden in subclasses to provide a TopLinkTemplate instance with different configuration, or a custom TopLinkTemplate subclass.

Parameters:
sessionFactory - the TopLink SessionFactory to create a TopLinkTemplate for
Returns:
the new TopLinkTemplate instance
See Also:
setSessionFactory(org.springframework.orm.toplink.SessionFactory)

getSessionFactory

public final SessionFactory getSessionFactory()
Return the TopLink SessionFactory used by this DAO.


setTopLinkTemplate

public final void setTopLinkTemplate(TopLinkTemplate topLinkTemplate)
Set the TopLinkTemplate for this DAO explicitly, as an alternative to specifying a SessionFactory.

See Also:
setSessionFactory(org.springframework.orm.toplink.SessionFactory)

getTopLinkTemplate

public final TopLinkTemplate getTopLinkTemplate()
Return the TopLinkTemplate for this DAO, pre-initialized with the SessionFactory or set explicitly.


afterPropertiesSet

public final void afterPropertiesSet()
                              throws Exception
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails.

initDao

protected void initDao()
                throws Exception
Subclasses can override this for custom initialization behavior. Gets called after population of this instance's bean properties.

Throws:
Exception - if initialization fails

getSession

protected final oracle.toplink.sessions.Session getSession()
                                                    throws DataAccessResourceFailureException,
                                                           IllegalStateException
Get a TopLink Session, either from the current transaction or a new one. The latter is only allowed if the "allowCreate" setting of this bean's TopLinkTemplate is true.

Note that this is not meant to be invoked from TopLinkTemplate code but rather just in plain TopLink code. Either rely on a thread-bound Session (via TopLinkInterceptor), or use it in combination with releaseSession.

In general, it is recommended to use TopLinkTemplate, either with the provided convenience operations or with a custom TopLinkCallback that provides you with a Session to work on. TopLinkTemplate will care for all resource management and for proper exception conversion.

Returns:
the TopLink Session
Throws:
DataAccessResourceFailureException - if the Session couldn't be created
IllegalStateException - if no thread-bound Session found and allowCreate false
See Also:
TopLinkTemplate, SessionFactoryUtils.getSession(SessionFactory, boolean), TopLinkInterceptor, TopLinkTemplate, TopLinkCallback

getSession

protected final oracle.toplink.sessions.Session getSession(boolean allowCreate)
                                                    throws DataAccessResourceFailureException,
                                                           IllegalStateException
Get a TopLink Session, either from the current transaction or a new one. The latter is only allowed if "allowCreate" is true.

Note that this is not meant to be invoked from TopLinkTemplate code but rather just in plain TopLink code. Either rely on a thread-bound Session (via TopLinkInterceptor), or use it in combination with releaseSession.

In general, it is recommended to use TopLinkTemplate, either with the provided convenience operations or with a custom TopLinkCallback that provides you with a Session to work on. TopLinkTemplate will care for all resource management and for proper exception conversion.

Parameters:
allowCreate - if a new Session should be created if no thread-bound found
Returns:
the TopLink Session
Throws:
DataAccessResourceFailureException - if the Session couldn't be created
IllegalStateException - if no thread-bound Session found and allowCreate false
See Also:
SessionFactoryUtils.getSession(SessionFactory, boolean), TopLinkInterceptor, TopLinkTemplate, TopLinkCallback

convertTopLinkAccessException

protected final DataAccessException convertTopLinkAccessException(oracle.toplink.exceptions.TopLinkException ex)
Convert the given TopLinkException to an appropriate exception from the org.springframework.dao hierarchy. Will automatically detect wrapped SQLExceptions and convert them accordingly.

Delegates to the convertTopLinkAccessException method of this DAO's TopLinkTemplate.

Parameters:
ex - TopLinkException that occured
Returns:
the corresponding DataAccessException instance
See Also:
setTopLinkTemplate(org.springframework.orm.toplink.TopLinkTemplate), TopLinkAccessor.convertTopLinkAccessException(oracle.toplink.exceptions.TopLinkException)

releaseSession

protected final void releaseSession(oracle.toplink.sessions.Session session)
Close the given TopLink Session, created via this DAO's SessionFactory, if it isn't bound to the thread.

Parameters:
session - the TopLink Session to close
See Also:
SessionFactoryUtils.releaseSession(oracle.toplink.sessions.Session, org.springframework.orm.toplink.SessionFactory)


Copyright (c) 2002-2005 The Spring Framework Project.