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?

xenoterracide
  • 16,274
  • 24
  • 118
  • 243
Evan L
  • 11
  • 4

1 Answers1

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.

fg78nc
  • 4,774
  • 3
  • 19
  • 32
  • 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. – Evan L Jun 12 '18 at 21:39
  • What is the type of `filedata` on your database side? – fg78nc Jun 12 '18 at 21:50
  • 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. – Evan L Jun 12 '18 at 22:05
  • If it's BLOB they all Hibernate will fetch is location_id, not the data itself. – fg78nc Jun 13 '18 at 00:32