1

Using Spring running in Tomcat and Hibernate 5.0

Trying to lazy load a single column, which doesn't seem to be supported without bytecode enhancement. I've attempted following these steps but the column is still being loaded as the initial query.

@Bean( JpaConfig.EMF )
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
final DataSource dataSource,
final JpaVendorAdapter jpaVendorAdapter )
....
props.put( AvailableSettings.ENHANCER_ENABLE_LAZY_INITIALIZATION, Boolean.toString( true ) );
emf.setJpaPropertyMap( props );

Column Config

@Column( name = "file_data", nullable = false )
@Basic( fetch = FetchType.LAZY )
    private byte[] fileData;

Spring Configuration class

@Configuration
@EnableLoadTimeWeaving

Startup Output

[INFO ] [17:22:56] [localhost-startStop-1] weaving.DefaultContextLoadTimeWeaver:76 - Determined server-specific load-time weaver: org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver
Jun 12, 2018 5:22:56 PM org.apache.catalina.loader.WebappClassLoaderBase addTransformer
INFO: Added class file transformer [org.springframework.context.weaving.AspectJWeavingEnabler$AspectJClassBypassingClassFileTransformer@6086a542] to web application [ROOT].
[INFO ] [17:22:56] [localhost-startStop-1] weaving.DefaultContextLoadTimeWeaver:76 - Determined server-specific load-time weaver: org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver
[INFO ] [17:27:05] [localhost-startStop-1] jpa.LocalContainerEntityManagerFactoryBean:356 - Building JPA container EntityManagerFactory for persistence unit 'persistenceUnit'
Jun 12, 2018 5:27:20 PM org.apache.catalina.loader.WebappClassLoaderBase addTransformer
INFO: Added class file transformer [Standard ClassFileTransformer wrapping JPA transformer: org.hibernate.jpa.internal.enhance.EnhancingClassTransformerImpl@20635e8] to web application [ROOT].

Am I missing something?

1 Answer 1

1

According to JPA specifications directive to load lazily is only a hint to the persistence provider, Hibernate, in your case. The provider may not respect your hint. In general, what is your objective? You don't loose much in fetching additional field of basic type, as there is no object graph involved. If you have large array, add @Lob annotation.

Sign up to request clarification or add additional context in comments.

4 Comments

The array is large, I'm not sure what @Lob gains me, it doesn't appear to affect the Laziness. The fileData is always populated, but we only want to load the data when someone wants to view the contents of the file. We pull a Collection of these Objects to present to the user, then if they wish to view the contents they click and that's when we want data to load.
What is the type of filedata on your database side?
It's a BLOB, so I can see how the @Lob annotation is appropriate. I just don't know how it helps with my need of Lazy loading.
If it's BLOB they all Hibernate will fetch is location_id, not the data itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.