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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5966039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:44:07  来源:igfitidea点击:

How do I Serialize Object to Database for Hibernate to read in Java

javadatabasehibernateserialization

提问by Patrick McDaniel

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.

我目前正在编写一个工具来插入使用 Hibernate 的现有企业应用程序。我的工具在安装时需要将一些值写入数据库,其中一列是设置描述符对象的序列化版本。这个对象有两个对象列表和一些基本类型。

My current approach is to create a ByteArrayOutputStreamand an ObjectOutputStreamand then write the ObjectOutputStreamto 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:

我目前的做法是创建ByteArrayOutputStreamObjectOutputStream然后写ObjectOutputStreamByteArrayOutputStream,然后将得到的字节数组通过与Spring的1SimpleJdbcTemplate1的SQL。我目前使用这种方法的问题是,当企业工具提取我的行时,它无法使用以下内容对列进行反序列化:

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.

我觉得我可能需要序列化内部对象,但不知道如何做到这一点并将所有内容放在一起。

回答by Patrick McDaniel

Ended up solving my own problem. In the hibernate API there is a class called SerializationHelperthat 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.

最终解决了我自己的问题。在休眠 API 中,有一个名为SerializationHelper的类,它有一个静态函数serialize(Serializable obj),我可以用它来序列化我的对象,然后将其插入到数据库中。Hibernate 然后能够在企业应用程序中读取它。

回答by JustinKSU

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

您可以将 Java 对象序列化为字节,然后将其存储在 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;
    }