官术网_书友最值得收藏!

  • Mastering Hibernate
  • Ramin Rad
  • 350字
  • 2021-07-16 10:54:41

Proxy objects

If you are familiar with Java reflection, you have heard of the java.lang.reflect.Proxy class. In a nutshell, you can wrap any object with a proxy and intercept calls to methods of that object using an invocation handler. Many Java frameworks use proxy objects, or manipulate bytecode (also called instrumentation) to modify the behavior of an object. Hibernate uses both ways for different purposes. More importantly, Hibernate implements its own set of wrapper classes for collection types. (Refer to classes in org.hibernate.collection.internal.)

If you fetch an entity, Hibernate doesn't fetch the associated collections if they are marked as lazy fetch. Instead, it waits until you actually try to access the associated collection. As soon as you access an entity in the associated collection, Hibernate will fetch the associated entities from the persistence store and will then populate the wrapped collection for you to access. Hibernate accomplishes this using the internal collection wrappers. You can actually examine this yourself by writing a simple check, as follows:

parent = (Parent) session.get(Parent.class, new Long(1));
Set<Child> children = parent.getChildren();
if (children instanceof PersistentSet) {
 System.out.println("**** Not java.util.Set");
}

// PersistentSet is located in org.hibernate.collection.internal 

Hibernate uses byte code manipulation techniques to initialize an object that is uninitialized. This usually occurs when your entity has an associated entity, for example, a Person entity that is associated with an Address entity. When the root entity, in this case Person, is loaded from the database, the Address object is not initialized in case of LAZY loading. In such cases, Hibernate returns a manipulated version of the associated entity, and as soon as you try to access any of the attributes of the associated entity, for example, address.getStreet(), Hibernate will hit the database to fetch the values for the associated entity and initialize it.

Hibernate also returns a proxy object when you ask for an entity using the load method instead of the get method of the Session class.

Note

The byte code manipulation is achieved using the Javassist library.

When working with Hibernate, it is important that you keep in mind how Hibernate uses proxy objects.

主站蜘蛛池模板: 郎溪县| 稷山县| 雅安市| 古浪县| 娄烦县| 沙河市| 永年县| 隆化县| 吉木萨尔县| 宝坻区| 波密县| 德保县| 彰武县| 贵州省| 遂昌县| 青海省| 达州市| 宣城市| 德惠市| 五指山市| 临清市| 萨嘎县| 溧阳市| 潼南县| 丹阳市| 绵竹市| 巫山县| 星座| 双鸭山市| 贵定县| 抚宁县| 陵水| 榆社县| 于田县| 买车| 观塘区| 喀什市| 赤城县| 丰台区| 深水埗区| 临潭县|