Calendar
QuicksearchBookmarksMy del.icio.us
ArchivesCategoriesSyndicate This BlogBlog Administration |
Wednesday, November 21. 2007Creating an application with maven2, jpa, springframework and intellij
Some time ago I started working with intellij, recently I bought a mac, and now I want to start programming. But where to start? There are some mandatory things, or not? Well let’s just start and try to make it as flexible as possible so we can use other frameworks for some layers. For now I will focus on the following technologies:
When creating the sample I started of programming in Intellij. Even with the excellent integration of JPA and Hibernate I still had a lot op problems creating the sample. Damn this is not easy. So what do you do when you are stuck? You start reading, I already had some books that could help me out. Still I like the “Java Persistence with Hibernate” book the best. The similarities between hibernate and JPA enabled me to quicker learn the basics. So I started reading, I came to the conclusion I had or wanted to start using annotations. It struck me how well Intellij was helping me again. Just a few tips when developing with intellij, maven, jpa, spring, etc. Create a new project based on an external model, click next and choose maven. Click next again and browse to the folter containing the master pom file. Click next and you have the option to select the project. The finish will create your new project with all modules present. ![]() ![]() By default the spring and jpa facets are not present. You need to add them to the modules. Open the module settings (right mouse click and select menu item) and add the plus sign for adding a facet. The following screen shows the result of adding the spring facet and creating a new File Set. Another cool thing is that you can create a datasource to a database and connect this to a persistent unit. Columns and tables are then checked when creating your annotations. ![]() I like the intellij way of creating new projects, but still I am a maven adept, so I wanted to have it all running with maven. You can find the sample online again. Beware, it is work in progress. I am adding a front end and more logic the coming weeks. Check: http://gridshore.googlecode.com/svn/trunk/RaffleApp/ If you want to start with jpa, spring and maven I can recommend the following archetype: http://www.rateyourwriting.com/downloads/jpa-hibernate-archetype.tar.gz I did not try it out myself, but it looks very good. It is create by Chris Maki (http://www.jroller.com/cmaki/category/JPA+101) In the reference manual of the springframework I found a sentence that for new applications they prefer not to use the JpaDaoSuppor class. There is another way based on the special bean PersistenceAnnotationBeanPostProcessor. Using this bean you can use annotations like : @Entity, @PersistenceContext from within the spring container. Back to my problem, for some reason my tests using the AbstractJpaTests did not work with the separate jars for domain and data access (dao). I started by combining them in one module. But I was not satisfied. So I went back to my friend Google. I typed in a query for information about using modules with jpa and springframework. To my surprise I got back a lot of articles about the debate between using DAO’s or not. Yes we are back to the discussion about DDD or Domain Driven Design. After reading a lot of posts I guess the following sentence found on a blog item from Craig Wickesser but written by Adam Bien: “I would say: it depends. It depends how complex your application really is. ” Now back to the original problem, how to design a application using JPA? Some of the choices you have to make:
In the end I did manage to get back to my so well known structure of a DAO layer and domain objects. What I had to do? We will check the persistence.xml file later on. I had to add the specific classes in this file. Every problem needs a sample Let me first explain a bit about the example that I am going to use. For a technology evening at my company I had some books that I could give away to the people present. Since it was an evening about the Google Web Toolkit I created a nice front end application using a file as storage to a Raffle application. Check the following application if you want to see it: http://gridshore.googlecode.com/svn/trunk/Raffle/ I decided this file thing was not really optimal, so I wanted to create a database backed application. What to use? Well let’s try it out JPA, hibernate started to get boring and I wanted to move on. So what is in the raffle applications domain model ![]() Some rules:
We begin with the project setup using maven: First the dependencies that you need in your pom: spring-aop, spring-dao, spring-jpa, spring-hibernate3, hibernate-entitymanager, c3p0, mysql-connector-java, concurrent, spring-mock That’s about it, put that in the pom.xml, create the structure and do a mvn clean install, or ... Fire up intelij, create a new project based on this pom.xml and you can do the same things as I showed before. We’ll begin with the domain objects:
Next step is to create a DAO interface that does not have anything to do with JPA, I use the interface like this:
For now I will not focus on the implementation, I am not a JPA expert yet
Next step is to configure the spring beans:
Finally we need to configure the the entity manager, this is done with a special file named persistence.xml which is displayed beneath.
That is enough to start using the application. However, there is one thing I forget. What about testing this application. Spring comes with a special class for testing called AbstractJpaTests. This is class had familiar functionality, auto dependency injection and rolling back a transaction after the test. Using this test you still need a database. For the data access tests I am using a combination of dbunit, springframework and hsqldb. Most important part of the test class is the injection of the dao:
The following beans are in the special test spring config file together with the datasource for the test database.
That is about it, again, check the source code, everything is runnable by maven2 and tested on tomcat 5.5. If you want to read more, I used the following resources while creating learning about jpa and the other topics. Links: http://www.infoq.com/news/2007/09/jpa-dao http://debasishg.blogspot.com/ http://www.vitarara.org/cms/struts_2_cookbook/integration_testing http://dev2dev.bea.com/pub/a/2006/03/jpa-spring-medrec.html?page=6 http://www.jroller.com/cmaki/entry/domain_model_revisited Friday, November 16. 2007Loading properties using JNDI within the springframework deployed on tomcat
Hai All,
finally a new technical item. It is a short item, but I have been struggling to get this to work. Therefore I want to share this so other can use it to configure there properties in a flexible way. Problem: Properties like database url's, userid's and pasword's are different in your test environment and your production environment. There are other items you might want to configure. Solution: You can make complicated deployment scripts that get the right property files for the right environment. But you need to create different packages for different environments. I think a more elegant solution is to use the JNDI parameters to load the specific environmental values. If there are a lot a good way might be to store them in a database and only configure the database access parameters using JNDI. The following solution is based on two posts in different forums: http://forum.java.sun.com/thread.jspa?threadID=647327&messageID=3810991 - gives details about the configuration of tomcat. http://forum.springframework.org/showthread.php?t=30991 - gives details about the spring configuration Now the code, we start with the tomcat configuration. The server.xml file contains all the actual values connected to the exposed JNDI parameter names. server.xml
We need to tell the application the names of the parameters that are available. We must add the following items to the web configuration file. web.xml
Of course you need to do this for all four parameters. Then the tricky part. This is the one I missed while getting this to work. You need to add a tomcat specific deployment configuration with the following lines META-INF/context.xml
Thats is all we need to do to make these values available to the spring beans. So the last part is the spring configuration. We are going to create a PropertyPlaceHolderConfigurer. That way you can access the properties just like properties from a file. spring-config.xml
This made my deployment life a lot easier, hope you will find some good use of it. Thanks again to contributers on the forum for making the different parts clear to me. Till next time, which is going to be a piece about JPA. greetz Jettro Coenradie Monday, November 5. 2007I have a Mac
Wow, I have finally made the decision, I bought a Mac. Now I am the proud owner of a MacBook pro. So the next blog entries will be written on my new Mac. I think I will loose time at first, new key board, new mouse, new platform. But IntelliJ is running already. I have my mail setup, importing i-tunes now. To bad it is time to go to bed. I understood, I need to read the following website http://www.macmiep.nl, if you have better options, please let me know.
Now I say, goodnight, I am off with a very big smile.
(Page 1 of 1, totaling 3 entries)
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||

