Skip to content

Commit

Permalink
Merged example from hibernatespatial tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianperruolo committed Apr 18, 2015
1 parent 4fdfe54 commit 6f693b4
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
.classpath
.project
target/
/.settings
41 changes: 40 additions & 1 deletion pom.xml
Expand Up @@ -10,15 +10,54 @@
<artifactId>spring-data-jpa</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>4.3</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.0.Final</version>
<version>4.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1200-jdbc4</version>
</dependency>
<dependency>
<groupId>org.postgis</groupId>
<artifactId>postgis-jdbc</artifactId>
<version>1.5.2</version>
</dependency>
</dependencies>
<!-- add repositories for JTS and Hibernate Spatial and Hibernate (JBoss) -->
<repositories>
<repository>
<id>OSGEO GeoTools repo</id>
<url>http://download.osgeo.org/webdav/geotools</url>
</repository>
<repository>
<id>Hibernate Spatial repo</id>
<url>http://www.hibernatespatial.org/repository</url>
</repository>
<repository>
<id>JBOSS</id>
<url>https://repository.jboss.org/nexus/content/repositories/releases/</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
45 changes: 45 additions & 0 deletions src/main/java/com/devcrumb/App.java
@@ -1,11 +1,18 @@
package com.devcrumb;

import java.util.Date;
import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.devcrumb.dao.EventDao;
import com.devcrumb.dao.PersonDao;
import com.devcrumb.model.Event;
import com.devcrumb.model.Person;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

/**
* Standalone application with Spring Data JPA, Hibernate and Maven
Expand All @@ -16,6 +23,18 @@ public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
EventDao eventDao = context.getBean(EventDao.class);
Event myEvent = createAndStoreEvent("My Event", new Date(), "POINT(10 5)");
eventDao.save(myEvent);

System.out.println("Count Event records: " + eventDao.count());

List<Event> events = (List<Event>) eventDao.findAll();
for (Event event : events) {
System.out.println(event);
}


PersonDao dao = context.getBean(PersonDao.class);

Person peter = new Person("Peter", "Sagan");
Expand Down Expand Up @@ -52,4 +71,30 @@ public static void main(String[] args) {

context.close();
}

private static Event createAndStoreEvent(String title, Date theDate, String wktPoint) {
Geometry geom = wktToGeometry(wktPoint);

if (!geom.getGeometryType().equals("Point")) {
throw new RuntimeException("Geometry must be a point. Got a " + geom.getGeometryType());
}


Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
theEvent.setLocation((Point) geom);
return theEvent;
}

private static Geometry wktToGeometry(String wktPoint) {
WKTReader fromText = new WKTReader();
Geometry geom = null;
try {
geom = fromText.read(wktPoint);
} catch (ParseException e) {
throw new RuntimeException("Not a WKT string:" + wktPoint);
}
return geom;
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/devcrumb/dao/EventDao.java
@@ -0,0 +1,16 @@
package com.devcrumb.dao;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.devcrumb.model.Event;

/**
* Person dao interface
*
* @author DevCrumb.com
*/
@Repository
public interface EventDao extends CrudRepository<Event, Long> {

}
66 changes: 66 additions & 0 deletions src/main/java/com/devcrumb/model/Event.java
@@ -0,0 +1,66 @@
package com.devcrumb.model;

import com.vividsolutions.jts.geom.Point;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

import javax.persistence.*;
import java.util.Date;

@Entity
public class Event {

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Long id;

private String title;

private Date date;

@Type(type = "org.hibernate.spatial.GeometryType")
private Point location;

public Event() {
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Point getLocation() {
return this.location;
}

public void setLocation(Point location) {
this.location = location;
}

@Override
public String toString() {
return "Event [id=" + id + ", title=" + title + ", date=" + date
+ ", location=" + location + "]";
}

}
2 changes: 1 addition & 1 deletion src/main/resources/applicationContext.xml
Expand Up @@ -36,7 +36,7 @@
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.dialect">org.hibernate.spatial.dialect.postgis.PostgisDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
Expand Down

0 comments on commit 6f693b4

Please sign in to comment.