JPA and HSQL example (Spring and Non-Spring)

Many times when I start to work on a "sample" app for any purpose, I end up at a point where I need a database, and then once I have a database I want to use JPA. In the past I have used MySQL and so on, but to do that you have to instruct users of the sample to create a schema and user/pass, etc. It is often just easier to use an embedded db, such as HSQL (HSQLDB) or Derby (and I am still an HSQL user, sorry). Today I ended up in this predicament again and decided to, get this, save the sample so I could reuse it again later rather than do this over and over again ;). Hence my jpabasic starting point example. This is far from perfect, so don't bust my chops, it get's the caveat emptor disclaimer for sure. Yet, it may serve as a good place to begin for those just starting with:

* JPA * JPA + Spring * JPA + HSQL * JPA + Toplink * AllOfTheAbove + Maven

. . . and or as a template for those that already know this stuff and just need to prime another project again (and if you have suggestions or contributions please pass them along, I will put them in the project). The project intentionally does just a single JPA bean (no relationships) in both "plain" and "Spring" fashion. I am still a non-Spring blasphemer. I know it's cool, it can inject everything, do AOP, bake you a cake, mow your lawn, I know, I know. Still I often see it used where there are simpler ways, even simpler ways to "inject," especially in libraries, and that drives me nuts. If you want more, and better, Spring info check out Getting Started With JPA in Spring 2.0 and Using JPA in Spring without referencing Spring. Anyway, back to it. The Spring version of the Data Access Object (DAO), UserDAO , uses the Spring JpaTemplate, it looks like this:
public class UserDao extends JpaDaoSupport implements IUserDao
{  
   public User get(long id) throws DataException
   {
       return getJpaTemplate().find(User.class, id);       
   }
. . .
The non Spring stuff of course has to create the EntityManagerFactory and EntityManager on its own. The BaseDao object handles the factory like this:
public BaseDao()
    {   
        emf = Persistence.createEntityManagerFactory("sample");
        LOG.debug("EntityManagerFactory created");
    }
And then each individual non-Sprung DAO, like this UserDao, does the manager (running outside of a container resource local, no JTA, etc), as follows:
. . .
 private EntityManager manager;
    public UserDao()
    { 
        super();        
        this.manager = emf.createEntityManager();
        LOG.debug("userDao constructed");
        LOG.debug("EntityManager created");
    }
. . . 
Each of the DAO objects, Spring and plain, implement the IUserDAO interface:
public interface IUserDao
{    
    public User get(long id) throws DataException;
    public User get(String name) throws DataException; 
    public List getAll() throws DataException; 
    public void save(User user) throws DataException;
    public User update(User user) throws DataException;    
    public void remove(User user) throws DataException;    
}
It's really basic stuff, on purpose, no depth. Each approach uses JPA to persist, retrieve, and so on. The point is that it demonstrates JPA with HSQL (using Toplink as the JPA provider). A handy combination. Couple that with Spring or plain beans, and with a Maven 1.x build (I still prefer Maven 1 as well). The project is in a svn repo, so it can be checked out and run fairly easily (many of the libs are checked in, because I distribute the artifact as one shot, and needed it that way, ideally Maven should be asked to handle those to - may happen in the future). Because it runs Maven the structure will be familiar to some, and foreign to others. The persistence.xml file, and the Spring config, are both located in src/main/resources . If you want to run the tests from the command line, get Maven 1.x, and try:
maven clean test
To use the project with Eclipse, simply create the .project and .classpath files using the build tool (Maven):
maven clean eclipse
And then you can import the project as an "Existing" element within Eclipse. From there you can run the tests right in the IDE if you prefer. It ain't much, but at least now, if nothing else, I will have a personal reference other than gmail for every time I need to go find the POM for JPA Toplink/HSQL/Spring and so on. ;)