开发者

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

开发者 https://www.devze.com 2023-04-09 16:00 出处:网络
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

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.开发者_Go百科 Gives ClassCastException.


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

should work.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消