Java Hibernate Spatial:Type org.hibernatespatial.GeometryUserType
我使用的是HibernateSpatial版本4.0-M1。我正在学习教程。但是,我的代码失败,出现以下错误:Java Hibernate Spatial:Type org.hibernatespatial.GeometryUserType,java,hibernate,spatial,Java,Hibernate,Spatial,我使用的是HibernateSpatial版本4.0-M1。我正在学习教程。但是,我的代码失败,出现以下错误:org.hibernate.MappingException:无法确定org.hibernatespatial.GeometryUserType的类型,位于#table#u name#处的列#geometry# 我的会话工厂创建类如下所示: public class HibernateUtil { private static final SessionFactory sessi
org.hibernate.MappingException:无法确定org.hibernatespatial.GeometryUserType的类型,位于#table#u name#处的列#geometry#
我的会话工厂创建类如下所示:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Exception ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
<session-factory>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/dbName</property>
<property name="connection.username">dbUsername</property>
<property name="connection.password">dbPassword</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernatespatial.postgis.PostgisDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<mapping class="com.testapp.model.EntityClassWithAnnotations" />
</session-factory>
@Entity
@Table(name = "table_name")
public class MyEntityClass implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "gid")
private Long gid;
@Column(name = "adm1_name")
private String adminName;
@Column(name = "adm1_code")
private String adminCode;
@Column(name = "pmal")
private Double pmale;
@Column(name = "pfem")
private Double pfemale;
@Type(type = "org.hibernatespatial.GeometryUserType")
@Column(name = "the_geom", nullable = true)
private Geometry geom;
public MyEntityClass() {}
public Long getGid() {
return gid;
}
public void setGid(Long gid) {
this.gid = gid;
}
public String getAdminName() {
return adminName;
}
public void setAdmin_name(String adminName) {
this.adminName = adminName;
}
public String getAdminCode() {
return adminCode;
}
public void setAdmin_code(String adminCode) {
this.adminCode = adminCode;
}
public Double getPmale() {
return pmale;
}
public void setPmale(Double pmale) {
this.pmale = pmale;
}
public Double getPfemale() {
return pfemale;
}
public void setPfemale(Double pfemale) {
this.pfemale = pfemale;
}
public Geometry getGeom() {
return geom;
}
public void setGeom(Geometry geom) {
this.geom = geom;
}
}
在寻找可能的原因后,我发现这与我的配置有关。我的hibernate.cfg.xml
如下所示:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Exception ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
<session-factory>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/dbName</property>
<property name="connection.username">dbUsername</property>
<property name="connection.password">dbPassword</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernatespatial.postgis.PostgisDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<mapping class="com.testapp.model.EntityClassWithAnnotations" />
</session-factory>
@Entity
@Table(name = "table_name")
public class MyEntityClass implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "gid")
private Long gid;
@Column(name = "adm1_name")
private String adminName;
@Column(name = "adm1_code")
private String adminCode;
@Column(name = "pmal")
private Double pmale;
@Column(name = "pfem")
private Double pfemale;
@Type(type = "org.hibernatespatial.GeometryUserType")
@Column(name = "the_geom", nullable = true)
private Geometry geom;
public MyEntityClass() {}
public Long getGid() {
return gid;
}
public void setGid(Long gid) {
this.gid = gid;
}
public String getAdminName() {
return adminName;
}
public void setAdmin_name(String adminName) {
this.adminName = adminName;
}
public String getAdminCode() {
return adminCode;
}
public void setAdmin_code(String adminCode) {
this.adminCode = adminCode;
}
public Double getPmale() {
return pmale;
}
public void setPmale(Double pmale) {
this.pmale = pmale;
}
public Double getPfemale() {
return pfemale;
}
public void setPfemale(Double pfemale) {
this.pfemale = pfemale;
}
public Geometry getGeom() {
return geom;
}
public void setGeom(Geometry geom) {
this.geom = geom;
}
}
已编辑:我想我在hibernate.cfg文件中看到了错误,您使用了错误的方言 将方言行替换为:
<property name="hibernate.dialect" value="org.hibernate.spatial.dialect.postgis.PostgisDialect"/>
在我的例子中,
hibernate spatial.jar
丢失,其中包括org.hibernatespatial.GeometryUserType
我们正在使用maven和hibernate spatial的旧1.1.1版本,因此在我将以下内容添加到pom之后,一切都很好:
org.hibernatespatial
冬眠空间
1.1.1
我的映射如上图所示。我正在使用注释。我正在更新代码以包含实体类。至于数据库,我正在与其他应用程序一起使用,主要是openlayers。所以,我认为问题不在于我的数据库。谢谢@Eduardo。改变方言似乎没有效果。它可能是其他东西,而不是方言。好吧,你的地图是好的,你的方言是好的,你的数据库是好的,所以它一定是查询?我不这么认为,我会根据教程再次回顾Hibernate/JPA配置