其中有三个jar包比较重要分别为:hibernate-spatial-1.1.jar、hibernate-spatial-mysql-1.1.1.jar、
jts-1.11.jar,以我的理解:JTS( java Topology Suite)字面上理解就是空间拓扑的意思。hibernate-spatial则是pojo的映射,hibernate-spatial-mysql-1.1.1.jar这是对于mysql专门的空间支持。
Event.java
package event; import java.util.Date; import com.vividsolutions.jts.geom.Point; public class Event { private Long id; private Point point; private Date date; private String title; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Point getPoint() { return point; } public void setPoint(Point point) { this.point = point; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
package event; import org.hibernate.HibernateException; import org.hibernate.Session; import java.util.Date; import com.vividsolutions.jts.geom.Point; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.io.WKTReader; import com.vividsolutions.jts.io.ParseException; import org.hibernate.Criteria; import org.hibernate.criterion.Restrictions; import java.util.List; import org.hibernatespatial.criterion.SpatialRestrictions; import util.HibernateUtil; public class EventManager { public static void main(String[] args) throws ParseException { EventManager mgr = new EventManager(); /* if (args[0].equals("store")) { mgr.createAndStoreEvent("My Event", new Date(), assemble(args)); } Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); session.getTransaction().commit();*/ for(int i=0;i<10;i++){ Event ev = new Event(); ev.setDate(new java.sql.Date((new Date()).getTime())); WKTReader fromText = new WKTReader(); Geometry geom = null; geom = fromText.read("POINT(12.32 223)"); ev.setPoint((Point)geom); ev.setTitle("name"); mgr.Save(ev); } List<Event> points = mgr.find("POLYGON((0 0,200 0,200 400,0 400,0 0))"); for(Event pt:points){ System.out.println("size---"+points.size()+"-----"+pt.getPoint().getX()); } } public void Save(Event ev) { try { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); //获取hibernate的session session.beginTransaction(); session.save(ev); //这里只需要调用save方法把news对象传进去就插入成功了! session.getTransaction().commit(); } catch (HibernateException e) { e.printStackTrace(); } } /** * 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 List find(String wktFilter){ WKTReader fromText = new WKTReader(); Geometry filter = null; try{ filter = fromText.read(wktFilter); } catch(ParseException e){ throw new RuntimeException("Not a WKT String:" + wktFilter); } Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); System.out.println("Filter is : " + filter); Criteria testCriteria = session.createCriteria(Event.class); testCriteria.add(SpatialRestrictions.within("point", filter)); List results = testCriteria.list(); session.getTransaction().commit(); return results; } }
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="event"> <class name="Event" table="EVENTS"> <id name="id" column="EVENT_ID"> <generator class="native"/> </id> <property name="date" type="timestamp" column="EVENT_DATE"/> <property name="title" type="string"/> <property name="point" type="org.hibernatespatial.GeometryUserType" column="LOC"/> </class> </hibernate-mapping>
package util; import org.hibernate.*; import org.hibernate.cfg.*; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { // Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/events</property> <property name="connection.username">root</property> <property name="connection.password">B10090120</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SPATIAL SQL dialect --> <property name="dialect">org.hibernatespatial.mysql.MySQLSpatialDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <mapping resource="event/Event.hbm.xml"/> </session-factory> </hibernate-configuration>