代理如何在Hibernate / JPA中加载懒惰属性 [英] How proxy loads the lazy property in Hibernate/JPA

查看:67
本文介绍了代理如何在Hibernate / JPA中加载懒惰属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我的疑问很简单:为了获得最佳性能,建议在属性中始终使用惰性初始化(我不需要使用)(这很明显)。所以,想象下面的类:

  @Entity 
public class Person {
@Column
私人字符串名称;
@ManyToOne(fetch = FetchType.Lazy)
@JoinColumn(name =id_type)
私人TypePerson类型;



$ b $ p
$ b

在我的主类中,我调用没有初始化type属性的Person,像下面这样:

  public void init(){
//在这一点上,type属性没有初始化
Person person = dao.find(Select * from Person where id = 12);

// proxys在这里工作以获得对type属性的描述
System.out.println(person.getType()。getDescription());





$ b因此,我从dataBase中获得一个简单的Person对象,并在控制台上打印出人的类型。在这一刻,代理CGLIB的作品,并做你的魔术,并且一切正常。



但我在这里提出我的问题:

< getType()Hibernate(或其他机制)在幕后做了一个SQL查询?像:SELECT * FROM TypePerson where id = 3。



如果答案是肯定的:这种获取属性值的方法会非常痛苦,因为我认为Hibernate每次都会在数据库中获取如果答案是否定的:如果代理没有从dataBase加载,代理如何知道属性的值?

/ p>

解决方案

当您致电

  person.getType()。getDescription()

if person 引用了一个尚未初始化的Hibernate代理,然后,它会发出一个SQL查询来检索目标实体的字段值。
$ b


获取属性值的这种方法可能非常痛苦,因为i
认为Hibernate每次都在数据库中去获取这个信息
在幕后。


每个代理只会执行一次。当它第一次出现时,它会在代理上设置一个标志,表明它已被初始化,因此潜在的目标实体具有正确的值。如果加载了所有值,则不需要返回数据库。



这不完全是Hibernate创建其代理的方式,但它是一个很好的读取:代理模式


Well, my doubt is very simple: For best performance is recommended that use always lazy initialization in property that I don't need to use (this is obvious). So, imagine the following class:

  @Entity
  public class Person{
    @Column
    private String name;
    @ManyToOne(fetch = FetchType.Lazy)
    @JoinColumn(name = "id_type")
    private TypePerson type;
 }

In my main class I call the Person with "type" attribute not initialized, like bellow:

 public void init(){
    //in this point "type" attribute is not initialized
    Person person = dao.find("Select * from Person where id = 12");

    //proxys work here to get description of "type" attribute
    System.out.println(person.getType().getDescription());
  }

So, I get a simple Person object from dataBase, and print on console the type of person. In this moment Proxy CGLIB works and do your magic, and everything works fine.

But here I go my question:

1 - When I request the "getType()" the Hibernate (or another mechanism) make a SQL Query behind the scenes ? Like: "SELECT * FROM TypePerson where id = 3".

If answer is yes: This method to get value of a property can be very painful, because I think Hibernate goes everytime in database to get this information behind the scenes.

If answer is no: How Proxy know the value of property if this wasn't loaded from dataBase ?

解决方案

When you call

person.getType().getDescription()

if person is referencing a Hibernate proxy that hasn't been initialized, then, yes, it will issue an SQL query to retrieve the target entity's field values.

This method to get value of a property can be very painful, because i think Hibernate goes everytime in database to get this information behind the scenes.

It will only go once for each proxy. When it goes the first time, it will set a flag on the proxy that indicates that it is initialized and therefore the underlying target entity has the right value. You don't need to go back to the database if it has all its values loaded.

This is not exactly how Hibernate creates its proxies, but it is a good read: Proxy Pattern.

这篇关于代理如何在Hibernate / JPA中加载懒惰属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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