0

I have a problem with JPA querying a MySQL table that has a column of type geometry. It contains polygons having sets of latitude and longitude as the coordinates. While executing the nativequery to select from the table, I am getting the following error

Exception Description: The primary key read from the row [ArrayRecord(
 => POLYGON((102.642944444444 2.9757087270706,102.642944444444 2.79805447470818,....
 => 16.0
 => 325990)] during the execution of the query was detected to be null.  Primary keys must not contain null.

However the table has no row with primary key as null. This specific row has a very large polygon with 66 coordinates. Not sure if the problem is because of this.

Following are the table column names and types

geomarea  - geometry
riskvalue - double
id        - int (Autoincrement, Primary Key)

Following is the code in my EJB to read the table.

Query query = em.createNativeQuery("select astext(geomarea) geomarea,riskvalue,id from earthquakeRisk where Contains(geomarea,GeomFromText('POINT(" + node.getLongitude() + " "+node.getLatitude()+")'))",Earthquakerisk.class);
geomList.addAll(query.getResultList());

And here is how the fields are declared in the entity class

public class Earthquakerisk implements Serializable {
    private static final long serialVersionUID = 1L;
    @Basic(optional = false)
    @NotNull
    @Lob
    @Column(name = "geomArea")
    private byte[] geomArea;
    @Column(name = "riskvalue")
    private Double riskvalue;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

Any idea how to solve this?

1
  • You do not need specify that your ID field will be not null (using @Basic), because @Id annotation will do it for you and it tells that this field is an identity of your instance and this identifier should never be null, should always be permanent and unique.
    – solvator
    Feb 19, 2014 at 16:15

1 Answer 1

1

I found a solution to the problem. Adding it here in case someone finds it useful.

Solved it by removing the Earthquakerisk.class from the query, and changing my List to List. So the working code is now as follows.

List<Object[]> geomList = new ArrayList<>();
Query query = em.createNativeQuery("select astext(geomarea) geomarea,riskvalue,id from earthquakeRisk where Contains(geomarea,GeomFromText('POINT(" + node.getLongitude() + " "+node.getLatitude()+")'))");
geomList.addAll(query.getResultList());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.