简体   繁体   中英

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

I tried to use LAZY LOADING when loading information of Match without list of live links. But the result always fetch all links in Match.

My question is: Is it possible to use LAZY LOAD for One-to-many relationship in Hibernate 5? If we can, please help me to check my code.

Thank you very much!

I have following code:

@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;
}

My Query function:

@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();
    }

My TRACE log:

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]

Yes, of-course we can use, lazy loading in @OneToMany relationship.

Just do this

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

More info OnetoOne and OneToMany

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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