How to fix “ERROR: Invalid endian flag value encountered.” during saving (Geometry type) data in PostgreSQL through Hibernate

fg I was trying to save data into the PostgreSQL through Hibernate but whenever I try to save “Location” field of “Geometry” data type, it gives me

ERROR: Invalid endian flag value encountered.

This error arises only when I’m trying to set Location of Geometry type. If I comment the line tempStudent.setLocation((Point) geom) , the record is successfully saved into database. I am not able to understand where I’m getting wrong in saving the Location.

The SQL statement executed by my code:

 insert into student (email, first_name, last_name, location) values ($1, $2, $3, $4)

The parameter values:

binding parameter [1] as [VARCHAR] - []
binding parameter [2] as [VARCHAR] - [abc1]
binding parameter [3] as [VARCHAR] - [abc2]
binding parameter [4] as [VARBINARY] - [POINT (7 2)]

The solution suggested in PostGIS Geometry saving: “Invalid endian flag value encountered.” isn’t working for me maybe because I’m using newer versions compared to the ones specified in that post. The versions I am using are:

  • spring-5.0.8
  • hibernate-c3p0-5.4.2
  • hibernate-spatial-5.4.2
  • jts-1.13
  • PostgreSQL 10
  • PostGIS Bundle 2.5.2

package com.luv2code.hibernate.demo.entity;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;

public class Student implements Serializable {

    private int id;

    private String firstName;

    private String lastName;

    private String email;

    @Column (name="location", columnDefinition="geometry(Point,4326)", nullable=true)
    private Point location;

    public Student() {


    public Student(String firstName, String lastName, String email, int id, Point location) {
        this.firstName = firstName;
        this.lastName = lastName; = email; = id;
        this.location = location;

    public Point getLocation() {
        return location;

    public void setLocation(Point location) {
        this.location = location;

    public int getId() {
        return id;

    public void setId(int id) { = id;

    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;

    public String getEmail() {
        return email;

    public void setEmail(String email) { = email;

    public String toString() {
        return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email
                + ", location=" + location + "]";


package com.luv2code.hibernate.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.luv2code.hibernate.demo.entity.Student;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

public class CreateStudentDemo {

    public static void main(String[] args) {
        CreateStudentDemo mgr = new CreateStudentDemo();
        if(args[0].equals("store")) {

    private void storeLocation(String wktPoint) {
        WKTReader fromText = new WKTReader();
        Geometry geom = null;
        try {
            geom =;
        } catch (ParseException e) {
            throw new RuntimeException("Not a WKT string: "+wktPoint);
        if (!geom.getGeometryType().equals("Point")) {
            throw new RuntimeException("Geometry must be a point. Got a " + geom.getGeometryType());

        // create session factory
        SessionFactory factory = new Configuration()

        // create session
        Session session = factory.getCurrentSession();
        try {           
            // create a student object
            System.out.println("Creating new student object...");
        catch (Exception e) {
            // TODO: handle exception
        // start a transaction

        // Student tempStudent = new Student("Paul1", "Doe1", "", 5, (Point)geom);

        Student tempStudent = new Student();
        tempStudent.setLocation((Point) geom);

        /*GeometryFactory geometryFactory = new GeometryFactory();
        Point point = geometryFactory.createPoint(new Coordinate(10,7));

        System.out.println("Saving the student...");;

        // commit transaction

    private static String assemble(String[] args) {
        // TODO Auto-generated method stub
        StringBuilder builder = new StringBuilder();
        for(int i=1; i<args.length; i++) {
            builder.append(args[i]).append(" ");
        return builder.toString();


<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"



        <!-- JDBC Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5433/ats</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">shivam</property>

        <!-- JDBC connection pool settings ... using built-in test pool -->
        <property name="connection.pool_size">1</property>

        <!-- Select our SQL dialect -->
        <property name="dialect">org.hibernate.spatial.dialect.postgis.PostgisDialect</property>

        <!-- Echo the SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Set the current session context -->
        <property name="current_session_context_class">thread</property>



Error trace:

INFO: HHH000400: Using dialect: org.hibernate.spatial.dialect.postgis.PostgisDialect Creating new student object…

Student [id=0, firstName=Paul1, lastName=Doel1,, location=POINT (7 2)]
Saving the student...
Hibernate: insert into student (email, first_name, last_name, location) values (?, ?, ?, ?)
Jul 15, 2019 10:04:08 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: XX000
Jul 15, 2019 10:04:08 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ERROR: Invalid endian flag value encountered.
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(
    at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(
    at org.hibernate.engine.spi.ActionQueue.execute(
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(
    at org.hibernate.engine.spi.ActionQueue.addAction(
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(
    at org.hibernate.internal.SessionImpl.fireSave(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(
    at com.sun.proxy.$ Source)
    at com.luv2code.hibernate.demo.CreateStudentDemo.storeLocation(
    at com.luv2code.hibernate.demo.CreateStudentDemo.main(
Caused by: org.postgresql.util.PSQLException: ERROR: Invalid endian flag value encountered.
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(
    at org.postgresql.core.v3.QueryExecutorImpl.execute(
    at org.postgresql.jdbc.PgStatement.executeInternal(
    at org.postgresql.jdbc.PgStatement.execute(
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(
    at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(
    ... 29 more

I don’t know why Hibernate sends the string POINT (7 2) as VARBINARY , but maybe that is the main problem. So, I’m confused if it is a bug in Hibernate or something is wrong in the code. Please provide necessary help.

did you manage to get this working?

I had the same issue and I mentioned the Postgis Dialect in the It was solved after I added the dialect to the instead.