0

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=?
4

1 回答 1

1

您是否 100% 确定没有任何东西会触发延迟加载StandardDimension?有时它是由一个过于贪婪的 equals 或 hashcode 实现触发的。

如果没有,您是否可以尝试使用 Criteria API 进行查询,看看是否有任何不同?

于 2013-09-27T01:32:43.587 回答