JPA can't convert PostgreSQL POINT type

2019-09-15 11:04发布

问题:

I am in the process converting an existing Spring project to JPA/Jinq. One of my database columns is using the PostGIS geometry POINT type. I am mapping the table for that column to an entity called City. The class for that something like this:

import java.io.Serializable
import javax.persistence.*;
import org.postgresql.geometric.PGpoint;

@Entity
@NamedQuery(name="City.findAll", query="SELECT c FROM City c")
public class City implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private Integer id;

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

    /* constructor, getters, and setters */
}

I generated this class using EclipseLink's reverse engineering feature, but the location field was originally of type Object which is not serializable, so I have changed it to PGpoint.

Unfortunately, when I try to use this class, I get the following error:

SEVERE: Servlet.service() for servlet [appServlet] in context with path 
[/NatureLynxV03] threw exception [Request processing failed; nested exception 
is javax.persistence.PersistenceException: Exception [EclipseLink-3002] 
(Eclipse Persistence Services - 2.6.4.v20160829-44060b6): 
org.eclipse.persistence.exceptions.ConversionException
Exception Description: The object [(-123.456,54.321)], of class 
[class org.postgresql.geometric.PGpoint], from mapping 
[org.eclipse.persistence.mappings.DirectToFieldMapping[location-->CITY.location]] 
with descriptor [RelationalDescriptor(org.abmi.naturelynx.model.City --> 
[DatabaseTable(CITY)])], could not be converted to [class [B].] with root cause
Local Exception Stack: 
Exception [EclipseLink-3002] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): 
org.eclipse.persistence.exceptions.ConversionException
Exception Description: The object [(-123.456,54.321)], of class 
[class org.postgresql.geometric.PGpoint], from mapping 
[org.eclipse.persistence.mappings.DirectToFieldMapping[location-->CITY.location]] 
with descriptor [RelationalDescriptor(org.abmi.naturelynx.model.City --> 
[DatabaseTable(CITY)])], could not be converted to [class [B]. at ...

What am I doing wrong?