tags:

views:

13

answers:

1

What is the equivalant way to configure lazy="true" in hibernate3.?

While fetching object i will fetch the associated object using fetch concept.So in mapping itself i need to specify it.

A: 

I'm not sure I understood the question but if you're looking for an equivalent using annotations, then the ManyToOne annotation admits a fetch attribute. From the JPA 1.0 specification:

9.1.22 ManyToOne Annotation

The ManyToOne annotation defines a single-valued association to another entity class that has many-to-one multiplicity. It is not normally necessary to specify the target entity explicitly since it can usually be inferred from the type of the object being referenced.

Table 15 lists the annotation elements that may be specified for a ManyToOne annotation and their default values.

The cascade element specifies the set of cascadable operations that are propagated to the associated entity. The operations that are cascadable are defined by the CascadeType enum:

public enum CascadeType { ALL, PERSIST, MERGE, REMOVE, REFRESH}; 

The value cascade=ALL is equivalent to cascade={PERSIST, MERGE, REMOVE, REFRESH}.

@Target({METHOD, FIELD}) @Retention(RUNTIME)
public @interface ManyToOne {
  Class targetEntity() default void.class;
  CascadeType[] cascade() default {};
  FetchType fetch() default EAGER;
  boolean optional() default true;
}

The EAGER strategy is a requirement on the persistence provider runtime that the associated entity must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime that the associated entity should be fetched lazily when it is first accessed. The implementation is permitted to eagerly fetch associations for which the LAZY strategy hint has been specified.

So you can do:

@ManyToOne(fetch=FetchType.LAZY)
Foo foo

And with Hibernate's XML mappings, the association would be lazy by default.

References

  • JPA 1.0 specification
    • Section 9.1.22 "ManyToOne Annotation"
  • Hibernate Core documentation
Pascal Thivent