I am developing application using spring and hibernate. I am using lazy loading. My hierarchy of layer is controller>>Service>>Converter>>Repository, where converter task is to convert the entity into VO. Suppose I have two entity as below: Parent class as:
@Entity
@Table(name = "Parent")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Parent {
private Integer id;
private List<Childrens> childrens;
@Override
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@Column( name="Parent_ID", unique = true, nullable = false, updatable = false )
public Integer getId() { return id; }
private void setId(Integer id){this.id=id};
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL,CascadeType.PERSIST,CascadeType.MERGE }, mappedBy = "parent")
@Column(nullable = false)
public List<Children> getChildrens() { return childrens; }
public void setChildrens( List<Children> childrens) { this.childrens = childrens; }
}
And child class as:
@Entity
@Table(name = "Children")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Children {
private Integer id;
private Parent parent;
@Override
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@Column( name="Parent_ID", unique = true, nullable = false, updatable = false )
public Integer getId() { return id; }
private void setId(Integer id){this.id=id};
@ManyToOne( fetch = FetchType.EAGER )
@JoinColumn(name = "parent_id", nullable = false )
public Parent getParent() { return parent; }
public void setParent(Parent parent) {
this.parent = parent;
}
}
Now I have ParentVO as:
public class ParentVO {
private Integer id;
private List<ChildrenVO> childrensVO;
public Integer getId() { return id; }
private void setId(Integer id){this.id=id};
public List<ChildrenVO> getChildrensVO() { return childrensVO; }
public void setChildrensVO( List<ChildrenVO> childrensVO) { this.childrensVO = childrensVO; }
}
And ChildVO as:
public class ChildrenVO {
private Integer id;
private ParentVO parentVO;
public Integer getId() { return id; }
public void setId(Integer id){this.id=id;}
public ParentVO getParentVO() { return parentVO; }
public void setParentVO(ParentVO parentVO) {
this.parentVO = parentVO;
}
}
Whenever I request service for Parent class, it first retrives service from dao and converts Parent to ParentVO as:
public class ParentConverter {
public ParentVO getParentVOFromParent(Parent parent){
ParentVO parentVO=new ParentVO();
ChildrenConverter childrenConverter=new ChildrenConverter();
parentVO.setChildrens(childrenConverter.getChildrensVOList(parent.getChildrens()));
return parentVO;
}
}
Here, parentVO.setChildrens(childrenConverter.getChildrensVOList(parent.getChildrens())); calls list of childrens which makes it no longer lazy.
And ChildrenConverter as:
public class ChildrenConverter {
public ChildrenVO getChildrenVOFromChildren(Children children){
ChildrenVO childrenVO=new ChildrenVO();
childrenVO.setId(children.getId());
return childrenVO;
}
public List<ChildrenVO> getChildrensVOList(List<Children> childrens){
List<ChildrenVO> childrenVOs=new ArrayList<ChildrenVO>();
for(Children child: childrens){
childrenVOs.add(getChildrenVOFromChildren(child));
}
return childrenVOs;
}
}
In controller I am only use VO objects so, what I want is :
When I request ParentVO from service, it must not convert Children List into ChildrenVO List untill and unless I call ParentVO's getChildrensVO() method.
Now, it will preserve hiberate lazy loading feature. Is there any to acheive this feature.
Aucun commentaire:
Enregistrer un commentaire