Hi,
Since the begin of my project I notice difficulties with my ER model that hibernate is querying ToOne relationships even if I explicity mark them lazy. Here is a bit of code how it is currently done:
@ManyToOne(fetch = LAZY, optional = false)
@LazyToOne(NO_PROXY)
@JoinColumn(name = "audit_tran_id")
public AuditTransaction getAuditTransaction() {
return auditTransaction;
}
the classes are bytecode-enhanced by the maven plugin:
<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<version>${hibernate.version}</version>
<executions>
<execution>
<configuration>
<enableLazyInitialization>true</enableLazyInitialization>
</configuration>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
and when my SpringBoot app is started it logs that, too
HHH000157: Lazy property fetching available for: [...]
Still, I notice unnecessary queries during the JPA load, in this case because the reference was broken, I got a nice stack trace:
javax.persistence.EntityNotFoundException: Unable to find mypackage.AuditTransaction with id 3133020786
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$JpaEntityNotFoundDelegate.handleEntityNotFound(EntityManagerFactoryBuilderImpl.java:162)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:211)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:246)
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:105)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1260)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1143)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:687)
at org.hibernate.type.EntityType.resolve(EntityType.java:464)
at org.hibernate.type.ManyToOneType.resolve(ManyToOneType.java:239)
at org.hibernate.type.EntityType.resolve(EntityType.java:457)
at org.hibernate.type.EntityType.nullSafeGet(EntityType.java:272)
at org.hibernate.persister.entity.AbstractEntityPersister.initializeLazyPropertiesFromDatastore(AbstractEntityPersister.java:1210)
at org.hibernate.persister.entity.AbstractEntityPersister.initializeLazyProperty(AbstractEntityPersister.java:1121)
at org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor$1.doWork(LazyAttributeLoadingInterceptor.java:105)
at org.hibernate.bytecode.enhance.spi.interceptor.Helper.performWork(Helper.java:97)
at org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor.loadAttribute(LazyAttributeLoadingInterceptor.java:76)
at org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor.fetchAttribute(LazyAttributeLoadingInterceptor.java:72)
at org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor.intercept(LazyAttributeLoadingInterceptor.java:61)
at org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor.readObject(LazyAttributeLoadingInterceptor.java:296)
at mypackage.Achieved.$$_hibernate_read_version(Achieved.java) ~[classes!/:1-SNAPSHOT]
at mypackage.Achieved.getVersion(Achieved.java:272) ~[classes!/:1-SNAPSHOT]
at mypackage.Service.lambda$checkForDuplicateAchievement$2(Service.java:97) ~[classes!/:1-SNAPSHOT]
That brings up my question, did I oversee anything to get this a true lazy relation (because the audit information is only used when inserting),
And secondly, is there a JPA way to handle the situation gently when there is a referenced entity missing?