如何将对象序列化到数据库以便 Hibernate 在 Java 中读取
我目前正在编写一个工具来插入使用 Hibernate 的现有企业应用程序。我的工具在安装时需要将一些值写入数据库,其中其中一列是设置描述符对象的序列化版本。该对象有两个对象列表和一些基本类型。
我当前的方法是创建一个 ByteArrayOutputStream 和一个 ObjectOutputStream,然后将 ObjectOutputStream 写入 ByteArrayOutputStream,然后传递使用 Spring 的 1SimpleJdbcTemplate1 将生成的字节数组放入 sql 中。我目前使用这种方法的问题是,当企业工具拉取我的行时,它无法通过以下方式反序列化列:
org.springframework.orm.hibernate3.HibernateSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize
我觉得我可能需要序列化内部对象,但不知道如何做到这一点并将所有内容放在一起。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最终解决了我自己的问题。在 hibernate API 中有一个名为 SerializationHelper< /a> 有一个静态函数
serialize(Serializing 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.您可以将 Java 对象序列化为字节,然后将其存储在 BLOB 中。
序列化:
反序列化:
You can serealize a Java object into bytes and then store it in a BLOB.
Serialize:
Deserialize: