tags:

views:

1087

answers:

3

Hello,

I use JPA (Hibernate) with Spring. When i want to lazy load a Stirng property i use this syntax:

@Lob
@Basic(fetch = FetchType.LAZY)
public String getHtmlSummary() {
    return htmlSummary;
}

But when i look at the sql that hibernate creates, it seems this property is not lazy loaded? I also use this class org.hibernate.tool.instrument.javassist.InstrumentTask in ANT script to instrument this property but it seems it does not work.

Please help me.

Khosro.

A: 

Lazy fetching only applies to references to other entities or collections of entities. It does not apply to values like String or int.

Mark Thornton
Not true, LOBs can be lazy-loaded. See Section 2.2.2.1 of the hibernate-annotations documentation: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#d0e342 -- specifically the 'detailedComment' property on that example, and the note below it.
Cowan
A: 

Hi, from the specification of JPA they say that even if you use annotate a property to be fetched lazily, this is not guaranteed to be applied, so the properties may or may not be loaded lazily (depends on the implementer of JPA), however if you specify that you should fetch them Eagerly then the JPA implementer must load them eagerly.

Bottom line: @Basic(fetch = FetchType.LAZY) may or may not work, depends on the JPA implementer.

Omar Al Kababji
I use Hibernate ,and i describe the problem here http://stackoverflow.com/questions/2112508/basicfetch-fetchtype-lazy-does-not-work/2112846#2112846 in Henning's answer
Khosro
A: 

Lazy Lob loading would require bytecode instrumentation to work properly, so it is not available by default in any JPA implementation I'm aware of.

Your best bet is to put the Lob into a separate entity, like HtmlSummary, and use a lazily loaded one-to-one association.

Henning
Would you please tell me how to instrument?I use Hibernate implementation of JPA.I also do instruction describe here but ,it does not works :http://docs.jboss.org/hibernate/stable/core/reference/en/html_single/#performance-fetching-lazyproperties
Khosro
@Khosro: What does the Ant log output say? "Does not work" is not quite enough information to diagnose your problem, I'm afraid.
Henning
Well,my Ant task works well ,and the output is [instrument] accepting transformation of field access [News.textBody]but after running apps i got this exception org.hibernate.MappingException: Could not determine type for: org.hibernate.repackage.cglib.transform.impl.InterceptFieldCallback, at table: News, for columns: [org.hibernate.mapping.Column(interceptFieldCallback)]in this case i use org.hibernate.tool.instrument.cglib.InstrumentTask for instrumenting.In another case when i use org.hibernate.tool.instrument.javassist.InstrumentTask ,apps deploys but lazy loading does not works
Khosro
"does not works " means that ,when i fetch News from database ,it also fetches textBody,but according to lazy loading it must not fetched.
Khosro
@Khosro:Is org.hibernate.repackage.cglib.transform.impl.InterceptFieldCallback on your classpath at runtime?
Henning
Henning ,Yes,this class is in hibernate3.jar that i place it in classpath.Also i use JPA(Hibernate implementation)
Khosro
And i think if this class in not in my classpath i must get this exception " java.lang.NoClassDefFoundError" not this exception Could not determine type for: org.hibernate.repackage.cglib.transform.impl.InterceptFieldCallback, at table: News, for columns: [org.hibernate.mapping.Column(interceptFieldCallback)
Khosro