I have following two entities with bidirectional relationship @OneToMany and @ManyToOne. When I query child table with join query as shown below, hibernate automatically query StandardDimension (Parent) table for each MinuteData (Child table) to load @ManyToOne relationship even though it is marked as lazy loading. This is causing DB performance issue. Can you please help me resolve this issue so for each MinuteData it will not load standard dimension record ? I am using JPA 2.0 and hibernate-core-4.1.1.Final. Any help will be greatly appreciated. How can stop hibernate from loading StandardDimension objects ?
@Query("SELECT md FROM StandardDimension sd ,MinuteData md where sd.dimensionAK = md.dimensionFK and sd.app = :applicationId and md.timeBucket between :startTime and :endTime ")
public List<MinuteData> findMinuteDataByTimeBucket( @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("applicationId") String applicationId);
@Entity (name="StandardDimension")
@Table (name="STANDARD_DIMENSION")
public class StandardDimension implements Serializable {
@OneToMany (targetEntity=MinuteData.class, fetch=FetchType.LAZY, mappedBy="dimensionFK", cascade=CascadeType.REMOVE)
private Set<MinuteData> muniteDateDimensionViaFK = new HashSet<MinuteData>();
}
@Entity (name="MinuteData")
@Table (name="MINUTE_DATA")
public class MinuteData implements Serializable {
........
@ManyToOne (fetch=FetchType.LAZY)
@JoinColumn(name="DIMENSION_FK", referencedColumnName = "DIMENSION_AK", nullable=false , unique=false , insertable=true, updatable=true)
private StandardDimension dimensionFK;
.........
}
Here is SQL Statement form console:
Hibernate:
select
minutedata1_.minute_data_pk as minute1_1_,
minutedata1_.created_date as created2_1_,
minutedata1_.data_obj as data3_1_,
minutedata1_.dimension_fk as dimension6_1_,
minutedata1_.modified_date as modified4_1_,
minutedata1_.time_bucket as time5_1_
from
standard_dimension standarddi0_,
minute_data minutedata1_
where
standarddi0_.dimension_ak=minutedata1_.dimension_fk
and standarddi0_.app=?
and (
minutedata1_.time_bucket between ? and ?
)
Hibernate:
select
standarddi0_.standard_dimensions_pk as standard1_2_0_,
standarddi0_.app as app2_0_,
standarddi0_.created_date as created3_2_0_,
standarddi0_.data_center as data4_2_0_,
standarddi0_.dimension_ak as dimension5_2_0_,
standarddi0_.env as env2_0_,
standarddi0_.last_seen as last7_2_0_,
standarddi0_.modified_date as modified8_2_0_,
standarddi0_.server as server2_0_,
standarddi0_.app_version as app10_2_0_
from
standard_dimension standarddi0_
where
standarddi0_.dimension_ak=?