简体   繁体   English

什么时候需要休眠运行时代理生成?

[英]When do I need hibernate runtime proxy generation?

The hibernate documentation says: "the constructor should be defined with at least package visibility if you wish to leverage runtime proxy generation". 休眠文档说:“如果希望利用运行时代理生成,则构造函数至少应具有程序包可见性”。 I've read in the hibernate documentation, that hibernate can enhance bytecode instead of proxy creation(Hibernate 5.x). 我在hibernate文档中已经读到,hibernate可以增强字节码而不是创建代理(Hibernate 5.x)。 It can substitute the proxy creation with this new method in every cases? 在每种情况下都可以用这种新方法替代代理创建吗? In which cases do I need runtime proxy generation? 在哪些情况下需要运行时代理生成?

Runtime enhancement is the default, it is used for lazy loading support. 运行时增强是默认设置,用于延迟加载支持。 If you want to use lazy loading on a @OneToOne or @ManyToOne association then the class which will be lazily loaded should have a protected constructor or else Hibernate will throw an exception when you attempt to fetch the parent entity. 如果要在@OneToOne@ManyToOne关联上使用延迟加载,则将延迟加载的类应具有受保护的构造函数,否则,当您尝试获取父实体时,Hibernate将抛出异常。 So for example: 因此,例如:

@Entity
public class Parent {

    @OneToOne(fetch=FetchType.LAZY)
    private Child child;

}

In this example the Child class must have a constructor with protected or greater visibility. 在此示例中, Child类必须具有具有受保护的或具有更高可见性的构造函数。

With Hibernate 5.0 you can do bytecode enhancement. 使用Hibernate 5.0,您可以进行字节码增强。 This is an extra compilation step, so you will need to place it in your Maven or Ant (or whatever) build process. 这是一个额外的编译步骤,因此您需要将其放入Maven或Ant(或任何其他)构建过程中。 With bytecode enhancement you should not need to no-arg constructor but if you don't include it your application will not be adhering to the JPA spec (only important if you plan on switching to some provider other than Hibernate). 通过字节码增强,您不需要no-arg构造函数,但是如果不包含它,则您的应用程序将不遵循JPA规范(仅在计划切换到Hibernate之外的其他提供程序时才重要)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM