我有一个序列化对象,需要转换为'blob'对象并存储到数据库中。以前我们用来存储一个由其他项目对象定义的对象,但是它遵循序列化规则,因为它遇到了很多问题,所以我们决定改变结构'blob'对象,它现在只包含原始对象(String, boolean,Integer等)。到目前为止,我们可以使所有属性都期望两个。
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;
变量Object可以包含任何类似的数组列表,字符串,布尔值或类对象。 但是一旦解码到实际对象,它需要将类型转换为任何类型。我们的目标是将此对象转换为任何原始类型和存储,同时将其作为原始值进行检索。基本上我们认为每个对象都是具有对象类型的字符串,如'1_integer','Y_Boolean'并且转换为blob并且在恢复分割字符串时使用字符串作为反射的一部分来获取用于投射的对象类型。但这不是可行或正确的解决方案。任何想法。
答案 0 :(得分: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);
}
}