5

I am trying to persist one of my JPA/EclipseLink models, one attribute location of type org.postgresql.geometric.PGpoint is not inserting correctly.

My model looks something like this:

@Entity
@NamedQuery(name="Entity.findAll", query="SELECT * FROM Entity e")
public class Entity {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name="name")
private String name;

@Column(name="location")
private PGpoint location;

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

@Column(name="user_id")
private Integer user_id;

/* getters and setters */

I am getting this exception:

Internal Exception: org.postgresql.util.PSQLException: 
ERROR: column "location" is of type point but expression is of type bytea

I have enabled query logging and determined that the problem seems to be caused by malformed SQL. All attributes following location seem to be grouped together into a bytea array. Specifically, JPA is trying to execute an insert like this:

INSERT INTO ENTITY (name, location, date, user_id) 
VALUES ("my_name", [B26bf8d677, "2017-05-12 12:33:00.0", 1])

That is, it seems to be trying to insert the last three attributes all into the location column.

I've also tried storing location as type String, which works on one of my other models that only reads location values from the database and never inserts them. That seems to fix the malformed SQL issue since the inserts look like this:

INSERT INTO ENTITY (name, location, date, user_id)
VALUES("my_name", "POINT(42,24)", "2017-05-12 12:33:00.0", 1)

However, it doesn't actually work since I get this exception:

Internal Exception: org.postgresql.util.PSQLException: 
ERROR: column "location" is of type point but expression is of type character varying

How can I get the location attribute to insert correctly?

Ecliptica
  • 760
  • 2
  • 8
  • 21
  • suppose you need to map `org.postgresql.geometric.PGpoint` to Hibernate Type. This [example](http://stackoverflow.com/questions/25514859/how-to-map-org-postgresql-geometric-pgpoint-to-hibernate-type) might help you to find a work around. Also this [post](http://stackoverflow.com/questions/42797149/jpa-cant-convert-postgresql-point-type) provide a alternative solution. – Rajith Pemabandu May 12 '17 at 23:36
  • 1
    Seems your JPA provider doesn't handle that geospatial type out of the box, so you need to make use of EclipseLink's type mapping capabilities (whatever they are) or use a JPA 2.1 AttributeConverter. The comment by @RajithPemabandu seems to think you're using Hibernate, but you aren't. – Neil Stockton May 13 '17 at 06:31
  • 1
    see http://database.cloudypoint.com/forums/topic/postgresql-solved-jpa-cant-convert-postgresql-point-type/ – Chris May 15 '17 at 17:52
  • That post keeps popping up, haha. It's my own question/answer (and also the second link Rajith posted). It didn't actually end up working so I deleted my original answer on the other question but it looks like it got archived on this site – Ecliptica May 15 '17 at 19:26

0 Answers0