我想问一个有关 Hibernate 中延迟获取的问题。
据我所知,为了实现延迟获取,Hibernate 创建了一个占位符,它是真实属性的代理。
如果我的实体包含 String 属性或其他最终类怎么办? CGLIB 将如何子类化它?
最佳答案
长话短说:
- CGLib 根本无法代理最终类,您之前可能在日志中看到过类似的内容
无法生成类 [class SomeClass] 的 CGLIB 子类:此问题的常见原因包括使用最终类或非最终类可见类
- Hibernate 首先代理您的实体类,具有相应的 attribute Interceptors注入(inject)到各自的 getter 中,因此实际的调用堆栈通常如下所示:
myEntity.getMyString() |_ proxy.getMyString() |_ lazyAttributeLoadingInterceptor.fetchAttribute(myEntity,"myString") |_ ... (actual call to underlying DB if required)
也就是说,您在这里所说的一切都是正确的:
Hibernate creates a placeholder that is a proxy of the real ...
如果您以单词 entity/pojo 而不是 property 结尾
关于java - CGLIB如何代理String或其他final类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51953547/