Olá a todos!
Há pouco eu postei um problema sobre o FetchType.LAZY não estar funcionando numa relacionamento @ManyToOne quando eu chamava a classe relacionada. Bom, recomecei o projeto e tudo estava funcionando perfeitamente, até eu chegar no Interceptor. Acontece que ao adicionar o interceptador mesmo sem função nenhuma e ao registrá-lo no WebConfig, o FetchType.LAZY para de funcionar e somente colcando FetchType.EAGER é que eu consigo acessar os valores da classe relacionada.
Porém não quero gerar consultas excessivas com o FetchType.EAGER, quero que ele faça a consulta somente quando eu realmente acessar a classe selecionada.
Resumindo: Se eu remover o Interceptador ou deixar FetchType.EAGER, funciona, mas preciso do interceptador e do Lazy.
E aí, o que vocês me sugerem pra resolver isso?
Classe relacionada que preciso acessar os atributos:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(foreignKey = @ForeignKey(name="profiles_fkey"))
private Profile profile;
Erro:
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1021E: A problem occurred whilst attempting to access the property 'name': 'Unable to access property 'name' through getter method'
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:209)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:51)
at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:406)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:92)
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:112)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:337)
at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:265)
... 67 more
Caused by: org.springframework.expression.AccessException: Unable to access property 'name' through getter method
at org.springframework.expression.spel.support.ReflectivePropertyAccessor$OptimalPropertyAccessor.read(ReflectivePropertyAccessor.java:708)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:204)
... 74 more
Parte 2 do erro:
Caused by: org.springframework.expression.AccessException: Unable to access property 'name' through getter method
at org.springframework.expression.spel.support.ReflectivePropertyAccessor$OptimalPropertyAccessor.read(ReflectivePropertyAccessor.java:708)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:204)
... 74 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.expression.spel.support.ReflectivePropertyAccessor$OptimalPropertyAccessor.read(ReflectivePropertyAccessor.java:704)
... 75 more
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy [br.com.occhi.annotations.model.Profile#1] - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:170)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:310)
at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45)
at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95)
at br.com.occhi.annotations.model.Profile$HibernateProxy$O6Dxgz9e.getName(Unknown Source)
... 80 more