繁体   English   中英

我们可以在Hibernate 5的一对多关系中使用LAZY LOADING吗?

[英]Can we use LAZY LOADING on One-to-many Relationship in Hibernate 5?

我在加载不包含实时链接列表的比赛信息时尝试使用LAZY LOADING。 但是结果总是获取Match中的所有链接。

我的问题是:是否可以在Hibernate 5中将LAZY LOAD用于一对多关系? 如果可以的话,请帮助我检查我的代码。

非常感谢你!

我有以下代码:

@Entity
@Table(name = "match_info")
public class MatchInfoModel extends AuditModel {

    @Id
    @Column(name = "MATCH_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long matchId;

    @Column(name = "STADIUM_NAME")
    private String stadiumName;

    @Column(name = "START_TIME")
    private Date startTime;

    @OneToMany(mappedBy = "matchInfoModel", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    private Set<LiveStreamModel> liveStreamsSet;    
}


@Entity
@Table(name = "live_stream")
public class LiveStreamModel extends AuditModel {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "LIVE_LINK_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long liveLinkId;

    @Column(name = "LINK")
    private String link;

    @Column(name = "SPEED")
    private String speed;

    @Column(name = "ORDER_NUMBER")
    private int orderNumber;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "MATCH_ID")
    @JsonProperty(access = Access.WRITE_ONLY)
    private MatchInfoModel matchInfoModel;
}

我的查询功能:

@Override
    public List<MatchInfoModel> getLiveMatches() throws Exception {

        Session session = getSession();
        CriteriaBuilder criteria = session.getCriteriaBuilder();
        CriteriaQuery<MatchInfoModel> criteriaQuery = criteria.createQuery(MatchInfoModel.class);
        Root<MatchInfoModel> matchInfoRoot = criteriaQuery.from(MatchInfoModel.class);
        criteriaQuery.select(matchInfoRoot);

        // Order by start time
        criteriaQuery.orderBy(criteria.asc(matchInfoRoot.get("startTime")));
        return session.createQuery(criteriaQuery).getResultList();
    }

我的TRACE日志:

2018-08-13 10:00:24 DEBUG org.hibernate.SQL - 
    select
        matchinfom0_.match_id as match_id1_4_,
        matchinfom0_.creat_date as creat_da2_4_,
        matchinfom0_.create_user as create_u3_4_,
        matchinfom0_.last_modified as last_mod4_4_,
        matchinfom0_.modified_user as modified5_4_,
        matchinfom0_.guest_team_id as guest_t10_4_,
        matchinfom0_.has_live_stream as has_live6_4_,
        matchinfom0_.host_team_id as host_te11_4_,
        matchinfom0_.league_name as league_n7_4_,
        matchinfom0_.stadium_name as stadium_8_4_,
        matchinfom0_.start_time as start_ti9_4_ 
    from
        match_info matchinfom0_ 
    order by
        matchinfom0_.start_time asc
2018-08-13 10:00:25 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [1]
2018-08-13 10:00:25 DEBUG org.hibernate.SQL - 
    select
        livestream0_.match_id as match_i12_3_0_,
        livestream0_.live_link_id as live_lin1_3_0_,
        livestream0_.live_link_id as live_lin1_3_1_,
        livestream0_.creat_date as creat_da2_3_1_,
        livestream0_.create_user as create_u3_3_1_,
        livestream0_.last_modified as last_mod4_3_1_,
        livestream0_.modified_user as modified5_3_1_,
        livestream0_.language as language6_3_1_,
        livestream0_.link as link7_3_1_,
        livestream0_.link_type as link_typ8_3_1_,
        livestream0_.match_id as match_i12_3_1_,
        livestream0_.order_number as order_nu9_3_1_,
        livestream0_.speed as speed10_3_1_,
        livestream0_.status as status11_3_1_ 
    from
        live_stream livestream0_ 
    where
        livestream0_.match_id=?
2018-08-13 10:00:25 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [7]
2018-08-13 10:00:25 DEBUG org.hibernate.SQL - 
    select
        livestream0_.match_id as match_i12_3_0_,
        livestream0_.live_link_id as live_lin1_3_0_,
        livestream0_.live_link_id as live_lin1_3_1_,
        livestream0_.creat_date as creat_da2_3_1_,
        livestream0_.create_user as create_u3_3_1_,
        livestream0_.last_modified as last_mod4_3_1_,
        livestream0_.modified_user as modified5_3_1_,
        livestream0_.language as language6_3_1_,
        livestream0_.link as link7_3_1_,
        livestream0_.link_type as link_typ8_3_1_,
        livestream0_.match_id as match_i12_3_1_,
        livestream0_.order_number as order_nu9_3_1_,
        livestream0_.speed as speed10_3_1_,
        livestream0_.status as status11_3_1_ 
    from
        live_stream livestream0_ 
    where
        livestream0_.match_id=?
2018-08-13 10:00:25 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [6]

是的,我们当然可以使用@OneToMany关系中的延迟加载。

做这个

@OneToMany(fetch = FetchType.LAZY)
@MapsId
private Set<LiveStreamModel> liveStreamsSet;

更多信息OnetoOneOneToMany

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM