CGLIB如何代理String或其他final 类?

How does CGLIB proxy String or other final classes?

我想问一个关于 Hibernate 中延迟获取的问题。

据我所知,为了实现 Lazy Fetching,Hibernate 创建了一个占位符,它是真正 属性.

的代理。

如果我的实体包含字符串 属性 或其他最终 class 怎么办? CGLIB怎么会subclass呢?

长话短说:

  1. CGLib 根本无法代理最终 classes,您之前可能在日志中看到过类似 Could not generate CGLIB subclass of class [class SomeClass]: Common causes of this problem include using a final class or a non-visible class
  2. 的内容
  3. Hibernate 首先代理您的实体 class,将相应的 attribute Interceptors 注入到各自的 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 而不是 属性

结束这个短语