jpa中onetomany两个相同list懒加载

本文介绍了在JPA中如何实现懒加载,特别是针对同一类型的两个不同属性的onetomany关系。通过设置特定的控制器参数,配合DAO查询时的EntityGraph,以及@NamedEntityGraph和@Where注解,可以有效地控制不同list的懒加载行为。

1,懒加载使用

1,controller
String[] lazyFieldNames=new String[] {“casInfoGroup”,“edu”};
map.put(JPAUtil.JPA_ENTITY_LZAYLOAD_FIELDS, lazyFieldNames);
放入list查询前面
2,dao查询前
String[] lazyFieldNames=(String[])paramMap.get(JPAUtil.JPA_ENTITY_LZAYLOAD_FIELDS);
if(lazyFieldNames!=null&&lazyFieldNames.length>0) {
EntityGraph graph = em.createEntityGraph(AppUCASInfo.class);
for(String fieldName:lazyFieldNames) {
graph.addSubgraph(fieldName);
}
listQ.setHint(“javax.persistence.loadgraph”, graph);
}

	List<AppUCASInfo> list = listQ.getResultList();

如果说需要使用query自带查询,使用如下视图

1, @Entity
@Table(name = “app_ucas_info”)
@NamedEntityGraphs({
@NamedEntityGraph(name=“negPart”, attributeNodes={
@NamedAttributeNode(“edu”),
@NamedAttributeNode(“users”)
}),
@NamedEntityGraph(name=“negFull”, attributeNodes={
@NamedAttributeNode(“casInfoGroup”),
@NamedAttributeNode(“edu”),
@NamedAttributeNode(“users”)
})
})

2,@Query("select f from “+ENTITY_NAME+” f where f.valid =0 and f.name like ‘%学院%’ " )
@EntityGraph(value = “negPart”, type = EntityGraph.EntityGraphType.LOAD)
List queryZkyPartProperty();

如果有列A与列B属于同类,单数属性不同得list,可以用如下方法

@OneToMany(targetEntity = FileUpload.class, mappedBy = “review”, fetch = FetchType.LAZY)
@Where(clause = "filetype = 5 ")
public Set getApprovalfiles() {
return approvalfiles;
}

public void setApprovalfiles(Set<FileUpload> approvalfiles) {
	this.approvalfiles = approvalfiles;
}

@OneToMany(targetEntity = FileUpload.class, mappedBy = "review", fetch = FetchType.LAZY)
@Where(clause = "filetype = 6 ")
public Set<FileUpload> getConcludingfiles() {
	return concludingfiles;
}

public void setConcludingfiles(Set<FileUpload> concludingfiles) {
	this.concludingfiles = concludingfiles;
}

使用set而不是list

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值