Wrapper for Serializing an Object in Java JPA -


i want persist object in java. after research, found can done using wrapper class.

here's came with:

public class objectwrapper implements serializable {     private static final long serialversionuid = -343767114791490410l;     private objectwrapper object;    public objectwrapper(object object) {          this.object = (objectwrapper) object;         try {             serialize(object);         } catch (notserializableexception e) {             e.printstacktrace();         }     }    private byte[] serialize(object object) throws notserializableexception {      try {         // serialize data object file         objectoutputstream out = new objectoutputstream(                 new fileoutputstream("object.ser"));         out.writeobject(object);         out.close();          // serialize data object byte array         bytearrayoutputstream bos = new bytearrayoutputstream();         out = new objectoutputstream(bos);         out.writeobject(object);         out.close();          // bytes of serialized object         byte[] buf = bos.tobytearray();          return buf;      } catch (ioexception e) {         throw new notserializableexception("error serializing object!");     }   }    public objectwrapper getobject() throws notserializableexception,         classnotfoundexception {     try {         fileinputstream in = new fileinputstream("object.ser");         objectinputstream reader = new objectinputstream(in);         objectwrapper x = new objectwrapper(object);         x = (objectwrapper) reader.readobject();         reader.close();         return x;      } catch (ioexception e) {         throw new notserializableexception("error serializing object!");     }    }  } 

since i'm learning jpa, question is: correct approach problem ? can wrapper class simplified ? find bit odd first serialize object file. necessary ?

jpa it's abstraction above different orms. so, main target of object-relational mapping. in case save object byte array without mapping on relational model (in database meaning). in general serialization , jpa/orm it's different tasks different aims. if want persist object in relational database may save blob in 1 column in table.

@entity public class objectwrapper {      @id     @generatedvalue(strategy= generationtype.auto)     private long objectid;      @lob     private serializable object;      public long getobjectid() {         return objectid;     }      public void setobjectid(long objectid) {         this.objectid = objectid;     }      public serializable getobject() {         return object;     }      public void setobject(serializable object) {         this.object = object;     } } 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -