For the below code, it works when I store, but when it comes to retrieving I keep getting
32350 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: XX000
32350 [main] ERROR org.hibernate.util.JDBCExceptionReporter - ERROR: Invalid endian flag value encountered.
Any help would be appreciated
package event;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import com.vividsolutions.jts.util.GeometricShapeFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import util.HibernateUtil;
import javax.persistence.EntityManager;
import java.util.Date;
import java.util.List;
public class EventManager {
public static void main(String[] args) {
EventManager mgr = new EventManager();
if (args[0].equals("store")) {
mgr.createAndStoreEvent("My Event", new Date(), assemble(args));
}else{
List events = mgr.find(args[1]);
for (int i = 0; i < events.size(); i++) {
Event event = (Event) events.get(i);
System.out.println("Event: " + event.getTitle() +
", Time: " + event.getDate() +
", Location: " + event.getLocation());
}
}
HibernateUtil.getSessionFactory().close();
}
private List find(String wktFilter) {
Geometry filter = wktToGeometry(wktFilter);
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery("select e from Event e where within(e.location, :filter) = true");
query.setParameter("filter", filter);
return query.list();
}
private List find(Double longitude, Double latitude, Double radius) {
Geometry filter = createCircle(longitude, latitude, radius);
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery("select e from Event e where within(e.location, :filter) = true");
query.setParameter("filter", filter);
return query.list();
}
//this method replaces the above wktToGeometry() method
private static Geometry createCircle(double x, double y, final double RADIUS) {
GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
shapeFactory.setNumPoints(32);
shapeFactory.setCentre(new Coordinate(x, y));//there are your coordinates
shapeFactory.setSize(RADIUS * 2);//this is how you set the radius
return shapeFactory.createCircle();
}
private Geometry wktToGeometry(String wktPoint) {
WKTReader fromText = new WKTReader();
Geometry geom = null;
try {
geom = fromText.read(wktPoint);
} catch (ParseException e) {
throw new RuntimeException("Not a WKT string:" + wktPoint);
}
return geom;
}
/**
* Utility method to assemble all arguments save the first into a String
*/
private static String assemble(String[] args) {
StringBuilder builder = new StringBuilder();
for (int i = 1; i < args.length; i++) {
builder.append(args[i]).append(" ");
}
return builder.toString();
}
private void createAndStoreEvent(String title, Date theDate, String wktPoint) {
//First interpret the WKT string to a point
WKTReader fromText = new WKTReader();
Geometry geom = null;
try {
geom = fromText.read(wktPoint);
} 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());
}
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
theEvent.setLocation((Point) geom);
session.save(theEvent);
session.getTransaction().commit();
}
}