Hibernate Spatial处理空间数据

hibernate.cfg.xml

<?xml version=’1.0′ encoding=’UTF-8′?>
<!DOCTYPE hibernate-configuration PUBLIC
          “-//Hibernate/Hibernate Configuration DTD 3.0//EN”
          “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>

<!– Generated by MyEclipse Hibernate Tools.                   –>
<hibernate-configuration>

<session-factory>
    <property name=”dialect”>org.hibernatespatial.postgis.PostgisDialect</property>
    <property name=”connection.url”>
        jdbc:postgresql://localhost:5432/postgis
    </property>
    <property name=”connection.username”>postgres</property>
    <property name=”connection.password”>ubiloc</property>
    <property name=”connection.driver_class”>
        org.postgresql.Driver
    </property>
    <property name=”myeclipse.connection.profile”>postgis</property>

    <property name=”show_sql”>true</property>
    <mapping resource=”State.hbm.xml” />
</session-factory>

</hibernate-configuration>

State.hbm.xml

<?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=”State” table=”state” schema=”public”>
        <id name=”gid” type=”Java.lang.Integer”>
            <column name=”gid” />
            <generator class=”assigned” />
        </id>
        <property name=”id” type=”java.lang.Integer”>
            <column name=”id” />
        </property>
        <property name=”geom” type=”org.hibernatespatial.GeometryUserType”>
            <column name=”geom” />
        </property>
    </class>
</hibernate-mapping>


import com.vividsolutions.jts.geom.Point;
public class State {
   int gid;
   int id;
   Point geom;
public int getGid() {
    return gid;
}
public void setGid(int gid) {
    this.gid = gid;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public Point getGeom() {
    return geom;
}
public void setGeom(Point geom) {
    this.geom = geom;
}
   
}

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test {

    /**
     * @param args
     * 注意postgis.jar包导入
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         Configuration config=new Configuration().configure();
            //从配置中取出SessionFactory
            SessionFactory factory=config.buildSessionFactory();
            //从SessionFactory中取出一个Session
            Session session=factory.openSession();

            Query query=session.createQuery(“from State as s where s.id=:pid”);
            //设置参数
            query.setInteger(“pid”, 4);
              List<State> result= query.list();
              
              for(State p:result){
                  System.out.println(p.getId());
                  System.out.println(p.getGeom());
               System.out.println(“x:”+p.getGeom().getX());
               System.out.println(“y:”+p.getGeom().getY());
               System.out.println(p.getGeom().getSRID());
              }
              
             
            
            session.close();
    }

}

转载自:https://blog.csdn.net/cpp1781089410/article/details/51217954

You may also like...