The solution for delayed loading of common hibernate fields is invalid.

Source: Internet
Author: User

The solution for delayed loading of common hibernate fields is invalid.

We don't need to talk about the delayed loading of associated objects.


Delayed loading of common fields, especially lob fields, has a great impact on performance if there is no delayed loading. However, the simple use of the @ Basic (fetch = FetchType. LAZY) annotation has no effect. Hibernate explains this as 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.

For more information about bytecode instrumentation, see explain.

It is precisely because our persistent classes did not use bytecode instrumentation enhancement that caused the loading of common fields to be delayed.

So rewrite it. The following is a persistent class that uses 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; // used to delay loading of table fields. This technique is not required if the associated object is delayed loading. @ JSON (serialize = false) public Field Handler 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 public SchemeTask) {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;} @ JSO N (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 ;}@ Overridepublic int hashCode () {final int prime = 31; int result = 1; result = prime * result + (id = null )? 0: id. hashCode (); return result ;}@ Overridepublic 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 ;}}


The key lies in the FieldHandled interface and the lob field getter.


Hibernate delayed Loading

Hibernate. initialize (stu. getCourses ());
Before closing the SESSION. The filter is actually before the SESSION.

You can open another SESSION and query the course according to the course set.

Hibernate problems delayed Loading Problems

Configure the OpenSessionInView filter in web. xml to solve the problem.

<! -- OpenSessionInView configuration to solve the problem of delayed loading of Hibernate -->
<Filter>
<Filter-name> lazyLoadingFilter </filter-name>
<Filter-class> org. springframework. orm. hibernate3.support. OpenSessionInViewFilter </filter-class>
</Filter>

<Filter-mapping>
<Filter-name> lazyLoadingFilter </filter-name>
<Url-pattern> *. do </url-pattern>
</Filter-mapping>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.