如何将对象序列化到数据库以便 Hibernate 在 Java 中读取

发布于 2024-11-06 15:51:48 字数 554 浏览 1 评论 0原文

我目前正在编写一个工具来插入使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下雨或天晴 2024-11-13 15:51:49

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.

玩套路吗 2024-11-13 15:51:49

您可以将 Java 对象序列化为字节,然后将其存储在 BLOB 中。

序列化:

ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(byteOut);
objOut.writeObject(object);
objOut.close();
byteOut.close();
byte[] bytes = byteOut.toByteArray()

反序列化:

 public <T extends Serializable> T getObject(Class<T> type) throws IOException, ClassNotFoundException{
        if(bytes == null){
            return null;
        }
        ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
        ObjectInputStream in = new ObjectInputStream(byteIn);
        T obj = (T) in.readObject();
        in.close();
        return obj;
    }

You can serealize a Java object into bytes and then store it in a BLOB.

Serialize:

ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(byteOut);
objOut.writeObject(object);
objOut.close();
byteOut.close();
byte[] bytes = byteOut.toByteArray()

Deserialize:

 public <T extends Serializable> T getObject(Class<T> type) throws IOException, ClassNotFoundException{
        if(bytes == null){
            return null;
        }
        ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
        ObjectInputStream in = new ObjectInputStream(byteIn);
        T obj = (T) in.readObject();
        in.close();
        return obj;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文