2010-07-11 27 views
0

我有一个需要转换为“blob”对象并存储到数据库中的序列化对象。以前我们用来存储一个由其他项目对象定义的对象,但是它遵循序列化规则,因为它遇到了很多问题,所以我们决定改变现在只包含原始对象的结构“blob”对象(String,布尔值,整数等)。到目前为止,我们可以使所有的属性期望两个将对象转换为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(); 

    } 

上述被写入团块 斑点包含

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;  

在报告对象有follwoing属性

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

可变对象可以有任何形式的阵列列表,字符串,布尔或一个类对象。 但是,一旦解码为实际对象,它就需要将类型转换为任意类型。我们的目标是将此对象转换为原始类型和存储中的任何一种,并将其作为原始值恢复。基本上我们认为每个对象都是以对象类型附加的字符串的形式附加到它上面,如'1_整数','Y_Boolean'并转换为blob,同时恢复拆分字符串并将该字符串用作反射的一部分以获取用于投射的对象类型。但这不是可行或正确的解决方案。有任何想法吗。

回答

2

如果你抛弃了你的对象并存储了单独的字段而不是一大块数据,这不是更简单吗?

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