Hibernate代理对象包含什么?

时间:2022-05-30 22:49:00

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使用代理对象来实现延迟加载。当我们请求从数据库加载Object,并且获取的Object具有对另一个具体对象的引用时,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.

    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方法时会设置其他字段吗?

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

Proxy对象是否包含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的值,但是Address的代理。

Am I correct?

我对么?

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

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

1 个解决方案

#1


37  

As explained in my book, High-Performance Java Persistence, the Hibernate Proxy is used to substitute an actual entity POJO (Plain Old Java Object).

正如我的书“高性能Java持久性”中所解释的那样,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使用实体的代理对象来允许延迟加载。

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

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

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.

实体类中的每个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,它由关联的侦听器处理,它知道要发出哪个初始化查询(取决于配置的获取计划)。

#1


37  

As explained in my book, High-Performance Java Persistence, the Hibernate Proxy is used to substitute an actual entity POJO (Plain Old Java Object).

正如我的书“高性能Java持久性”中所解释的那样,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使用实体的代理对象来允许延迟加载。

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

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

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.

实体类中的每个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,它由关联的侦听器处理,它知道要发出哪个初始化查询(取决于配置的获取计划)。