我在加载不包含实时链接列表的Match信息时尝试使用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]