我们可以在休眠 5 中的一对多关系上使用延迟加载吗?



我尝试在没有实时链接列表的情况下加载匹配信息时使用延迟加载。但结果总是获取匹配中的所有链接。

我的问题是: 是否可以在 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();
}

我的跟踪日志:

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;

更多信息 一对一和一对一对多

最新更新