[postgis-users] PostGIS - hibernate - EJB3

Norman Barker nbarker at ittvis.com
Wed Aug 16 21:04:11 PDT 2006


Hi,

I have searched all over the place for this, and no one has posted a solution , just the same question !! - apologies if you have already solved this, hope it helps someone.

This allows you to use spatial types from within the EJB3 Java Persistence Architecture.

The solution is as follows (only tested in write mode so far);

EJB3 annotation (the @Type hibernate annotation is key here)

	@Type(type = "org.osgeo.geodata.hibernate.GeometryType")
	@Column(name="extents")
	public Geometry getExtents()
	{
		return extents;	
	}

and then GeometryType looks like this

package org.osgeo.geodata.hibernate;

import java.io.Serializable;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import org.postgis.Geometry;
import org.postgis.binary.BinaryParser;
import org.postgis.binary.BinaryWriter;

/**
 * @author nbarker $date 16/8/06
 */
public class GeometryType implements UserType {
	 private static final int[] SQL_TYPES = { Types.BLOB };
	

	/**
	 * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
	 */
	public Object deepCopy(Object value) throws HibernateException {
		return value;
	}

	/**
	 * @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
	 */
	public boolean equals(Object x, Object y) throws HibernateException {
		if (x == y) {
			return true;
		} else if (x == null || y == null) {
			return false;
		} else {
			return x.equals(y);
		}
	}

	/**
	 * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
	 */
	public int hashCode(Object arg0) throws HibernateException {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * @see org.hibernate.usertype.UserType#isMutable()
	 */
	public boolean isMutable() {
		return false;
	}

	/**)
	 * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
	 */
	public Object nullSafeGet(ResultSet resultSet, 
		      String[] names, Object owner) throws HibernateException, SQLException {
	    Geometry result = null;
	    Blob geomAsBlob = resultSet.getBlob(names[0]);
	    if (!resultSet.wasNull()) {
	      BinaryParser bp = new BinaryParser();
	      result = geomAsBlob == null 
	        ? null 
	        : bp.parse(geomAsBlob.getBytes(1, (int)geomAsBlob.length()));
	    }
	    return result;
	}

	/**
	 * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
	 */
	public void nullSafeSet(PreparedStatement statement, 
		      Object value, int index) throws HibernateException, SQLException {
	    if (value == null) {
	        statement.setBytes(index, null);
	      } else {
	    	BinaryWriter bw = new BinaryWriter();
	    	
	    	byte[] bytes = bw.writeBinary((Geometry)value);
	        statement.setBytes(index, bytes);
	      }
	}

	/* (non-Javadoc)
	 * @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
	 */
	public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.hibernate.usertype.UserType#returnedClass()
	 */
	public Class returnedClass() {
		return Geometry.class;
	}

	/**
	 * @see org.hibernate.usertype.UserType#sqlTypes()
	 */
	public int[] sqlTypes() {
		return GeometryType.SQL_TYPES;
	}

	/**
	 * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
	 */
	public Object assemble(Serializable cached, Object owner) throws HibernateException {
		return cached;
	}
	
	/**
	 * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
	 */
	public Serializable disassemble(Object value) throws HibernateException {
		return (Serializable)value;
	}

}




-----Original Message-----
From: postgis-users-bounces at postgis.refractions.net on behalf of Norman Barker
Sent: Mon 8/7/2006 4:08 PM
To: PostGIS Users Discussion
Subject: [postgis-users] PostGIS - hibernate - EJB3
 
Hi,

I am trying to use EJB3 in Java JBoss (which is hibernate behind the
scenes),

I have marked up a property as follows 

	@Column(name="extents")
	public Geometry getExtents()
	{
		return extents;	
	}

And my data source looks like

<datasources>
  <local-tx-datasource>
    <jndi-name>GeoDataDS</jndi-name>
 
<connection-url>jdbc:postgresql://127.0.0.1:5432/geometa</connection-url
>
    <driver-class>org.postgis.DriverWrapper</driver-class>
    <user-name>xxx</user-name>
    <password>xxx</password>
      <metadata>
         <type-mapping>PostgreSQL 8.1</type-mapping>
      </metadata>
  </local-tx-datasource>
</datasources>

Where extents is of type org.postgis.Geometry, when I tell the
entityManager to commit this I get invalid geometry error even when I
commit an empty polygon, or  simple linestring such as 

LineString ls = new LineString("LINESTRING(191232 243118,191108
243242)");

If anyone has done this with EJB3 I would be really grateful for some
advice, but if you have done it with hibernate I would also be
interested as they are pretty similar.

Many thanks,

Norman
_______________________________________________
postgis-users mailing list
postgis-users at postgis.refractions.net
http://postgis.refractions.net/mailman/listinfo/postgis-users


-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 5091 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20060817/91fc9551/attachment.bin>


More information about the postgis-users mailing list