代码之家  ›  专栏  ›  技术社区  ›  GustyWind

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

  •  0
  • GustyWind  · 技术社区  · 14 年前

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

    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包含

    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;       
    

    在report对象中有以下属性

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

    变量对象可以有任何类似的数组列表、字符串、布尔值或类对象。 但一旦解码成实际的对象,它就需要类型转换成它是什么类型,我们的目标是将这个对象转换成任何原始类型,并存储和检索回原始值。本质上,我们认为每个对象都是字符串,对象类型附加在字符串上,如“1_integer”、“Y_Boolean”并转换为blob,同时恢复拆分字符串并将字符串用作反射的一部分,以获取要转换的对象类型。但这不可行也不正确。任何想法。

    1 回复  |  直到 14 年前
        1
  •  2
  •   npinti    14 年前

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

    另一方面,你可能想试试 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);
        }    
    }