I have a GeoPoint class which contains two properties latitude and longitude. To store it with JPA I have created a GeoPointType which is a custom user type. This all works fine – I can both read and write GPS data from the MySQL database this way.
However, when trying to write a query for updating a GeoPoint, I have run into trouble. As an example, I have an entity Place which has a property called location which is a GeoPoint, so to update the location of a place I am trying to use this query:
Query updateLocationQuery = entityManager.createQuery( "UPDATE Place p SET p.location = :location WHERE p.id = :placeId" ) .setParameter("location", myGeoPoint) .setParameter("placeId", placeId);
In the database the Place table has two columns latitude and longitude, and I was hoping that Hibernate would figure out how to get the latitude and longitude from the GeoPoint parameter and execute an update of these columns. Sadly, what I am seeing is this:
org.hibernate.exception.SQLGrammarException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' longitude=(55.723758697509766, 9.5314302444458) where id=3282502802948813353171' at line 1 at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:166) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final] ...
So to me it looks as if Hibernate thinks the GeoPointType only uses a single database column for storing both the latitude and longitude, which is of course not correct. My custom type specifies that it needs two columns like this
@Override public int[] sqlTypes() {return new int[]{DoubleType.INSTANCE.sqlType(), DoubleType.INSTANCE.sqlType()};}
So my question is: Why does Hibernate fail to realize that the custom type uses two columns and try to push both the lat and lon into the same column? Note that, on a regular persist of a new entity containing a GeoPoint everything works fine, and Hibernate does the right thing, so why does it not work for an update?
Thanks
Randahl
|