如何在hibernate映射文件中表达一个Serializable Blob类型 [英] How to express a Serializable Blob type in a hibernate mapping file

查看:597
本文介绍了如何在hibernate映射文件中表达一个Serializable Blob类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个遗留应用程序,它使用hibernate将对象映射到数据库中。它使用Hibernate Mapping XML文件来执行此操作。 java类包含两个实现java Serializable的属性abc和def。映射定义如下:

 < property name =abccolumn =ABCtype =serializablelength = 32672/> 
< property name =defcolumn =DEFtype =serializablelength =32672/>

当我尝试使用oracle进行设置时,出现一个令人讨厌的错误ORA-01754:a表可能只包含一个LONG类型的列,这实质上是在抱怨在一个表中创建两个'long raw'列。 Oracle不喜欢这样。在阅读完这个问题后,推荐的方法是使用Blob而不是'long raw'类型。



我的问题是,我如何在hibernate映射文件中表达使用映射到blob列的可序列化类型?我认为会有一个serializable_blob类型,但似乎没有。



我知道使用@Basic和@Lob可以使用JPA注释。应该也可以使用hibernate映射文件。如何在hibernate映射文件中完成这项工作?



更新:

下面的命令不会像serializable那样工作:




  • type = binary - 这一个需要一个字节[]。对于Serializable类不起作用。给出ClassCastException。

  • type = blob - - 这个需要java.sql.Blob。对于Serializable类不起作用。给出ClassCastException。

  • type = materialized_blob - - 这一个需要一个字节[]。对于Serializable类不起作用。给出ClassCastException。


解决方案

好的,按照上面的评论做了更多的研究, ,我发现它。



在Hibernate 3.5 + Spring 3.1中,我使用了Spring的 org.springframework.orm.hibernate3.support.BlobSerializableType 。现在我升级到Hibernate 4.3,该选项不再可用。我确实发现了列映射的类型为OP,但在我的应用程序中有各种映射到BLOB字段的字符串(遗留)。

所以,正如我在上面的注释中,我找到了 org.hibernate.type.SerializableToBlob 类型,它是参数化的。

 < property name =说明下面介绍了如何使映射工作(使用良好的老式hbm.xml映射) column =TEXT> 
< type name =org.hibernate.type.SerializableToBlobType>
< param name =classname> java.lang.String< / param>
< / type>
< / property>

而这似乎是个窍门。 ( classname 值应该是您映射的属性的类型,我认为)


I have a legacy application that uses hibernate for mapping objects into a database. It uses the Hibernate Mapping XML file to do so. The java class contains two properties abc and def that implement java Serializable. The mapping is defined this way:

<property name="abc" column="ABC" type="serializable" length="32672"/>
<property name="def" column="DEF" type="serializable" length="32672"/>

When I try to set this up with oracle, I get a nasty error "ORA-01754: a table may contain only one column of type LONG" which essentially is complaining about creating two 'long raw' columns in one table. Oracle does not like this. After reading up on the issue, the recommended approach is to use blobs instead of 'long raw' types.

My question is, how can I express in the hibernate mapping file to use a serializable type mapped into a blob column? I would think there would be a serializable_blob type but there does not seem to be.

I know this is possible with JPA annotations using @Basic and @Lob. It should also be possible using the hibernate mapping file. How can this be done in the hibernate mapping file?

Update:

The following do not work as Serializable works:

  • type=binary - This one expects a byte[]. Does not work for Serializable classes. Gives ClassCastException.
  • type=blob - - This one expects a java.sql.Blob. Does not work for Serializable classes. Gives ClassCastException.
  • type=materialized_blob - - This one expects a byte[]. Does not work for Serializable classes. Gives ClassCastException.

解决方案

Ok, did some more research following my comment above and, by Jove, I found it.

In Hibernate 3.5 + Spring 3.1, I used Spring's org.springframework.orm.hibernate3.support.BlobSerializableType. Now I'm upgrading to Hibernate 4.3, that option isn't available anymore. I did find the type to column mappings as OP, but in my application there are various Strings (legacy) that are mapped to BLOB fields.

So, as I reported in the comment above, I found the org.hibernate.type.SerializableToBlob type, which is parameterize. Below how I got the mapping to work (using good old-fashioned hbm.xml mappings)

<property name="description" column="TEXT">
    <type name="org.hibernate.type.SerializableToBlobType">
        <param name="classname">java.lang.String</param>
    </type>
</property>

And that appears to do the trick. (the classname value should be the type of the attribute you are mapping, I think)

这篇关于如何在hibernate映射文件中表达一个Serializable Blob类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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