Java Hibernate 代理对象包含什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25340606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:09:22  来源:igfitidea点击:

What does the Hibernate proxy object contain?

javahibernateormproxyhibernate-mapping

提问by vineeshchauhan

All I could gather from Google is that:

我只能从谷歌收集到的是:

  • Hibernate uses a proxy object to implement lazy loading. When we request to load the Object from the database, and the fetched Object has a reference to another concrete object, Hibernate returns a proxy instead of the concrete associated object.

  • Hibernate creates a proxy object using bytecode instrumentation (provided by javassist). Hibernate creates a subclass of our entity class at runtime using the code generation library and replaces the actual object with the newly created proxy.

  • Hibernate 使用代理对象来实现延迟加载。当我们请求从数据库中加载对象,并且获取的对象引用了另一个具体对象时,Hibernate 返回一个代理而不是具体的关联对象。

  • Hibernate 使用字节码检测(由 javassist 提供)创建代理对象。Hibernate 在运行时使用代码生成库创建我们实体类的子类,并用新创建的代理替换实际对象。

So, what exactly does the Proxy object contain?

那么,Proxy 对象究竟包含什么?

Does it contain a skeleton object reference object with only the id field set? Others field will be set when we call the get method?

它是否包含仅具有 id 字段集的骨架对象引用对象?调用get方法时会设置others字段吗?

Does the Proxy object contain the JDBC statement to fetch all data required to fully populate the referenced object.

代理对象是否包含 JDBC 语句以获取完全填充引用对象所需的所有数据。

Is there something else I might be missing?

还有什么我可能会遗漏的吗?

I am not asking for spoon feeding but if you can provide any link with information that would be great.

我不是要求用勺子喂食,但如果您能提供任何包含信息的链接,那就太好了。

Any correction to above description will also be welcomed.

对上述描述的任何更正也将受到欢迎。

Example.

例子。

class Address {
   String city;
   String country;
}

class Person{
   int id;
   String name;
   Address address;
}    

When we try to load the Person object, Hibernate will subclass Person class like:

当我们尝试加载 Person 对象时,Hibernate 会子类化 Person 类,例如:

class ProxyPerson extends Person {
       int id;
       String name;
       Address proxyCGLIBObject;
}

and return a ProxyPerson object. Object of ProxyPerson will have a value for id and name but proxy for Address.

并返回一个 ProxyPerson 对象。ProxyPerson 的对象将有一个 id 和 name 的值,但有一个地址的代理。

Am I correct?

我对么?

What can I expect from adding a toString() method on the proxy object?

在代理对象上添加 toString() 方法可以期待什么?

采纳答案by Vlad Mihalcea

This is a very common question, so this answer is based on this articleon my blog.

这是一个很常见的问题,所以这个答案是基于我博客上的这篇文章

The Hibernate Proxy is used to substitute an actual entity POJO (Plain Old Java Object).

Hibernate 代理用于替换实际实体 POJO(Plain Old Java Object)。

The Proxy class is generated at runtime and it extends the original entity class.

Proxy 类是在运行时生成的,它扩展了原始实体类。

Hibernate uses Proxy objects for entities is for to allow lazy loading.

Hibernate 为实体使用 Proxy 对象是为了允许延迟加载

When accessing basic properties on the Proxy, it simply delegates the call to the original entity.

在访问代理上的基本属性时,它只是将调用委托给原始实体。

Every List, Set, Maptype in the entity class is substituted by a PersistentList, PersistentSet, PersistentMap. These classes are responsible for intercepting a call to an uninitialized collection.

实体类中的每个List, Set,Map类型都替换为PersistentList, PersistentSet, PersistentMap。这些类负责拦截对未初始化集合的调用。

The Proxy doesn't issue any SQL statement. It simply triggers an InitializeCollectionEvent, which is handled by the associated listener, that knows which initialization query to issue (depends on the configured fetch plan).

代理不发出任何 SQL 语句。它只是触发一个InitializeCollectionEvent,它由关联的侦听器处理,它知道要发出哪个初始化查询(取决于配置的获取计划)。