如何将对象序列化到数据库以供Hibernate在Java中读取 [英] How do I Serialize Object to Database for Hibernate to read in Java
问题描述
我目前正在写一个工具来插入使用Hibernate的现有企业应用程序。我的安装工具需要在数据库中写入一些值,其中一个列是设置描述符对象的序列化版本。这个对象有两个对象列表和一些原始类型。
I'm currently writing a tool to plug into an existing enterprise application that uses Hibernate. My tool at install time needs to write some values into the database where one of the columns is a serialized version of a setting descriptor object. This object has two lists of objects and a few primitive types.
我当前的方法是创建一个 ByteArrayOutputStream
ObjectOutputStream
,然后将 ObjectOutputStream
写入 ByteArrayOutputStream
然后将生成的字节数组传递给Spring的1SimpleJdbcTemplate1的sql。我这个方法的当前问题是,当企业工具拉我的行,它无法反序列化列与以下:
My current approach is to create a ByteArrayOutputStream
and an ObjectOutputStream
and then write the ObjectOutputStream
to the ByteArrayOutputStream
, then passing the resulting byte array into the sql with Spring's 1SimpleJdbcTemplate1. My current issue with this approach is that when the enterprise tool pulls my rows it fails to de-serialze the column with the following:
org.springframework.orm.hibernate3.HibernateSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize
我觉得我可能需要序列化内在的物体,但没有线索如何做到这一点,保持一切。
I feel I may need to serialize the inner objects, but have no clue how to do that and keep everything together.
推荐答案
结束了解决我自己的问题。在hibernate API中有一个类叫做 SerializationHelper ,它有一个静态函数 serialize(Serializable obj)
,我可以使用它序列化我的对象,然后将其插入数据库。 Hibernate然后能够在企业应用程序中读取它。
Ended up solving my own problem. In the hibernate API there is a class called SerializationHelper that has a static function serialize(Serializable obj)
which I was able to use to serialize my object and then insert it into the database. Hibernate was then able to read it in the enterprise app.
这篇关于如何将对象序列化到数据库以供Hibernate在Java中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!