nono
nono

Reputation: 1702

JPA entity with GlassFish 4 and PostGIS

I'm trying to create a JPA entity with GlassFish 4 using PostgreSQL 9.1 and PostGIS 1.5.

I've created/configured a data source in GlassFish by adding the postgres-jdbc and postgis-jdbc JARs into the domain/lib directory.

I've created a simple entity MyPoint that handles a PostGIS point:

@Entity
public class MyPoint implements Serializable {
    private static final long serialVersionUID = 1595466016586244423L;

    @Id
    private int id;

    @Column(columnDefinition="Geometry")
    private Point the_geom;

    public MyPoint() {
        super();
    }   

    public int getId() {
        return this.id;
    }

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

    public Point getPoint() {
        return this.the_geom;
    }

    public void setPoint(Point p) {
        this.point = p;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Point [id=" + id + ", point=" + the_geom + "]";
    }   
}

And a point home with the createPoint() method like this:

@Stateless(name="PointHome")
public class PointHome {
    @PersistenceContext
    private EntityManager entityManager;

    public MyPoint createPoint(MyPoint point){
        entityManager.persist(point);
        return point;
    }       
}

On war deployment, the table MyPoint with columns id and the_geom are created. Column the_geom is of type geometry. Here is the table schema:

CREATE TABLE mypoint
(
    id integer NOT NULL,
    the_geom geometry,
    CONSTRAINT mypoint_pkey PRIMARY KEY (id )
)

The problem is when invoking the persist() method on the entity manager:

MyPoint z = new MyPoint();
z.setPoint(new Point(6, 43));
System.err.println(z);
zoneHome.createPoint(z);

I've got an org.eclipse.persistence.exceptions.DatabaseException.

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: org.postgresql.util.PSQLException: ERROR: parse error - invalid geometry Hint: You must specify a valid OGC WKT geometry type such as POINT, LINESTRING or POLYGON

...

Caused by: org.postgresql.util.PSQLException: ERROR: parse error - invalid geometry Hint: You must specify a valid OGC WKT geometry type such as POINT, LINESTRING or POLYGON Error Code: 0 Call: INSERT INTO MYPOINT (ID, THE_GEOM) VALUES (?, ?)

bind => [2 parameters bound] Query: InsertObjectQuery(Point [id=0, point=POINT(6 43)])

Any ideas?

Upvotes: 1

Views: 1010

Answers (1)

dzolo
dzolo

Reputation: 61

Use Hibernate Spatial extension (http://www.hibernatespatial.org/) with altered MyPoint entity:

@Entity
public class MyPoint implements Serializable {
    //...
    @Type(type="org.hibernate.spatial.GeometryType")
    private Point the_geom;
    //...
}

Upvotes: 1

Related Questions