SourceForge.net Logo
Main Overview Wiki Issues Forum Build Fisheye

  Dashboard > Compass > Struts v1, Spring, JPA (annotations) example
Struts v1, Spring, JPA (annotations) example Log In | Sign Up   View a printable version of the current page.

Added by brian mcsweeney , last edited by brian mcsweeney on Apr 08, 2008  (view change)
Labels: 
(None)

This page is aimed to show how to integrate compass with Struts v 1, with Spring, JPA (using hibernate and annotations).

I've created this example because I think that it's a common use case. I've created it in order to try to show people that compass can be easy to get started with, however I found the lack of examples a bit of a pity.

Lots of people have asked on the forums for an example of this kind, so hopefully this will be a decent starting point!

About the Example Application

The application doesn't make a lot of sense from a conceptual perspective.
It allows you to add a pet of a specific type to one or many owners and search for pets by name, description, owner and type.
Rather than concentrate on whether or not this makes any sense as an application, the emphasis is to show how to search using compass on a JPA domain model and to search by relationships within that model.

Thus it shows searching on an individual domain object, and also based on one-to-many relationships, and many-to-many relationships.

For this app wanted to try to show how to convert a simple webapp orm from using sql queries to using compass queries. However, I am quickly seeing that most webapps need join queries. I am not sure if compass supports this. For example, if I have two objects in a many-to-many relationship A and B, can i query for B's where A is a certain value? I'm not so sure...

Folder Structure

The code is built using maven version 1 and is split into a multiproject so that the backend is cleanly separated from the frontend.

sample-multi - a top level maven placeholder from which the other projects can extend
sample-services - the business logic (exposed as spring services) and JPA domain model
sample-webapp - the simple struts 1 webapp which shows how to query

System Configuration

The project assumes you have jdk 1.5 or later. Generics and annotations are used throughout.

The project also assumes maven version 1 is installed.

The project is configured to work with MySQL database. I have tested it on version 5.0
using the java mysql driver version mysql-connector-java-5.1.5-bin

Steps to Run

In order to get the system running do the following

1) Unzip the sample.zip file to your hard drive. Let's call that location %sample_home% for reference.

2) Update your maven repository

The project has dependencies on 3rd party libraries attached in the deps1 and deps2 attachments. Copy these into your local maven repo in a folder named org.compass.sample

3) Build the backend:

In a command prompt, cd into %sample_home%/sample-services

type: "maven clean build"

this builds the sample-services.jar file which contains the business layer (exposed as spring services) and jpa domain model

4) Build the frontend:

cd into %sample_home%/sample-webapp

type: "maven clean build"

this builds the sample-webapp.jar file

5) The application expects a database as configured in the file %sample_home%/sample_services/src/resources/applicationContext-services.xml

<property name="url" value="jdbc:mysql://localhost/compasssample" />
<property name="username" value="root" />
<property name="password" value="root" />

thus you must have a mysql database named compasssample created locally. Don't worry about creating any tables within the database yet.

6) Run the application

Drop the sample-webapp.jar file into the tomcat webapps directory and start tomcat.
The application should be available at http://localhost:8080/sample-webapp
Note that the database tables are automatically created by JPA (hibernate) however they are not populated yet.
To be able to use the application properly we must populate the database. To do this, run the

%sample_home%/sample-services/src/database/populatedb.sql script in the mysql database.

7) Now restart the application so these database changes can be picked up.

8) Add a pet through the wizard and then you can use the search bar at the top to search!

Site running on a free Atlassian Confluence Open Source Project License granted to compassframework.org . Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.6.2 Build:#919 Nov 26, 2007) - Bug/feature request - Contact Administrators