我正在使用 Hibernate Spatial 版本 4.0-M1。我在这里学习教程。但是,我的代码失败并出现以下错误:org.hibernate.MappingException: Could not determine type for: org.hibernatespatial.GeometryUserType, at #table_name# for column #geometry_column#
.
我的会话工厂创建类如下图:
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;
}
}
在寻找可能的原因后,我发现它与我的配置有关。我hibernate.cfg.xml
的如下图:
<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;
}
}