关联对象的 Hibernate 标准

发布于 2024-12-09 08:27:26 字数 2404 浏览 0 评论 0 原文

我有 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"));

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖哒 2024-12-16 08:27:26

首先,调用实体DTO确实令人困惑。

现在关于您的问题,您应该使用您在投影中定义的别名

ProjectionList rootProjection = 
    Projections.projectionList()
               .add(Projections.property("mainId"))
               .add(Projections.property("name"))
               .add(Projections.property("info.infoId"));

此外,考虑到您正在对 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

ProjectionList rootProjection = 
    Projections.projectionList()
               .add(Projections.property("mainId"))
               .add(Projections.property("name"))
               .add(Projections.property("info.infoId"));

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文