[英]Why am I not seeing hibernate proxy objects in my ManyToOne associations?
According the the hibernate docs , my @ManyToOne
relationships should have proxy objects by default. 根据休眠文档 ,默认情况下,我的@ManyToOne
关系应该具有代理对象。 However, when I look at an expanded object in the Eclipse debugger ("Variables" view), it looks like the field variables are instances of the base type as defined in the entity class. 但是,当我在Eclipse调试器(“变量”视图)中查看扩展对象时,看起来字段变量是实体类中定义的基本类型的实例。 Moreover, when I call session.get(type, id)
with hibernate.show_sql=true
I can see left outer join
s for all @ManyToOne
relationships defined on the object. 此外,当我使用hibernate.show_sql=true
调用session.get(type, id)
,我可以看到对象上定义的所有@ManyToOne
关系的left outer join
@ManyToOne
。
Is there something specific that needs done in order for Hibernate to create proxy classes/objects for these relationships? 为了让Hibernate为这些关系创建代理类/对象,是否需要做一些特定的事情? Perhaps bytecode enhancement? 也许字节码增强?
Quote from hibernate docs: 引用休眠文档:
Lazy fetching for collections is implemented using Hibernate's own implementation of persistent collections. 使用Hibernate自己的持久性集合实现来实现集合的懒惰获取。 However, a different mechanism is needed for lazy behavior in single-ended associations. 但是,单端关联中的惰性行为需要不同的机制。 The target entity of the association must be proxied. 协会的目标实体必须被代理。 Hibernate implements lazy initializing proxies for persistent objects using runtime bytecode enhancement which is accessed via the bytecode provider. Hibernate使用运行时字节码增强功能(通过字节码提供程序访问)为持久对象实现延迟初始化代理。
At startup, Hibernate generates proxies by default for all persistent classes and uses them to enable lazy fetching of many-to-one and one-to-one associations. 在启动时,默认情况下,Hibernate会为所有持久类生成代理,并使用它们来实现多对一和一对一关联的延迟获取。
All @ManyToOne
and @OneToOne
associations are EAGER by default , that's why they are JOINED when you fetch the root entity. 默认情况下 ,所有@ManyToOne
和@OneToOne
关联都为EAGER ,这就是为什么当您获取根实体时将它们关联在一起的原因。
A proxy is used only if the association is not initialized. 仅当关联未初始化时才使用代理。 To make your associations LAZY, you just need to add the LAZY fetch attribute: 要使您的关联为LAZY,您只需添加LAZY fetch属性:
@ManyToOne(fetch = FetchType.LAZY)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.