• 金仓数据库KingbaseES客户端编程开发框架-Hibernate Spatial(3. Hibernate-Spatial 配置)


    3. Hibernate-Spatial 配置

    3.1. 配置说明

    使用Hibernate Spatial只需将配置文件中Hibernate方言包名称替换为对应版本的Hibernate Spatial方言包名称即可。

    1. Hibernate Spatial 1 替换为:

      org.hibernatespatial.postgis.PostgisESDialect
      
    2. Hibernate Spatial 1 以上替换为:

      org.hibernate.spatial.dialect.postgis.PostgisESDialect
      

    3.2. 举例说明

    以Hibernate4为例:

    1. Hibernate配置文件(hibernate.cfg.xml)示例

      
      
      
      
          
              
              org.postgresql.Driver
              jdbc:postgresql://localhost:54321/test
              system
              123456
      
              
              1
      
              
              
                  org.hibernate.spatial.dialect.postgis.PostgisESDialect
              
      
              
              thread
      
              
              
                  org.hibernate.cache.NoCacheProvider
              
      
              
              true
      
              
              create
      
              
          
      
      
    2. Hibernate映射文件(event.hbm.xml)示例

      
      
      
      
          
      
              
                  
              
      
              
      
              
      
              
           
      
      
    3. java对象(event)示例

      import java.util.Date;
      
      import com.vividsolutions.jts.geom.Point;
      
      public class Event {
          private Long id;
          private String title;
          private Date date;
          private Point location;
      
          public Event() {
          }
      
          public Long getId() {
              return id;
          }
      
          public void setId(Long id) {
              this.id = id;
          }
      
          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;
          }
      
          public Point getLocation() {
              return this.location;
          }
      
          public void setLocation(Point location) {
              this.location = location;
          }
      }
      
    4. Hibernate获取SessionFactory

      package org.hibernatespatial.util;
      
      import org.hibernate.*;
      import org.hibernate.cfg.*;
      
      public class HibernateUtil {
      
          private static final SessionFactory sessionFactory;
      
          static {
              try {
                  /* 从hibernate.cfg.xml文件中创建SessionFactory */
                  sessionFactory = new Configuration().configure().buildSessionFactory();
              } catch (Throwable ex) {
                  System.err.println("Initial SessionFactory creation failed." + ex);
                  throw new ExceptionInInitializerError(ex);
              }
          }
      
          public static SessionFactory getSessionFactory() {
              return sessionFactory;
          }
      
      }
      
    5. Hibernate的java使用例子

      package org.hibernatespatial.test;
      
      import org.hibernate.Session;
      import org.hibernatespatial.entity.Event;
      import org.hibernatespatial.util.HibernateUtil;
      
      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;
      
      public class EventManager {
      
          public static void main(String[] args) {
                          EventManager mgr = new EventManager();
      
                          if (args[0].equals("store")) {
                              mgr.createAndStoreEvent("My Event", new Date(), assemble(args));
                          }
      
                          HibernateUtil.getSessionFactory().close();
          }
      
          private void createAndStoreEvent(String title, Date theDate, String wktPoint) {
      
              /* 创建一个WKTReader对象 */
              WKTReader fromText = new WKTReader();
              Geometry geom = null;
              try {
                  geom = fromText.read(wktPoint);
              } catch (ParseException e) {
                  throw new RuntimeException("Not a WKT string:" + wktPoint);
              }
              if (!geom.getGeometryType().equals("Point")) {
                  throw new RuntimeException("Geometry must be a point. Got a " + geom.getGeometryType());
              }
      
              Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      
              session.beginTransaction();
      
              Event theEvent = new Event();
              theEvent.setTitle(title);
              theEvent.setDate(theDate);
              theEvent.setLocation((Point) geom);
              session.save(theEvent);
      
              session.getTransaction().commit();
          }
      
          /**
          * 将数组按顺序拼接成以空格符分割的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();
          }
      
      }
  • 相关阅读:
    突破众多技术干货的迷茫
    rust中的reborrow和NLL
    金鸣识别网页版:轻松实现表格识别的神器
    IEEE投稿模板下载
    接口幂等性最佳实践--redis+注解
    如何应对软件可变性?这4种常用的方法肯定要知道
    java计算机毕业设计物业后台管理系统源程序+mysql+系统+lw文档+远程调试
    将函数实现放到CPP报“无法解析的外部符号...”,系VS Bug
    某旗小说107逆向(补发)
    揭秘计算机的神经系统:探索计算机的基本组成
  • 原文地址:https://blog.csdn.net/arthemis_14/article/details/126888302