您好,欢迎访问一九零五行业门户网

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

关联对象的延迟加载就不说了,大家都知道。 关于普通字段的延迟加载,尤其是lob字段,若没有延迟加载,对性能影响极大。然而简单的使用 @basic(fetch = fetchtype.lazy) 注解并没有效果。hibernate对此的解释是lazy property loading requires buildtime byt
关联对象的延迟加载就不说了,大家都知道。
关于普通字段的延迟加载,尤其是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
其它类似信息

推荐信息