* 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 testTo use the project with Eclipse, simply create the .project and .classpath files using the build tool (Maven):
maven clean eclipseAnd 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. ;)
Chatter
15 hours 11 min ago
3 days 6 hours ago
4 days 10 hours ago
6 days 35 min ago
6 days 41 min ago
6 days 9 hours ago
1 week 1 day ago
1 week 2 days ago
1 week 3 days ago
1 week 3 days ago