laker001
laker001

Reputation: 417

Wrapper for Serializing an Object in Java JPA

I want to persist an Object in Java. After some research, i found that this can be done using a wrapper class.

Here's what i came up 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 to a file
        ObjectOutputStream out = new ObjectOutputStream(
                new FileOutputStream("object.ser"));
        out.writeObject(object);
        out.close();

        // Serialize data object to a byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        out = new ObjectOutputStream(bos);
        out.writeObject(object);
        out.close();

        // Get the bytes of the serialized object
        byte[] buf = bos.toByteArray();

        return buf;

    } catch (IOException e) {
        throw new NotSerializableException("Error serializing the 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 the object!");
    }

  }

}

Since i'm learning JPA, my question is: is this a correct approach to this problem ? Can this wrapper class be simplified ? I find it a bit odd to first serialize the object to a file. Is this really necessary ?

Upvotes: 1

Views: 3946

Answers (1)

dmitrievanthony
dmitrievanthony

Reputation: 1561

JPA it's abstraction above different ORMs. So, a main target of it is object-relational mapping. In your case you just save object as byte array without mapping on any relational model (in database meaning). In general serialization and JPA/ORM it's different tasks with different aims. If you want to persist object as is in relational database you may save it as BLOB in one column in some 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;
    }
}

Upvotes: 3

Related Questions