关联对象的 Hibernate 标准
我有 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
(
),具有以下限制:
MainDTO.disabled = false、
MainDTO.root != null、
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"));
非常感谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,调用实体DTO确实令人困惑。
现在关于您的问题,您应该使用您在投影中定义的别名
此外,考虑到您正在对 root 和 child 进行内部联接,最后两个限制是不必要的:内部联接已经确保它们不为空。
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.