2
我有DTO的下一個結構對象:休眠標準assotiated對象
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;
...
}
我需要填寫Map<String, Map<Long, Long>>
(<name from MainDTO, <mainId from MainDTO, infoId from InfoDTO >>
)與下一個限制:
MainDTO.disabled =假,
MainDTO.root!= null,
MainDTO.root.disabled = false,
MainDTO.child.disabled =假,
MainDTO.child.info!= NULL,
MainDTO.child.info.disabled =假
這裏是我寫的代碼,但從InfoDTO中選擇infoId:
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"));
非常感謝!
非常感謝!也許你可以幫我解決另一個問題:當我期望Object [3]將它轉換爲我的Map時,這個rootCriteria.list()返回下一個結構:Object [Object [3]]。也許我應該添加一些組聲明? – ninja
criteria.list()方法返回一個列表。列表中的每個元素都是由查詢返回的行,用對象數組表示。每行有三列。 criteria.list()方法應該返回一個List
現在它返回列表