1
votes

According the the hibernate docs, my @ManyToOne relationships should have proxy objects by default. 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. Moreover, when I call session.get(type, id) with hibernate.show_sql=true I can see left outer joins for all @ManyToOne relationships defined on the object.

Is there something specific that needs done in order for Hibernate to create proxy classes/objects for these relationships? Perhaps bytecode enhancement?

Quote from hibernate docs:

Lazy fetching for collections is implemented using Hibernate's own implementation of persistent collections. 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.

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.

1
Make the association lazyJB Nizet
@JBNizet - thanks, how do i do that? Also, why do the docs say it should happen by default?jlb

1 Answers

2
votes

All @ManyToOne and @OneToOne associations are EAGER by default, that's why they are JOINED when you fetch the root entity.

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:

@ManyToOne(fetch = FetchType.LAZY)