Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: Arvik on Aug 1st, 2012  |  syntax: Java 5  |  size: 0.83 KB  |  views: 105  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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.     }
clone this paste RAW Paste Data