解决:hibernate 实现空间数据库oracle spatial

  本来报错“无效列类型”,但是今天在“大强”“linliangyi2007”的要求下公

 

布代码以供找错,于是自己重新整理了一个test,发现可以用了,呵呵,奇怪了

 

呀,可能是我原来程序那里有点不对,我再找找去。在此特别感谢两位“大强”

 

“linliangyi2007”!

 

实体类: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值