我有DTO对象的下一个结构:
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 = false,
MainDTO.root!=空,
MainDTO.root.disabled = false,
MainDTO.child.disabled = false,
MainDTO.child.info!= null,
MainDTO.child.info.disabled = false
这是我编写的代码,但是卡在InfoDTO的select 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"));
非常感谢!
最佳答案
首先,调用实体DTO确实令人困惑。
现在关于您的问题,您应该使用在投影中定义的别名
ProjectionList rootProjection =
Projections.projectionList()
.add(Projections.property("mainId"))
.add(Projections.property("name"))
.add(Projections.property("info.infoId"));
另外,假设您正在对root和child进行内部联接,则后两个限制不是必需的:内部联接已经确保它们不为null。
关于java - 关联对象的 hibernate 标准,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7715691/