user5061573
user5061573

Reputation:

How to store geometry point in Oracle using java with hibernate?

I need to store a POINT in Oracle, and I don't know what type to use. First, I tried String (using the format POINT(123, 456) because that's the format Oracle shows me. But it didn't work.

I have this class:

package model;

import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="teste")
public class Test {

    @Id
    @Column(name="id")
    private Integer id;

    @Column(name="timestamp")
    private Timestamp timestamp;

    @Column(name="geometry")
    private String point; // <-- I don't know which type to use here.

// constructors, getters and setters
}

Does someone know what's the correct type here?

EDIT: I've followed the tutorial indicated by @Zia and now, I got this error:

Exception in thread "main" java.lang.UnsupportedOperationException
    at org.hibernate.spatial.GeometrySqlTypeDescriptor.getBinder(GeometrySqlTypeDescriptor.java:52)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281)
    at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2843)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3121)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)

One thing that I added to the code was:

@Column(name="geometry", columnDefinition="SDO_GEOMETRY")
@Type(type="org.hibernate.spatial.GeometryType")
private String point;

But I don't know if I have to use SDO_GEOMETRY or GEOMETRY. Both are giving me the same exception.

Upvotes: 0

Views: 2467

Answers (2)

Zia
Zia

Reputation: 1011

You can use the below

@Type(type="org.hibernate.spatial.GeometryType")

its from hibernate spatial api.. for more details have look into below link

http://www.hibernatespatial.org/

http://www.hibernatespatial.org/documentation/02-Tutorial/01-tutorial4/

Dont forget to add the SpatialDialect in your configuration file.for Mysql it would be like

org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect

Upvotes: 1

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

The best is to use two columns x and y and save the coordinates of the point

@Column(name="x")
private int x; 

@Column(name="y")
private int y; 

Saving the point in two different coordinates will give you:

  • easy access to coordinates without parsing them
  • easy build to queries like find all points left to the origin (0, 0)
  • possibility to add indexes on x and y to find points faster

Spatial types of oracle are not standard types so there is no equivalent mapping to a java type as for INT, DATE, VARCHAR or similar.

To map to it you need to use the java type JGeometryType. Here a nice tutorial to do that.

Upvotes: 1

Related Questions