Advertisement
Arvik

Untitled

Aug 1st, 2012
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.83 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.*;
  4. import javax.persistence.*;
  5.  
  6. @Entity
  7. public class XXXEntity {
  8.  
  9.     @Id private Long id;
  10.  
  11.     @Transient private Object entity;
  12.  
  13.     @Lob private byte[] binEntity;
  14.    
  15.     public Object getEntity() {
  16.     try {
  17.         return this.entity!=null ? this.entity : (this.entity = new ObjectInputStream(new ByteArrayInputStream(this.binEntity)).readObject());
  18.     } catch (Exception e) {
  19.         throw new RuntimeException(e);
  20.     }
  21.     }
  22.  
  23.     public void setEntity(Object entity) {
  24.     this.entity = entity;
  25.     ByteArrayOutputStream baos = new ByteArrayOutputStream();
  26.     try {
  27.         final ObjectOutputStream oos = new ObjectOutputStream(baos);
  28.         oos.writeObject(entity);
  29.         oos.close();
  30.         this.binEntity = baos.toByteArray();
  31.     } catch (IOException e) {
  32.         throw new RuntimeException(e);
  33.     }
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement