MySQL support for spatial databases and the use of Hibernate spatial data persistence operations

Source: Internet
Author: User
Tags generator


1. Spatial data: If you do the development of the map, then the spatial data must not be unfamiliar, that is, map elements namely, points, lines, graphs, they have x, y coordinates of information

2.MySQL for the spatial database itself is support, but the support is not very comprehensive, in fact, the professional space database is not postgis, the reason for using MySQL is because the database in the project has already used it, and the function of the map is not very high, so there is such a scenario: Using MySQL to make a spatial database, for the operation of the MySQL spatial database, see the MySQL User manual in chapter 19th, using SQL statements that use each spatial function

3.Hibernate Spatial is a free, open source, extended framework for Hibernate support for spatial data operations, and the version currently has 1.0,1.1,1.1.1,4.0 four versions, with the same usage,

1.1 Support hibernate3.5 and below,

1.1.1 Support Hibernate3.6;

4.0 Support hibernate4.x

For version support issues, depending on the version of the individual, must correspond, I in use is eating the version does not correspond to the loss, using Hibernate Spatial4.0 and hibernate3.6 use, and finally reported an unsupported exception

Exceptions are as follows:

4. For the later versions of Hibernate spatial, it seems that the release is not independent, but as Hibernate5 will exist together, which is likely to support the spatial data directly in the Hibernate5.

5. Code section: (Using 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;}}

The corresponding Hbm.xml file

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ HIBERNATE-MAPPING-3.0.DTD "><!--mapping file autogenerated by MyEclipse persistence Tools-->< Hibernate-mapping><class name= "Com.tcly.test.TowerPoint" table= "Om_point" ><id name= "line_id" type= " Java.lang.String "><column name=" point_id "length=" +/><generator class= "Assigned" ></generator ></id><property name= "point" type= "Org.hibernatespatial.GeometryUserType" ><column name= "Point_ Sharp "/></property></class>

The corresponding Cfg.xml file

<!--MySQL dialect that supports spatial databases--><property name = "Hibernate.dialect" > Org.hibernatespatial.mysql.mysqlspatialdialect</property>

The code to test:

Hibernate Tool Class

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, you can save to the table in the database, if you have friends to see and in the process of writing any questions, you can leave a message, I will reply



Related Article

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.