jpa中onetomany两个相同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
上一篇: QPSK调制发送数据包
下一篇: ThreadLocal使用注意的地方