For the past several years most of my projects were based on Spring and Hibernate. And what I keep seeing is how people struggle to understand how the two are working together.
So going from project to project, I have to repeat the same spiel over and over again to get people up to speed.
Since I believe that the most effective way to LEARN is to DO, I create simple examples for people to play with / to complete / to analyze, etc..
So “Money Making Project” is one of such examples. It comfortably lives at github’s luxury apartments, so anybody can “git clone” / “fork” and play with it.
Besides making money, this project also demos things such as:
A way to structure a project
Maven based structure ( hence can be easily used by gradle ). Configuration and property files organized under “META-INF/conf”, “META-INF/props”, etc..
A way to separate Spring configs
“tx-spring-config.xml”, “persistence-spring-config.xml”, “service-spring-config.xml”, “repository-spring-config.xml” and “application-context.xml” that includes them all
Properties via PropertyPlaceholderConfigurer
<context:property-placeholder location="classpath:META-INF/props/env.properties"/> |
Hibernate overall configuration file
That is injected into AnnotationSessionFactoryBean
Hibernate Named Queries
That are linked to the Hibernate overall config
<mapping resource="META-INF/conf/hibernate/mapping/startup-bank-named-queries.xml"/> |
Spring’s DAO / Hibernate Exception Translation
Via @Repository and “PersistenceExceptionTranslationPostProcessor”
Simple CRUD Repository
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
Transaction Management with Spring AOP
Declarative, on a Service Level, using “aop:config”, “tx:advice” namespaces
Spring Testing
With SpringJUnit4ClassRunner, ContextConfiguration, etc..
Using Embeded in-memory H2 Database for Testing
<jdbc:embedded-database id="dataSource" type="H2"/> |
Hibernate Logging
Most useful hibernate “log4j.logger” properties
Demoing how important Transaction is for Hibernate Sessions
“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”