Skip to content
Martin Liesenberg edited this page Feb 27, 2014 · 4 revisions

Table of Contents

  1. Hibernate
  2. Data Access Object
  3. JAX-RS API 2.0

1. Hibernate

Hibernate is an open source persistence and ORM framework for Java. It is being used to map Java classes to database tables and provides means to query and retrieve data without writing SQL.
The mapping is done via annotations which is being used for attributes as well as relations between entities (many-to-one, many-to-many, etc.). In order for Hibernate to manage the persistence, the class has to provide a no arguments constructor. We used an extension to Hibernate called Hibernate Spatial which also provides support for spatial data types such as Point and MultiPolygon. Find an example below:

@Entity
@Table(name = "CONSTITUENCY") // mapping the class to the table 'constituency'
public class Constituency extends ExportableGeometry implements Serializable {
    @Id // indicating the primary key
    @GeneratedValue(strategy=GenerationType.SEQUENCE) // how is the key generated
    private long gid;

    @Column(unique=true) // enforce unique constraint on column
    private int wkr_nr;

    // spatial type using extended annotations provided by hibernate spatial
    @Type(type="org.hibernate.spatial.GeometryType")
    @Column(columnDefinition="Geometry")
    private Point centerPoint;

    ......

    // defining a 1 <-> n relation between constituency and county
    @OneToMany(fetch=FetchType.LAZY)
    @JoinColumn(name="constituency_id", referencedColumnName="wkr_nr")
    @OrderBy("areaQuota")
    private List<CountyContainsConstituency> dependingCounties = new LinkedList<CountyContainsConstituency>();
    ......

2. Data Access Object

Data Access Object UML Diagram by Oracle (Core J2EE Patterns)

The pattern is used to provide a layer of abstraction between the actual data source and the application logic using the data. By defining a couple of interfaces and providing different implementing classes for the different data sources changing the data source of an application becomes rather easy. Below a UML diagram of our DAO classes.

Class Diagramm

Thoughts on using the pattern
Though it might seem a little bit over the top to use such a rather complex structure for a small scale project, we deemed it good software engineering practice to separate data access from application logic. Moreover if a follow up project wants to measure the performance of different databases while using the application or simply decides to switch the underlying database they simply need to implement another set of Data Access Object classes.

3. JAX-RS API 2.0

With the help of JAX-RS plain Java Objects can be exposed as RESTful Web resources independent of the underlying technology using a declarative and annotation based API.

@Path("/constituency")
public class RestConstituency {
    private SpatialDAOFactory f = SpatialDAOFactory.getDAOFactory(SpatialDAOFactory.POSTGIS);
    
    .....
	
    @GET
    @Path("{id}/geometry")
    @Produces({MediaType.APPLICATION_JSON})
    public double[][][] getConstituencyGeometryById(@PathParam("id") long id) {	
        Session s = DatabaseConnection.openSession();
        // Create a DAO
        ConstituencyDAO constituencyDAO = f.getConstituencyDAO();
        constituencyDAO.setConnection(s);

	Constituency c = constituencyDAO.findConstituencyById(id);
	c.setGeometryDetail(0);
	c.getGeometryArray();
	double[][][] result = c.getGeometryArray();
		
	s.close();
	return result;
    }

    .......

@Path specifies the relative path of the function, @GET the type of method (@POST, @DELETE and @PUT are also available) and @Produces({MediaType.APPLICATION_JSON}) indicates that the method will eventually return JSON (as opposed to @Consumes which indicates that the method is passed a particular type).
Another interesting feature to note is @PathParam which is being passed to the function. It's used to identify a single instance of a constituency and binds the method parameter to a path segment.

Clone this wiki locally