java - 将对象转换为 Blob 对象时出现问题

标签 java serialization

我有一个序列化对象,需要转换为“blob”对象并存储到数据库中。以前我们用来存储一个由其他项目对象定义的对象,但不遵循序列化规则,因此它遇到了许多问题,因此我们决定更改结构“blob”对象,该对象现在仅包含原始对象(String, boolean 值、整数等)。到目前为止,我们可以使所有属性都期望为两个。

private byte[] encode(ScheduledReport schedSTDReport)
{
    byte[] bytes = null;
    try
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(schedSTDReport);
        oos.flush(); 
        oos.close(); 
        bos.close();
        //byte [] data = bos.toByteArray();
        //ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //GZIPOutputStream out = new GZIPOutputStream(baos);
        //XMLEncoder encoder = new XMLEncoder(out);
        //encoder.writeObject(schedSTDReport);
        //encoder.close();
        bytes = bos.toByteArray();
        //GZIPOutputStream out = new GZIPOutputStream(bos);
        //out.write(bytes);
        //bytes = bos.toByteArray();

    }

上面是写blob Blob 包含

public class ScheduledReport extends ScheduledReportInfo implements Serializable {



    private SupervisoryScope                   _scope          = null;
    private Report                             _attributes     = null;
    private ScheduledReportScheduleBase        _schedule       = null;
    private HashMap                            selectionList   = new HashMap();
    private EmailInfo                          _emailInfo      = null;
    private String                             _pdfFileName    = null;
    private short                              _baseDateOffset = -1;       

报表对象中有以下属性

private String     deflt = null;
private Object     guiValue = null;
protected Object   serverValue = null;

变量对象可以有任何像数组列表、字符串、 boolean 值或类对象。 但是,一旦解码为实际对象,它就需要类型转换为任何类型。我们的目标是将这个对象转换为任何原始类型并存储,同时检索回原始值。本质上,我们认为每个对象都是字符串,其对象类型附加到它,如 '1_integer' 、'Y_Boolean' 并转换为 blob,同时恢复分割字符串并使用字符串作为反射的一部分来获取用于转换的对象类型。但这不是可行或正确的解决方案。任何想法。

最佳答案

如果您分解对象并存储单独的字段而不是一大块数据,不是更简单吗?

另一方面,您可能想尝试 Hibernate 。该框架基本上允许您将对象存储在关系数据库中,然后它允许您自动从关系数据库重新创建对象。使用起来很简单,我添加的例子是从here获取的

package org.kodejava.example.hibernate.app;

import java.util.Date;

import org.hibernate.Session;

public class LabelManager {
    private Label getLabel(Long id) {
        Session session = SessionFactoryHelper.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        /*
         * We get back Label object from database by calling the Session object
         * get() method and passing the object type and the object id to be
         * read.
         */
        Label label = (Label) session.get(Label.class, id);
        session.getTransaction().commit();

        return label;
    }

    private void saveLabel(Label label) {
        /*
         * To save an object we first get a session by calling getCurrentSession()
         * method from the SessionFactoryHelper class. Next we create a new
         * transcation, save the Label object and commit it to database,
         */
        Session session = SessionFactoryHelper.getSessionFactory().getCurrentSession();

        session.beginTransaction();        
        session.save(label);        
        session.getTransaction().commit();
    }

    public static void main(String[] args) {        
        LabelManager manager = new LabelManager();

        /*
         * Creates a Label object we are going to stored in the database. We
         * set the name, modified by and modified date information.
         */
        Label label = new Label();
        label.setName("Sony Music");
        label.setModifiedBy("admin");
        label.setModifiedDate(new Date());

        /*
         * Call the LabelManager saveLabel method.
         */
        manager.saveLabel(label);

        /*
         * Read the object back from database.
         */
        label = manager.getLabel(label.getId());
        System.out.println("Label = " + label);
    }    
}

关于java - 将对象转换为 Blob 对象时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3222340/

相关文章:

java - Spring data JPA - 封装的表达式不是有效的表达式

ruby-on-rails - 如何向在 Rails 中序列化为 JSON 的 ActiveRecord 记录添加额外数据?

java - Spring + Hibernate = 未知实体

Java 8 获取类成员的准确顺序

java - JSF 中的重新渲染属性

来自 jacoco 的 java.lang.NoSuchFieldException 错误

java - 反序列化ArrayList中的对象

java - 如何使用多个类的序列化来实现备份和恢复?

asp.net-mvc-3 - 使用序列化 View 结果的 mvc3 单元测试最佳实践

java - Jackson 包括用户设置的空值?