while this project is many years old, and only runs with a pre 11 Java, the principles still apply
A very simple example on how to use Spring and Hibernate together
There are plenty of good examples from Spring and Hibernate themselves. This one is aimed to bring the two together mostly for people who "heard" things about the two, read about it, but having a "practical difficulty" to combine them.
Well, if you take a look at APIs, you'll see a "MoneyMakingBusinessService" with a "makeMoney" method. So technically, each time you run "CleverMoneyMakingBusinessServiceIntegrationTest", you'd be making some money.
And, of course, if you master Spring & Hibernate, and find yourself a gig, you'll be making even more.
Spring and Hibernate based project that demos simple things such as:
Maven based structure ( hence can be easily used by gradle ). Configuration and property files organized under "META-INF/conf", "META-INF/props", etc..
"tx-spring-config.xml", "persistence-spring-config.xml", "service-spring-config.xml", "repository-spring-config.xml" and "application-context.xml" that includes them all
<context:property-placeholder location="classpath:META-INF/props/env.properties"/>
That is injected into AnnotationSessionFactoryBean
That are linked to the Hibernate overall config ( <mapping resource="META-INF/conf/hibernate/mapping/startup-bank-named-queries.xml"/> )
Via @Repository and "PersistenceExceptionTranslationPostProcessor"
public interface MoneyRepository {
public void make( MoneyRoll moneyRoll ); // C
public MoneyRoll find( Long id ); // R
public void update( MoneyRoll moneyRoll ); // U
public void takeOut( MoneyRoll moneyRoll ); // D
}
with a Hibernate based implementation
Declarative, on a Service Level, using "aop:config", "tx:advice" namespaces
With SpringJUnit4ClassRunner, ContextConfiguration, etc..
<jdbc:embedded-database id="dataSource" type="H2"/>
Most useful hibernate "log4j.logger" properties
"HibernateSessionNotBoundToThreadIntegrationTest" for the second (after LazyInitializationException ) most common Hibernate exception: "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here"