开发者

Problem in Converting a Object to Blob object

I have a serialized object which needs to be converted to 'blob' object and store into Database. Previously we used to store an object which is defined by other project objects but doest follow rules of serialization because of which it ran into numerous problems and so we decided to change the structure 'blob' object which now contains only primitive objects only(String,boolean,Integer etc). So far we could make all attribute expect two.

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();

    }

The above is writing blob Blob contains

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;       

In the report object there are follwoing attribute

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

The variable Object can have any like array list,string,boolean or a class object. But once decode to actual object it needs to type cast to whatever type it is.Our objective is to convert this object into any of primitive type and store and while retrieving back as original value. Essentially we thought every object as string with type of object append to it like '1_integer' ,'Y_Boolean' and convert to blob and while reviving split string and use the string as part of reflection to get the type of object for casting. But this not feasible or correct solution. Any ideas.


Wouldn't it be simpler if you broke down your object and stored the separate fields rather than 1 big chunk of data?

On the other hand, you might want to try Hibernate. This framework basically allows you to store objects in relational databases and then, it allows you to re-create the object back from a relational database automatically. It is simple to use, the example I am adding has been obtained from 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);
    }    
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜