How to express a Serializable Blob type in a hibernate mapping file

对着背影说爱祢 提交于 2019-12-11 11:38:21

问题


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.

回答1:


<property
    name="data"
    type="blob"
    column="DATA"/>
...

should work.




回答2:


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)



来源:https://stackoverflow.com/questions/7601576/how-to-express-a-serializable-blob-type-in-a-hibernate-mapping-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!