如何将oracle时间戳映射到hibernate中适当的java类型? [英] How to map oracle timestamp to appropriate java type in hibernate?

查看:155
本文介绍了如何将oracle时间戳映射到hibernate中适当的java类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是hibernate的新手,我很难过。在我的数据库中,我有一些具有 TIMESTAMP(6)列的表。我使用的是Netbeans 6.5.1,当我生成 hibernate.reveng.xml hbm.xml文件时,以及 pojo文件它将列设置为 Serializable 类型。这不是我所期望的,也不是我希望他们成为的。



我发现

a>在hibernate论坛上发帖说:

 < sql-type jdbc-type =OTHERhibernate-type =java.sql.Timestamp/> 

hibernate.reveng.xml 文件中。

在Netbeans中,您无法从该文件生成映射(每次都创建一个映射),并且似乎没有能力重新创建映射从文件生成它们(至少根据这个,它将在版本7中提供) 。

所以我想弄清楚该怎么做。我更倾向于相信自己做错了事,因为我是新手,看起来这对其他人来说会是一个普遍问题。


  • 那么我做错了什么?

  • 如果我没有做错什么,我该如何解决这个问题?



我使用的是Netbeans 6.5,Oracle 10G,我相信Hibernate 3(它与我的NetBeans一起提供)。

编辑:想说我发现这个 stackoverflow的问题,但它确实是一个不同的问题。



更新:
我的oracle jdbc驱动程序使用(ojdbc14.jar)是9.0.2.0.0
我现在也尝试过了:


  • ojdbc14.jar版本10.2。 0.4.0
  • ojdbc6.jar版本11.2.0.1.0

解决方案

我找到了解决这个问题的方法。这个问题本身似乎围绕着这样一个事实:Netbeans 6.5(以及我后来的版本)不允许你从现有的 hibernate.reveng.xml 文件。这将在第7版中提供。



我发现的解决方法是创建一个ant任务来重新创建 hbm.xml

code>和pojo java文件。当我执行一个清理和构建时,我目前有这种情况发生,但我会尝试找到一种方法将它完全分开,因为它只需在数据库模式更改时运行。



要完成此操作,需要编辑 build.xml 文件,然后执行清理和构建。



第一部分是您需要的库。因此,请添加:

 <路径id =toolslib> 
< path location =lib / hibernate-support / jtidy-r938.jar/>
< / path>

您应该已经安装了hibernate-tools.jar,hibernate3.jar和ojdbc14.jar文件你机器。所以只要改变他们的路径。 freemaker.jar jtidy-r938.jar 需要下载,因为我没有这些。



build.xml 您需要添加:

 < taskdef name = hibernatetool
classname =org.hibernate.tool.ant.HibernateToolTask​​
classpathref =toolslib>
< classpath>
< fileset dir =lib>
< include name =** / *。jar/>
< / fileset>
< / classpath>
< / taskdef>

您需要的最后一部分是要在后整理部分运行的集合:

 < target name = -  post-clean> 
< delete dir =src / *将foler放在pojos和hbm.xml文件所在的位置*/>
< hibernatetool>
configurationfile =src\hibernate.cfg.xml
packagename =*您希望重新创建它们的包*
revengfile =src\ hibernate.reveng.xml
detectmanytomany =true
/>
< hbm2hbmxml destdir =src/>
< hbm2java destdir =src/>
< / hibernatetool>
< / target>




  • 删除部分会删除现有的hbm和pojo文件被重新创建。
  • 配置文件指向您的主配置文件。

  • 包名称是您希望在( com.stackoverflow.pojo 例如)中创建的点分隔包。

  • revengfile 是创建hbm和pojo文件时使用的反向工程xml文件。
  • hbm2hbmxml 会创建表格的 hbm.xml 文件。

  • hbm2java 会创建表格的Java POJO文件。



现在为了让Oracle时间戳不是 Serializable ,编辑 hibernate.reveng.xml code> file并添加:

 < type-mapping> 
< sql-type jdbc-type =OTHERhibernate-type =java.sql.Timestamp/>
< / type-mapping>

紧跟在模式选择标记之后。



<因此,一个干净的构建和时间戳不会是 java.sql.Timestamp 而不是 Serializable 对象。



我知道这是一个很长的答案,但这也应该适用于您必须在 hibernate.reveng.xml中设置的任何其他更改文件(我认为)。我不是冬眠专家,所以你的里程可能会因此而有所不同。

更新:
所以在Google搜索后,我发现这个关于Netbeans中自定义ant任务的网站。所以我只是简单地将目标的名称改为 gen-dao ,现在它不会在我每次进行清理和构建时运行,只是当我专门调用它时。 / p>

I am new to hibernate and I am stumped. In my database I have tables that have a columns of TIMESTAMP(6). I am using Netbeans 6.5.1 and when I generate the hibernate.reveng.xml, hbm.xml files, and pojo files it sets the columns to be of type Serializable. This is not what I expected, nor what I want them to be.

I found this post on the hibernate forums saying to place:

<sql-type jdbc-type="OTHER" hibernate-type="java.sql.Timestamp" />

in the hibernate.reveng.xml file.

In Netbeans you are not able to generate the mappings from this file (it creates a new one every time) and it does not seem to have the ability to re-generate them from the file either (at least according to this it is slated to be available in version 7).

So I am trying to figure out what to do. I am more inclined to believe I am doing something wrong since I am new to this, and it seems like it would be a common problem for others.

  • So what am I doing wrong?
  • If I am not doing anything wrong, how do I work around this?

I am using Netbeans 6.5, Oracle 10G, and I believe Hibernate 3 (it came with my netbeans).

Edit: Meant to say I found this stackoverflow question, but it is really a different problem.

UPDATE: The oracle jdbc driver I was using (ojdbc14.jar) is 9.0.2.0.0 I have now also tried:

  • ojdbc14.jar version 10.2.0.4.0
  • ojdbc6.jar version 11.2.0.1.0

解决方案

I found a work around for this problem. The issue itself seems to revolve around the fact that Netbeans 6.5 (and I later versions up to this point) do not allow you to reverse engineer a database from an existing hibernate.reveng.xml file. This is slated to be available in version 7.

The work around I found is to create an ant task to recreate the hbm.xml and pojo java files. I currently have this hooked to happen when I do a clean and build, but I am going to try to find a way to to have it completely separate, since it will only need to be ran when the database schema changes.

To accomplish this when you do a clean and build though you need to edit your build.xml file.

The first part is the libraries you will need. So add:

<path id="toolslib">
        <path location="lib/hibernate-support/hibernate-tools.jar" />
        <path location="lib/hibernate-support/hibernate3.jar" />
        <path location="lib/hibernate-support/freemarker.jar" />
        <path location="lib/hibernate-support/jtidy-r938.jar" />
        <path location="lib/ojdbc14.jar" />
</path>

You should already have the hibernate-tools.jar, hibernate3.jar, and ojdbc14.jar files on you machine. So just change the path to them. The freemaker.jar and jtidy-r938.jar will need to be downloaded, as I did not have those.

Below this in the build.xml you will need to add:

<taskdef name="hibernatetool"
     classname="org.hibernate.tool.ant.HibernateToolTask"
     classpathref="toolslib">
    <classpath>
        <fileset dir="lib">
            <include name="**/*.jar"/>
        </fileset>
    </classpath>
</taskdef>

The last section you will need is the set to run in the post-clean section:

<target name="-post-clean">
        <delete dir="src/*Put the foler where your pojos and hbm.xml files are located*"/>
        <hibernatetool>
            <jdbcconfiguration
                configurationfile="src\hibernate.cfg.xml"
                packagename="*the package where you want them recreated*"
                revengfile="src\hibernate.reveng.xml"
                detectmanytomany="true"
            />
            <hbm2hbmxml destdir="src" />
            <hbm2java  destdir="src" />
        </hibernatetool>
</target>

  • The delete portion will delete the existing hbm and pojo files, before they are re-created.
  • The configurationfile points to your main configuration file.
  • The package name is the dot separated package you want them created in (com.stackoverflow.pojo for example).
  • The revengfile is the reverse engineering xml file to use when creating the hbm and pojo files.
  • The hbm2hbmxml will create the hbm.xml files of your tables.
  • The hbm2java will create the java pojo files of your tables.

Now to get the Oracle Timestamps to be something other than Serializable, edit the hibernate.reveng.xml file and add:

<type-mapping>
        <sql-type jdbc-type="OTHER" hibernate-type="java.sql.Timestamp" />
</type-mapping>

just after the schema-selection tag.

So a clean and build and the timestamps will not be java.sql.Timestamp instead of Serializable objects.

This is a long answer I know, but this should also work for any other changes that you would have to set in the hibernate.reveng.xml file (I think). I am no expert in hibernate, so your mileage may vary with this.

UPDATE: So after some googling I found this site about custom ant tasks in Netbeans. So I simply changed the name of the target to be gen-dao and now it does not run every time I do a clean and build, just when I specifically invoke it.

这篇关于如何将oracle时间戳映射到hibernate中适当的java类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆