Mysql's support for spatial databases and the use of HibernateSpatial for spatial data holding _ MySQL

Source: Internet
Author: User
Mysql supports spatial databases and uses HibernateSpatial for spatial data persistence. Hibernate

1. spatial data: if you develop maps, you will certainly be familiar with spatial data, that is, map elements, points, lines, and graphics, which have x and y coordinates.

2. mySQL supports spatial databases, but does not fully support them. in fact, professional spatial databases do not belong to postgis. the reason why Mysql is used is because the database in the project already uses it, in addition, the map function is not very high, so this should be the case: using MySQL for space databases, for MySQL space database operations, see MySQL User Manual Chapter 19th for detailed instructions on the use of SQL statements for spatial functions

3. Hibernate Spatial is a free and open-source extension framework that supports Spatial data operations in hibernate. It has four versions: 1.0, 1.1, 1.1.1, and 4.0. the usage is basically the same,

1.1 Support for hibernate3.5 and below,

1.1.1 Support Hibernate3.6;

4.0 support for hibernate4.x

For version support problems, please decide based on your individual version. it must be matched. I am using a loss that does not match the version. I use Hibernate Spatial4.0 and hibernate3.6, finally, an unsupported exception is reported.

The exception is as follows:

4. for versions later than Hibernate Spatial, it seems that it will not be released independently, but will exist together with Hibernate5, that is, it may directly support space data in hibernate5.

5. Code Section: (use hibernate3.6.0 + mysql5.5 + hibernate spatial1.1.1)

Entity class

public class TowerPoint{private String line_id;private Point point;public String getLine_id(){return line_id;}public void setLine_id(String line_id){this.line_id = line_id;}public Point getPoint(){return point;}public void setPoint(Point point){this.point = point;}}
Corresponding hbm. xml file
 
 
 
  
   
    
    
   
   
    
   
  
 
Corresponding cfg. xml file
 
 
  
Org. hibernatespatial. mysql. MySQLSpatialDialect
 
Code for testing:

Hibernate tool

import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class HibernateConnUtil{private static SessionFactory sessionFactory;static{try{Configuration config = new Configuration().configure();sessionFactory = config.buildSessionFactory();} catch (Exception ex){ex.printStackTrace();}}public static Session openSession(){Session session = sessionFactory.openSession();return session;}public static void closeSession(Session session){if (null != session){session.clear();}}public static void closeSessionFactory(SessionFactory sessionFactory){if (null != sessionFactory){sessionFactory.close();}}public static void commitTransaction(Transaction transaction){if (null != transaction){transaction.commit();}}}
Test Code
import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.junit.After;import org.junit.Before;import org.junit.Test;import com.tcly.eap.core.utils.HibernateConnUtil;import com.vividsolutions.jts.geom.Geometry;import com.vividsolutions.jts.geom.Point;import com.vividsolutions.jts.io.ParseException;import com.vividsolutions.jts.io.WKTReader;public class HibernateCURDTest{public static SessionFactory sessionFactory;public static Session session;public static Transaction transaction;@Beforepublic void init(){session = HibernateConnUtil.openSession();transaction = session.beginTransaction();}@Afterpublic void destory(){HibernateConnUtil.commitTransaction(transaction);HibernateConnUtil.closeSession(session);HibernateConnUtil.closeSessionFactory(sessionFactory);}@Testpublic void testHibernateMapping() throws ParseException{TowerPoint towerPoint = new TowerPoint();WKTReader formText = new WKTReader();Geometry geom = null;geom = formText.read("POINT(12.32 23.34)");towerPoint.setLine_id("test001");towerPoint.setPoint((Point) geom);session.save(towerPoint);}}
After testing, it can be saved to the table in the database. if a friend sees any problems during the writing process, he can leave a message and I will reply to it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.