设为首页 收藏本站
查看: 567|回复: 0

[经验分享] 解决:hibernate 实现空间数据库oracle spatial

[复制链接]

尚未签到

发表于 2016-8-6 10:52:42 | 显示全部楼层 |阅读模式
    本来报错“无效列类型”,但是今天在“大强”“linliangyi2007”的要求下公
  
  布代码以供找错,于是自己重新整理了一个test,发现可以用了,呵呵,奇怪了
  
  呀,可能是我原来程序那里有点不对,我再找找去。在此特别感谢两位“大强”
   
  “linliangyi2007”! DSC0000.gif DSC0001.gif
  
  实体类:User:
   

package com.test.vo;
import com.navsys.spatial.JGeometryType;
public class User {
private Integer id;
private  String name;
private JGeometryType location;
/**
*
*/
public User() {
super();
}
/**
* @param name
* @param location
*/
public User(String name, JGeometryType location) {
super();
this.name = name;
this.location = location;
}
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the location
*/
public JGeometryType getLocation() {
return location;
}
/**
* @param location the location to set
*/
public void setLocation(JGeometryType location) {
this.location = location;
}

}

   映射文件:
  

<?xml version="1.0"?>
<!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 package="com.test.vo">
<class name="User" table = "tb_user" schema="CMSDB">
<id name="id" type="integer">
<column name="User_ID" precision="22" scale="0" />
<generator class="native">
<param name="sequence">WQ_SEQUENCE</param>
</generator>
</id>
<property name="name" not-null="true"/>
<property name = "location" type="com.navsys.spatial.JGeometryType"/>
</class>
</hibernate-mapping>
  
  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">
<hibernate-configuration>
<session-factory>
<!-- dialect for Oracle 10G Spatial -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url"> jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">CMSDB</property>
<property name="connection.password">CMSDB</property>
<!--  dialect for Oracle 10G Spatial  -->
<property name="hibernate.dialect">com.navsys.spatial.OracleSpatialDialect</property>
<!--加载hibernate自动更新数据库结构-->
<property name="hibernate.hbm2ddl.auto" >update</property>
<!-- 让hibernate自动管理会话环境 -->
<property name="current_session_context_class">thread</property>     
<!--配置的JDBC连接池-->
<property name="connection.pool_size">5</property>
<!--显示查询语句-->
<property name="hibernate.show_sql">true</property>
<!--格式化SQL-->
<property name="hibernate.format_sql">true</property>
<!--配置二级缓存-->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!--使用二级缓存第三方提供商-->
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider</property>
<mapping resource="com/test/vo/User.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
  
  导出数据库:
  

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDB {

public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
   运行ExportDB.JAVA:正常创建数据库:
  

   drop table CMSDB.tb_user cascade constraints
drop sequence WQ_SEQUENCE
create table CMSDB.tb_user (
User_ID number(10,0) not null,
name varchar2(255 char) not null,
location sdo_geometry,
primary key (User_ID)
)
create sequence WQ_SEQUENCE

  
  最好测试类:
  

package test.upload;
import oracle.spatial.geometry.JGeometry;
import org.hibernate.Session;
import com.navsys.spatial.JGeometryType;
import com.test.vo.User;
import junit.framework.TestCase;
public class Upload extends TestCase {
public void testSave() {
JGeometry geometry = null;
Session session = null;
String s = "测试2";
try {
session = HSFUtility.getSession();
session.beginTransaction();
User u = new User();
u.setName(s);
double[] coords = { 113.2, 23 };
geometry = JGeometry.createPoint(coords, 2, 1);
JGeometryType point = new JGeometryType(geometry);
u.setLocation(point);
session.save(u);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
throw new java.lang.RuntimeException();
} finally {
HSFUtility.closeSession();
}
}
}

  
  昨晚试了报错如下:  利用了HIbernate Oracle 里的JGeomtry。做映射,可以建数据库,但无法上传数据,报以下错误,谁好心帮我看看啥问题,说是“无效的列类型”: 
  21:19:55,890 WARN JDBCExceptionReporter:71 - SQL Error: 17004, SQLState: null 21:19:55,906 ERROR JDBCExceptionReporter:72 - 无效的列类型 21:19:55,906 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session org.hibernate.exception.GenericJDBCException: could not insert: [cmsdb.database.Entity.WQ_Vessel_VO] at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2202) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2595) at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) at com.database.test.TestUpload.testSave(TestUpload.java:59) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at junit.framework.TestCase.runTest(TestCase.java:154) at junit.framework.TestCase.runBare(TestCase.java:127) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) Caused by: java.sql.SQLException: 无效的列类型 at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208) at oracle.jdbc.driver.OracleStatement.getInternalType(OracleStatement.java:3433) at oracle.jdbc.driver.OraclePreparedStatement.setNullCritical(OraclePreparedStatement.java:4197) at oracle.jdbc.driver.OraclePreparedStatement.setNull(OraclePreparedStatement.java:4186) at com.navsys.spatial.JGeometryType.nullSafeSet(JGeometryType.java:141) at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:146) at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:1932) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2178) ... 27 more org.hibernate.exception.GenericJDBCException: could not insert: [cmsdb.database.Entity.WQ_Vessel_VO] at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2202) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2595) at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) at com.database.test.TestUpload.testSave(TestUpload.java:59) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at junit.framework.TestCase.runTest(TestCase.java:154) at junit.framework.TestCase.runBare(TestCase.java:127) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) Caused by: java.sql.SQLException: 无效的列类型 at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208) at oracle.jdbc.driver.OracleStatement.getInternalType(OracleStatement.java:3433) at oracle.jdbc.driver.OraclePreparedStatement.setNullCritical(OraclePreparedStatement.java:4197) at oracle.jdbc.driver.OraclePreparedStatement.setNull(OraclePreparedStatement.java:4186) at com.navsys.spatial.JGeometryType.nullSafeSet(JGeometryType.java:141) at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:146) at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:1932) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2178) ... 27 more

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-253623-1-1.html 上篇帖子: Oracle数据导入导出的N种实现。 下篇帖子: Oracle创建用户、表空间、导入导出命令 (转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表