wiki:Spring

Option 1 Example of a Dao or Service object:

@Repository
@Transactional
public class ErikExampleDao {

	@Autowired
	private SessionFactory sessionFactory;

	void saveErikExampleDomainClass(ErikExampleDomain erikExampleDomain){
		sessionFactory.getCurrentSession().save(erikExampleDomain)

This configuaration needed:

<bean id="erikExampleDao" class="org.geonetwork.dao.ebxml.ErikExampleDao">

Option 2

public class ExampleDao extends HibernateDaoSupport {
	void saveErikExampleDomain(ErikExampleDomain erikExampleDomain ) {
		this.getHibernateTemplate().save(erikExampleDomain );
	}

This configuaration needed:

	<bean id="erikExampleDao" class="org.geonetwork.dao.ebxml.ErikExampleDao">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

Option 3

public class ExampleDao extends HibernateDaoSupport {
	void saveErikExampleDomain(ErikExampleDomain erikExampleDomain ) {
		this.getHibernateTemplate().save(erikExampleDomain );
	}

This configuaration needed:

	<bean id="hibernateDaoSupport" class="org.springframework.orm.hibernate3.support.HibernateDaoSupport" abstract="true">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="erikExampleDao" class="org.geonetwork.dao.ebxml.ErikExampleDao" parent="hibernateDaoSupport" />

Chosen Option 3. First Option 1 was chosen because of : This option is chosen because (1) one line less of configuration (2) It is working directly with the sessionFactory instead of a Facade like HibernateTemplate. Working with a template can have advantages. However in case of Hibernate, Spring has little to add because Hibernate is a very mature package. (3) SessionFactory is directly available. This might be necessary in cases of more advanced use of Hibernate.

Option 3 has been chosen after a tip from Jose. Option 3 has a little bit more configuration at the beginning. But for each Dao there is only one line of configuration needed. This is more elegant especially when there will be more Dao's. The sessionFactory is still available via this.getSessionFactory(). The dao itself is very minimalistic, all the boilerplate code is in the HibernateDaoSupport.

In option 1 you see as well the annotations @Transactional and @Repository. @Transaction means that every method in this Dao is considered as a transaction. @Repository has to do with the way Spring handles exceptions.

Last modified 15 years ago Last modified on Nov 12, 2008, 12:24:02 AM
Note: See TracWiki for help on using the wiki.