hibernate 普通字段延迟加载无效的解决办法

关联对象的延迟加载就不说了,大家都知道。

关于普通字段的延迟加载,尤其是lob字段,若没有延迟加载,对性能影响极大。然而简单的使用 @Basic(fetch = FetchType.LAZY) 注解并没有效果。hibernate对此的解释是Lazy property loading requires buildtime bytecode instrumentation. If your persistent classes are not enhanced, Hibernate will ignore lazy property settings and return to immediate fetching.

而bytecode instrumentation的介绍可以参考http://www.correlsense.com/blog/java-bytecode-instrumentation-an-introduction/,本文不多作介绍。

正是因为我们的persistent classes没有使用bytecode instrumentation增强,才导致了普通字段无法延迟加载。

因此要改写一下。以下为一个使用了bytecode instrumentation的持久类:

[java]  view plain  copy
  1. public class PublicSchemeTaskFile  implements java.io.Serializable , FieldHandled  {  
  2.   
  3.   
  4.     // Fields      
  5.   
  6.      /** 
  7.      *  
  8.      */  
  9.      private static final long serialVersionUID = -8297912895820802249L;  
  10.      private Integer id;  
  11.      private PublicTask publicSchemeTask;  
  12.      private Integer fileType;  
  13.      private String fileName;  
  14.      private byte[] content;  
  15.        
  16.      private FieldHandler fieldHandler;//用于延迟加载表字段,关联对象延迟加载的话无需此技术  
  17.       
  18.     @JSON(serialize = false)  
  19.     public FieldHandler getFieldHandler() {  
  20.         return fieldHandler;  
  21.     }  
  22.   
  23.     public void setFieldHandler(FieldHandler fieldHandler) {  
  24.         this.fieldHandler = fieldHandler;  
  25.     }  
  26.     // Constructors  
  27.   
  28.     /** default constructor */  
  29.     public PublicSchemeTaskFile() {  
  30.     }  
  31.   
  32.     /** minimal constructor */  
  33.     public PublicSchemeTaskFile(Integer id) {  
  34.         this.id = id;  
  35.     }  
  36.      
  37.   
  38.      
  39.      
  40.   
  41.     // Property accessors  
  42.     @Id   
  43.       
  44.     @Column(name="ID", unique=true, nullable=false, precision=22, scale=0)  
  45.     @GeneratedValue(strategy=GenerationType.SEQUENCE,generator = "PUBLIC_SCHEME_TASK_FILE_SEQ")  
  46.     public Integer getId() {  
  47.         return this.id;  
  48.     }  
  49.       
  50.     public void setId(Integer id) {  
  51.         this.id = id;  
  52.     }  
  53.     @JSON(serialize = false)  
  54.     @ManyToOne(fetch=FetchType.LAZY)  
  55.      @JoinColumn(name="PUBLIC_TASK_ID")  
  56.   
  57.     public PublicTask getPublicSchemeTask() {  
  58.         return this.publicSchemeTask;  
  59.     }  
  60.       
  61.     public void setPublicSchemeTask(PublicTask publicSchemeTask) {  
  62.         this.publicSchemeTask = publicSchemeTask;  
  63.     }  
  64.       
  65.     @Column(name="FILE_TYPE", precision=22, scale=0)  
  66.   
  67.     public Integer getFileType() {  
  68.         return this.fileType;  
  69.     }  
  70.       
  71.     public void setFileType(Integer fileType) {  
  72.         this.fileType = fileType;  
  73.     }  
  74.       
  75.     @Column(name="FILE_NAME", length=50)  
  76.   
  77.     public String getFileName() {  
  78.         return this.fileName;  
  79.     }  
  80.       
  81.     public void setFileName(String fileName) {  
  82.         this.fileName = fileName;  
  83.     }  
  84.     @JSON(serialize = false)  
  85.     @Lob  
  86.     @Basic(fetch = FetchType.LAZY)   
  87.     @Column(name="CONTENT")  
  88.   
  89.     public byte[] getContent() {  
  90.          if (fieldHandler != null) {  
  91.                 return (byte[]) fieldHandler.readObject(this"content", content);  
  92.              }  
  93.          return null;  
  94.     }  
  95.       
  96.     public void setContent(byte[] content) {  
  97.         this.content = content;  
  98.     }  
  99.    
  100.   
  101.     @Override  
  102.     public int hashCode() {  
  103.         final int prime = 31;  
  104.         int result = 1;  
  105.         result = prime * result + ((id == null) ? 0 : id.hashCode());  
  106.         return result;  
  107.     }  
  108.   
  109.     @Override  
  110.     public boolean equals(Object obj) {  
  111.         if (this == obj)  
  112.             return true;  
  113.         if (obj == null)  
  114.             return false;  
  115.         if (getClass() != obj.getClass())  
  116.             return false;  
  117.         PublicSchemeTaskFile other = (PublicSchemeTaskFile) obj;  
  118.         if (id == null) {  
  119.             if (other.id != null)  
  120.                 return false;  
  121.         } else if (!id.equals(other.id))  
  122.             return false;  
  123.         return true;  
  124.     }  
  125.      
  126.   
  127.   
  128.   
  129.   
  130.   
  131.   
  132.   
  133.   
  134. }  


关键在于FieldHandled接口和lob字段的getter

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值