使用Hibernate Spatial只需将配置文件中Hibernate方言包名称替换为对应版本的Hibernate Spatial方言包名称即可。
Hibernate Spatial 1 替换为:
org.hibernatespatial.postgis.PostgisESDialect
Hibernate Spatial 1 以上替换为:
org.hibernate.spatial.dialect.postgis.PostgisESDialect
以Hibernate4为例:
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
Hibernate映射文件(event.hbm.xml)示例
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;
}
}
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;
}
}
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();
}
}