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

关于普通字段的延迟载入,尤其是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的持久类:

public class PublicSchemeTaskFile  implements java.io.Serializable , FieldHandled  {

    // Fields    

     /**
*
*/
private static final long serialVersionUID = -8297912895820802249L;
private Integer id;
private PublicTask publicSchemeTask;
private Integer fileType;
private String fileName;
private byte[] content; private FieldHandler fieldHandler;//用于延迟载入表字段。关联对象延迟载入的话无需此技术 @JSON(serialize = false)
public FieldHandler getFieldHandler() {
return fieldHandler;
} public void setFieldHandler(FieldHandler fieldHandler) {
this.fieldHandler = fieldHandler;
}
// Constructors /** default constructor */
public PublicSchemeTaskFile() {
} /** minimal constructor */
public PublicSchemeTaskFile(Integer id) {
this.id = id;
} // Property accessors
@Id @Column(name="ID", unique=true, nullable=false, precision=22, scale=0)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator = "PUBLIC_SCHEME_TASK_FILE_SEQ")
public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
}
@JSON(serialize = false)
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="PUBLIC_TASK_ID") public PublicTask getPublicSchemeTask() {
return this.publicSchemeTask;
} public void setPublicSchemeTask(PublicTask publicSchemeTask) {
this.publicSchemeTask = publicSchemeTask;
} @Column(name="FILE_TYPE", precision=22, scale=0) public Integer getFileType() {
return this.fileType;
} public void setFileType(Integer fileType) {
this.fileType = fileType;
} @Column(name="FILE_NAME", length=50) public String getFileName() {
return this.fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
}
@JSON(serialize = false)
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name="CONTENT") public byte[] getContent() {
if (fieldHandler != null) {
return (byte[]) fieldHandler.readObject(this, "content", content);
}
return null;
} public void setContent(byte[] content) {
this.content = content;
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PublicSchemeTaskFile other = (PublicSchemeTaskFile) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
} }

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

hibernate 普通字段延迟载入无效的解决的方法的更多相关文章

  1. hibernate之4.延迟载入

    延迟载入: 仅仅有当使用以实体对象的属性(除主键属性外)时,才会发送查询语句到数据库 get不支持延迟载入 @Test public void getTest(){ Session session=n ...

  2. java运行shell命令,chmod 777 xxx,改变权限无效的解决的方法。

    在java程序中运行shell命令,改变文件的权限.能够在命令行中运行 chmod 777 <span style="font-family: Arial, Helvetica, sa ...

  3. 解决的方法:mysql_connect()不支持请检查mysql模块是否正确载入

    故障现象:linux 安装discuz 错误提示:mysql_connect() 不支持请检查mysql模块是否正确载入. 解决的方法:查看/usr/lib/php/modules/ (64位的看/u ...

  4. Hibernate中的session和load延迟载入矛盾问题,怎样解决?

    假设延迟载入出现session close的情况下 方法1.在web.xml中配置spring的openSessionInViewFilter <filter>  <filter-n ...

  5. Hibernate 延迟载入

    一.延迟载入定义                 延迟载入,也叫懒载入,它是Hibernate为提高程序运行效率而提供的一种机制,即当仅仅有真正使用该对象的数据时才会创建. 说白了,所谓的延迟载入不是 ...

  6. hibernate手动设置的id无效的原因与解决方法

    在使用Hibernate的过程中,发现手动设置的id(主键)无效,Hibernate仍然会在保存(调用Hibernate提供的merge()方法)的时候自动生成一个随机的id. 经过调试发现问题出在了 ...

  7. 设置height:100%无效的解决方法

    设置height:100%无效的解决方法 刚接触网页排版的新手,常出现这种情况:设置table和div的高height="100%"无效,使用CSS来设置height:" ...

  8. [转]Hibernate中property-ref的应用,常用来解决遗留数据库One To Many关系

    [转]Hibernate中property-ref的使用,常用来解决遗留数据库One To Many关系 1.如表Class(ClassID,Class_No,ClassName)与Student(S ...

  9. 原声JS瀑布流加延迟载入

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. HDU4340 Capturing a country DP

    自己原来写的两个维度的DP有错,看了半天这个大牛的blog.http://blog.csdn.net/cyberzhg/article/details/7840922 题意:A军队和B军队要一起占领一 ...

  2. html表单——使用frameset写一个导航栏效果

    主页面: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4 ...

  3. 涨知识---IV

    1.如何减少换页错误? A.进程倾向于占用CPU. B.访问局部性(locality of reference)满足进程要求. C.进程倾向于占用I/O. D.使用基于最短剩余时间(shortest ...

  4. Mysql中的索引()key 、primary key 、unique key 与index区别)

    CREATE TABLE pre_forum_post ( pid int(10) unsigned NOT NULL COMMENT '帖子id', fid mediumint(8) unsigne ...

  5. 01--Java集合知识

    一.Java集合概览 Java中集合分2大块,Collection和Map,分别代表不同功能的集合类对象,整体结构图如下: Collection├List│├LinkedList│├ArrayList ...

  6. tomcat报错org.springframework.web.context.ContextLoaderListener找不到

    tomcat报错org.springframework.web.context.ContextLoaderListener找不到. 最后解决办法:将jar包copy到web-inf下面的lib中. 你 ...

  7. Python语言之模块

    模块基本上就是一个包含了所有你定义的函数和变量的文件.它用处在于使你你能在别的程序中重用它提供的功能和服务. 1.模块的使用方法 模块的文件名必须以.py作为扩展名. 当我们需要使用某个模块时,我们需 ...

  8. 11-c++虚拟函数

    虚拟函数 #include "stdio.h" class A{ public: void print() { printf("%s","this i ...

  9. react 子组件给父组件传值

    import React from 'react'import '../page1/header.css'import { Table } from 'antd'import Child from ' ...

  10. php第十九节课

    JQUERY <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...