Monday, March 8, 2010

Spring EYE on JAVA EE - 4

Once we have the basic entities ready, it is time to think about persist them into database. Among all five entity classes, Report.java is different cause it defines all two Report as static instances. The rest four will need to map to table. In this demonstration, I am going to apply Hibernate as the ORM tool for its popularity and reliability. HSQLDB is used as an in-memory database support. Start HSQLDB in another window/cmd prompt, create necessary table. Create hibernate configuration file to properly match the data source while takes advantage of the latest hibernate annotation.


hibernate.cfg.xml Now all the annotation tags in Product.java start to make sense.



As a common Java EE approach, Data Access Object (DAO) is created to bridge the calls. Notice all DAO calls do not explicitly handle transaction. It is expected that the caller will handle it.


HibernateProductDAO.java





Now it is testing time again. Testing hibernate DAO will be slightly tricky mainly for test class has to instantiate Hibernate SessionFactory programmatically.


HibernateProductDAOTest.java




Previous    Next

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home