title: Saving geolocation to Postgis database
body: <p>As a part of my project I am trying to save a java object with a geolocaton to the Postgis database, so that the Geolocation is translated to Postgis spatial data format correctly and I can use Postgis geofunctionality for simple distance or objects-in-radius search.</p>
  
  <p>The issue is - I can not find the Java data object type that can be translated to the "geometry" column of the Postgis DB. </p>
  
  <p>For now i am getting error when trying to save my test object to the database. The database table itself is created correctly when starting my application, and it saves objects without geodata, so I assume the DB-Connection is not the issue. The database is upgraded to Postgis using the extension guide on the Postgis site.
  What i have tried to solve the problem:</p>
  
  <ul>
  <li><p>Using different classes for Point creation - java Point, vividsolutions Geometry</p></li>
  <li><p>Inserting other jdbc dependencies - mysql-connector-java, PostgreSQL JDBC Driver. Also used hibernate spatial dependency for annotation manipulations.</p></li>
  <li><p>Using other dialects - different versions of PostgisDialect</p></li>
  <li><p>Using different annotations:</p>
  
  <p>@Column(columnDefinition = "geometry(Point,4326)")</p></li>
  </ul>
  
  <p>results in same error,</p>
  
  <pre><code>@Column(columnDefinition = "Geometry", nullable = true)
  @Type(type = "org.hibernate.spatial.GeometryType")
  </code></pre>
  
  <p>results in ClassNotFoundException - GeometryType.</p>
  
  <p>The current version of those are below. This seems to be the most stable configurtation giving me no errors without Point saving. The seve function used is the standard CRUD save function.</p>
  
  <p>Class declaration:</p>
  
  <pre><code>@Entity
  @Table(name = "proposals")
  public class Proposal implements Serializable {
  
  @Id
  @Exclude(CallType.REQUEST)
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "id")
  private int id;
  
  @Column(name = "name")
  @Required
  private String name;
  
  @Column(columnDefinition = "Geometry")
  private Point gis_location;//using org.postgis.Point
  
  //getter, setter, constructor
  }
  </code></pre>
  
  <p>Application.properties:</p>
  
  <pre><code>server.port=8100
  spring.datasource.url = jdbc:postgresql://localhost:5432/postgres_proposals
  spring.datasource.username = postgres
  spring.datasource.password = pass
  
  # JPA# JPA/Hibernate properties
  spring.jpa.show-sql = true
  
  spring.jpa.hibernate.ddl-auto = update
  
  spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
  spring.jpa.hibernate.naming.strategy = org.hibernate.cfg.ImprovedNamingStrategy
  </code></pre>
  
  <p>Gradle dependencies:</p>
  
  <pre><code>dependencies {
  compile('org.springframework.boot:spring-boot-starter-data-jpa')
  compile('org.springframework.boot:spring-boot-starter-data-rest')
  compile('org.springframework.boot:spring-boot-starter-jersey')
  compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.8.0-beta2'
  compile group: 'org.slf4j', name: 'slf4j-api', version: '1.8.0-beta2'
  compile group: 'net.postgis', name: 'postgis-jdbc', version: '2.3.0'
  runtime ('net.postgis:postgis-jdbc:2.3.0')
  testCompile('org.springframework.boot:spring-boot-starter-test')
  testCompile group: 'org.mockito', name: 'mockito-core', version: '2.21.0'
  testCompile 'junit:junit:4.12'
  }
  </code></pre>
  
  <p>Test object creation:</p>
  
  <pre><code>Proposal p = new Proposal();
  p.setId(0);
  p.setName("prop1");
  Point point = new Point(50,8.5,12);
  p.setGis_location(point);
  proposalRepository.save(p);
  </code></pre>
  
  <p>Currently I am having error when trying to save my test object to the database - only when i try saving wis gis_location Point. When commenting p.setGis_location(point); the applications starts normally and the object appears in the DB - without the geodata ofc. Running application with this line uncommented results in following error:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'proposalApplication': Invocation of init method failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement
  Caused by: org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement
  at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333)
  at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
  at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
  at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:518)
  at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292)
  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
  at com.sun.proxy.$Proxy91.save(Unknown Source)
  at service.proposal.ProposalApplication.init(ProposalApplication.java:49)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
  at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
  at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
  ... 18 more
  Caused by: org.hibernate.exception.GenericJDBCException: could not execute statement
  at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
  at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
  at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
  at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
  at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
  at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2949)
  at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3449)
  at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
  at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:582)
  at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:456)
  at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
  at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
  at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1282)
  at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:465)
  at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2963)
  at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2339)
  at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
  at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)
  at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
  at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
  at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
  at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:61)
  at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
  ... 42 more
  Caused by: org.postgresql.util.PSQLException: ERROR: Invalid endian flag value encountered.
  at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455)
  at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2155)
  at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:288)
  at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:430)
  at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356)
  at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:168)
  at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:135)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
  at com.sun.proxy.$Proxy96.executeUpdate(Unknown Source)
  at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
  ... 61 more
  </code></pre>
  
  <p>The line </p>
  
  <p>service.proposal.ProposalApplication.init(ProposalApplication.java:49)</p>
  
  <p>mentioned in the error is exatly the save() function.</p>
  
  <p>I am thankfull for any suggestions how i could fix this error.</p>