Hibernate:急切获取指定的字段
Hibernate: Eager Fetch Only Specified Fields
有没有办法指定在 hibernate/JPA @ManyToOne - FetchType.EAGER 中急切获取的唯一字段?
类似于:
@ManyToOne(fetch=FetchType.EAGER, eagerFields={"id","name"})
private Company company;
JPA 2.0 规范未定义您尝试实现的可能性,因为 @ManyToOne
注释定义如下(参见 section 11.1.26 of the JPA 2.0 specification):
public @interface ManyToOne {
Class targetEntity() default void.class;
CascadeType[] cascade() default {};
FetchType fetch() default EAGER;
boolean optional() default true;
}
如果您为要延迟加载的 Company
实体的字段指定 @Basic(fetch = FetchType.LAZY)
,则可以实现仅对选定字段进行预加载; 但是 对于关联字段以外的字段不鼓励这样做,因为它取决于提供程序实现的仁慈,如以下摘录所示:
The EAGER strategy is a requirement on the persistence provider runtime that data must be eagerly
fetched. The LAZY strategy is a hint to the persistence provider runtime that data should be fetched
lazily when it is first accessed. The implementation is permitted to eagerly fetch data for which the
LAZY strategy hint has been specified. In particular, lazy fetching might only be available for Basic
mappings for which property-based access is used.
有没有办法指定在 hibernate/JPA @ManyToOne - FetchType.EAGER 中急切获取的唯一字段?
类似于:
@ManyToOne(fetch=FetchType.EAGER, eagerFields={"id","name"})
private Company company;
JPA 2.0 规范未定义您尝试实现的可能性,因为 @ManyToOne
注释定义如下(参见 section 11.1.26 of the JPA 2.0 specification):
public @interface ManyToOne {
Class targetEntity() default void.class;
CascadeType[] cascade() default {};
FetchType fetch() default EAGER;
boolean optional() default true;
}
如果您为要延迟加载的 Company
实体的字段指定 @Basic(fetch = FetchType.LAZY)
,则可以实现仅对选定字段进行预加载; 但是 对于关联字段以外的字段不鼓励这样做,因为它取决于提供程序实现的仁慈,如以下摘录所示:
The EAGER strategy is a requirement on the persistence provider runtime that data must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed. The implementation is permitted to eagerly fetch data for which the LAZY strategy hint has been specified. In particular, lazy fetching might only be available for Basic mappings for which property-based access is used.