[postgis-users] SpatialEJB3: (PostGIS and Hibernate)
Andrew Hughes
azza at lisasoft.com
Wed Dec 20 15:46:41 PST 2006
An HTML attachment was scrubbed...
URL: http://lists.refractions.net/pipermail/postgis-users/attachments/20061221/6d81e057/attachment.html
-------------- next part --------------
package com.lisasoft.routedirections.ard.beans;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.apache.log4j.Logger;
import org.postgis.Geometry;
import org.postgis.Point;
@Stateless
public class RouteDirectionsARDBean implements RouteDirectionsARD {
private final Logger log = Logger.getLogger(RouteDirectionsARDBean.class);
/**
* This is part of the Bean Management
*/
@PersistenceContext
private EntityManager manager;
/**
* Default constructor for the Session Bean.
*/
public RouteDirectionsARDBean() {
super();
}
/**
*
*/
public Location getLocation(String name) {
final String QUERY_STR = "select location from Location as location where location.name = :nameToken";
Location retLocation = null;
try {
final Query query = manager.createQuery(QUERY_STR);
query.setParameter("nameToken", name);
retLocation = (Location) query.getSingleResult();
} catch (Exception e) {
log.error("Problem with Hibernate SQL: ", e);
}
return retLocation;
}
/**
* save a location
* @param loc
* @return
*/
public boolean setLocation(final Location loc) {
boolean retBoolean = false;
try {
//NATIVE SQL QUERY
//final String INSERT_STR = "INSERT INTO Location (id, name, geom) values (nextval('hibernate_sequence'), '"+loc.getName()+"', "+this.getTheGeomFromText(loc.getGeom())+")";
//final String INSERT_STR = "INSERT INTO Location (id, name) values (nextval('hibernate_sequence'), '"+loc.getName()+"')";
//Query query = manager.createNativeQuery(INSERT_STR);
//int result = query.executeUpdate();
//log.debug("Number of Locations INSERTED in table: "+result);
//HIBERNATE MANAGED MERGE
manager.persist(loc);
manager.flush();
//manager.merge(loc); //need to use native query!
retBoolean = true;
} catch (Exception e) {
log.error("Problem with Hibernate SQL: ", e);
}
return retBoolean;
}
private String getTheGeomFromText(Geometry geometry) {
String returnVal = "null";
if (geometry instanceof Point) {
Point point = (Point) geometry;
returnVal = "GeomFromText('POINT("+point.getX()+" "+point.getY()+")',4283)";
}
return returnVal;
}
}
-------------- next part --------------
/**
*
*/
package com.lisasoft.routedirections.ard.beans;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.Type;
import org.postgis.Geometry;
@Entity
final public class Location implements Serializable
{
/**
* Hibernate primary key
*/
private long id;
/**
* The locations name
*/
private String name;
/**
* This is the postgis Geometry field
* @see org.postgis.Point
*/
private Geometry geom;
/**
* Serializable ID
*/
private static final long serialVersionUID = 1L;
/**
* Default Constructor for Addresses.
*/
public Location() {
super();
}
/**
* Create a new Location
* @param name self explainitory
* @param geometry this is a 2 Dimensional Point
* @see org.postgis.Point
*/
public Location(String name, Geometry geom) {
//public Location(String name, Geometry theGeom) {
this();
this.name = name;
this.geom = geom;
}
/**
* @return Returns the id.
*/
@Id
@GeneratedValue
public long getId() {
return id;
}
/**
* @param locationId
* The id to set.
*/
public void setId(final long locationId) {
this.id = locationId;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
@Type(type = "org.postgis.hibernate.GeometryType")
@Column(name="geom", columnDefinition="Geometry")
public Geometry getGeom() {
return geom;
}
public void setGeom(Geometry geom) {
this.geom = geom;
}
/**
* Overwritten toString method
*
* @return String
*/
public String toString()
{
final StringBuffer retString = new StringBuffer(35);
retString.append("ID: ");
retString.append(id);
retString.append(", Name: ");
retString.append(name);
retString.append(", Geometry: ");
retString.append(""+geom);
return retString.toString();
}
}
-------------- next part --------------
package com.lisasoft.routedirections.ard.beans;
import javax.ejb.Remote;
/**
* BuddyFinder.java
* <br />
* This is the BuddyFinder Session Bean Interface.
* It provides all of the methods for interacting with the Entity beans
* which in turn persist state to the database.
* <br />
* Copyright 2006, LISAsoft Pty Ltd.
* <br />
*/
@Remote
public interface RouteDirectionsARD
{
/**
* get the location
* @param lat
* @param lon
* @return the location in an object of type Location
*/
Location getLocation(String name);
boolean setLocation(final Location location);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: postgres-ds.xml
Type: text/xml
Size: 1925 bytes
Desc: not available
Url : http://lists.refractions.net/pipermail/postgis-users/attachments/20061221/6d81e057/postgres-ds.xml
-------------- next part --------------
A non-text attachment was scrubbed...
Name: persistence.xml
Type: text/xml
Size: 327 bytes
Desc: not available
Url : http://lists.refractions.net/pipermail/postgis-users/attachments/20061221/6d81e057/persistence.xml