Example of a Spring 3.2 and Hibernate 4 repository base

The is a partial example of a spring 3.2 and hibernate 4 project repository base layer. It doesn’t necessarily compile or even do anything, but it’s an example…

aCtx-repository.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
					    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
					    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="packagesToScan" value="uk.co.vsf.penny.domain.impl" />
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
				<prop key="hibernate.hbm2ddl.auto">create</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<context:property-placeholder location="jdbc.properties" />

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true" />

</beans>

aCtx-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="baseDao" class="uk.co.vsf.penny.repository.dao.impl.BaseDao">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<bean id="accountDao"
		  class="uk.co.vsf.penny.repository.dao.impl.AccountDaoImpl"
		  parent="baseDao">
	</bean>

	<bean id="transactionDao"
		  class="uk.co.vsf.penny.repository.dao.impl.TransactionDaoImpl"
		  parent="baseDao">
	</bean>

</beans>

aCtx-base.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
					    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="uk.co.vsf.penny" />
</beans>

BaseDao.java

public class BaseDao {

    private SessionFactory sessionFactory;

    protected SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Long totalTableCount(Class<?> clazz) {
        Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(clazz);
        criteria.setProjection(Projections.rowCount());
        return (Long) criteria.uniqueResult();
    }
}

AccountDaoImpl.java

public class AccountDaoImpl extends BaseDao implements AccountDao {

    public void addAccount(Account account) {
        getSessionFactory().getCurrentSession().save(account);
    }

    public void updateAccount(Account account) {
        getSessionFactory().getCurrentSession().update(account);
    }

    public void deleteAccount(Account account) {
        getSessionFactory().getCurrentSession().delete(account);
    }

    @Override
    public Long countAccountsForUser(User user) {
        Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(Account.class);
        criteria.add(Restrictions.eq("user", user));
        criteria.setProjection(Projections.rowCount());
        return (Long) criteria.uniqueResult();
    }
}

Account.java

@Entity
@Table
public class Account {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(nullable = false)
    private String name;
    @Column(nullable = true)
    private String extraInfo;
    @Column(nullable = true)
    private Money startingBalance;

    @Column(nullable = true)
    @Temporal(TemporalType.DATE)
    private Calendar statementDate;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "account", orphanRemoval = true)
    private List<Transaction> transactions;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, optional = false)
    private User user;
}