Hibernate 代理对象包含什么? [英] What does the Hibernate proxy object contain?

查看:31
本文介绍了Hibernate 代理对象包含什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只能从 Google 那里了解到:

All I could gather from Google is that:

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

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

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.

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

So, what exactly does the Proxy object contain?

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

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?

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

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

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

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.

示例.

class Address {
   String city;
   String country;
}

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

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

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

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

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

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

我说得对吗?

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

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

推荐答案

Hibernate Proxy 用于替代实际实体 POJO(Plain Old Java Object).

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

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

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

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

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

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

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

实体类中的每个ListSetMap类型被一个PersistentList替代>PersistentSetPersistentMap.这些类负责拦截对未初始化集合的调用.

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

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

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).

这篇关于Hibernate 代理对象包含什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆