2010-07-11 5 views
0

'blob'개체로 변환하여 데이터베이스에 저장해야하는 serialize 된 개체가 있습니다. 이전에는 다른 프로젝트 객체에 의해 정의 된 객체를 저장하는 데 사용되었지만 여러 문제가 발생하여 직렬화 규칙을 따르기 때문에 원시 객체 만 포함하는 구조의 '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;  

을 포함

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

변수 객체는 같은 배열 목록, 문자열, 부울을 가질 수 있습니다 또는 클래스 객체. 그러나 실제 객체로 디코딩 한 후에는 형식이 무엇이든간에 형변환을해야합니다. 우리의 목표는이 객체를 원시 유형 및 저장소로 변환하고 원래 값으로 다시 가져 오는 것입니다. 본질적으로 모든 객체는 '1_integer', 'Y_Boolean'과 같이 객체의 유형을 가진 문자열로 생각하고 BLOB로 변환하고 분할 문자열을 되살리는 역할을하며 반사를 위해 객체의 유형을 가져 오기 위해 문자열을 사용합니다. 그러나 이것은 실현 가능하거나 정확한 해결책이 아닙니다. 어떤 아이디어.

답변

2

개체를 분해하고 1 개의 큰 데이터 덩어리가 아닌 개별 필드를 저장하면 더 간단하지 않습니까?

한편, 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); 
    }  
} 
관련 문제