关于hibernate支持mysql-spatial

其实在诸多数据库中对于空间分析的支持 mysql 是最不好的,但是谁让我们公司用的是 mysql 呢。看看官网怎么说来着: Some functions that work for PostGIS or Oracle may fail to work for MySQL.。哎真是的,看的我头疼。看了看官网,写了一个小例子,首先先看一下项目结构:

                                           关于hibernate支持mysql-spatial_第1张图片

其中有三个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;
	}
	
	

}

EventManager.java

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 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

Event.hbm.xml




	

        
			
		

        

        

         
	

HibernateUtil.java

 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;
        }

    }

hibernate.cfg.xml






    

        
        com.mysql.jdbc.Driver
        jdbc:mysql://localhost:3306/events
        root
        B10090120

        
        1

        
        org.hibernatespatial.mysql.MySQLSpatialDialect

        
        thread

        
        org.hibernate.cache.NoCacheProvider

        
        true

        
        create

        

    

		

参考文档:http://www.hibernatespatial.org/tutorial.html

你可能感兴趣的:(SSH)