I have the next structure of DTO objects:
public class MainDTO implements Serializable {
private Long mainId;
private String name;
... //some other fields
private boolean disabled;
@ManyToOne()
@JoinColumn(name = "root_id")
private RootDTO root;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "child_id", updatable = false, referencedColumnName = "child_id")
@Cascade(value = { CascadeType.ALL, CascadeType.DELETE_ORPHAN })
private ChildDTO child;
...
}
public class RootDTO implements Serializable {
... //some other fields
private boolean disabled;
...
}
public class ChildDTO implements Serializable {
private Long childId;
... //some other fields
private boolean disabled;
@ManyToOne()
@JoinColumn(name = "info_id")
private InfoDTO info;
...
}
public class InfoDTO implements Serializable {
private Long infoId;
... //some other fields
private boolean disabled;
...
}
I need to fill Map<String, Map<Long, Long>> (<name from MainDTO, <mainId from MainDTO, infoId from InfoDTO >>) with next restrictions:
MainDTO.disabled = false,
MainDTO.root != null,
MainDTO.root.disabled = false,
MainDTO.child.disabled = false,
MainDTO.child.info != null,
MainDTO.child.info.disabled = false
Here is the code I’ve written, but got stuck in select infoId from InfoDTO:
Criteria rootCriteria = getSession().createCriteria(
MainDTO.class);
// check that root is enabled
rootCriteria.createCriteria("root", "root").add(
Restrictions.eq("disabled", false));
// check that child is enabled
Criteria childCriteria = rootCriteria
.createCriteria("child", "child")
.add(Restrictions.eq("disabled", false))
.add(Restrictions.isNotNull("info"));
// check that info is enabled
childCriteria
.createCriteria("info", "info")
.add(Restrictions.eq("disabled", false));
ProjectionList rootProjection = Projections.projectionList()
.add(Projections.property("mainId"))
.add(Projections.property("name"))
.add(Projections.property("child.info")); //HOW TO SELECT ONLY ID??
rootCriteria.setProjection(rootProjection);
rootCriteria.add(Restrictions.eq("disabled", false))
.add(Restrictions.isNotNull("root"))
.add(Restrictions.isNotNull("child"));
Thanks a lot!
First of all, calling entities DTO is really confusing.
Now regarding your question, you should use the aliases you defined in your projections
Also, given that you’re doing an inner join to root and child, the last two restrictions are not necessary: the inner joins already make sure those are not null.