欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jpa中onetomany两个相同list懒加载

程序员文章站 2022-04-24 14:32:07
...

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

相关标签: 实际问题 jpa