Grails:检查分离的对象是否在附加的集合中

发布于 2024-10-04 10:00:38 字数 1469 浏览 0 评论 0原文

我的应用程序的会话包含一个用户对象,该对象具有一个设置对象,其中包含属性“ effectiveOrganization”。设置对象会立即加载,并且由于 Hibernate Session 是针对每个请求的,因此会话中的用户对象与 Hibernate Session 分离。

我想检查“有效组织”是否在附加对象的集合中:

<g:if test="${session.user.settings.effectiveOrganisation in
    documentInstance.downloadingOrganisations}">

但此测试的结果始终为假。也许这是因为会话中的组织和 documentInstance 的组织不是相同的对象。我在 Organization 类中实现了 equalshashCode 但没有帮助。

我在控制器中尝试了以下测试:

    def org = session.user.settings.effectiveOrganisation
    doc.downloadingOrganisations.each{
        if(it.equals(org))
            println("equals works")
    }
    if(! doc.downloadingOrganisations.contains(org))
        println("contains doesn't work")

令人惊讶的结果是:

equals works
contains doesn't work

equalshashCode 如下所示:

boolean equals(o) {
    if (this.is(o)) return true;
    if (getClass() != o.class) return false;
    Organisation that = (Organisation) o;
    if (name != that.name) return false;
    if (selfInspecting != that.selfInspecting) return false;
    return true;
}

int hashCode() {
    int result;
    result = (name != null ? name.hashCode() : 0);
    result = 31 * result + (selfInspecting != null ? selfInspecting.hashCode() : 0);
    return result;
}

How can I check a object from the session is contains in the set附加物体的?

The session of my application contains a user objects which has a settings objects which contains an attribute "effectiveOrganisation". The settings objects is loaded eagerly and since the Hibernate Session is per request, the user object in the session is detached from the Hibernate Session.

I want to check wheter the "effectiveOrganisation" is in the Set of an attached object:

<g:if test="${session.user.settings.effectiveOrganisation in
    documentInstance.downloadingOrganisations}">

But the result of this test is always false. Maybe this is because the organisation in the session and the organisation of the documentInstance are not identical objects. I implemented equals and hashCode in the Organisation class but it didn't help.

I tried the following test in a controller:

    def org = session.user.settings.effectiveOrganisation
    doc.downloadingOrganisations.each{
        if(it.equals(org))
            println("equals works")
    }
    if(! doc.downloadingOrganisations.contains(org))
        println("contains doesn't work")

The surprising result is:

equals works
contains doesn't work

equals and hashCode looks as follows:

boolean equals(o) {
    if (this.is(o)) return true;
    if (getClass() != o.class) return false;
    Organisation that = (Organisation) o;
    if (name != that.name) return false;
    if (selfInspecting != that.selfInspecting) return false;
    return true;
}

int hashCode() {
    int result;
    result = (name != null ? name.hashCode() : 0);
    result = 31 * result + (selfInspecting != null ? selfInspecting.hashCode() : 0);
    return result;
}

How can I check wheter an object from the session is contained in the set of an attached object?

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

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

发布评论

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

评论(2

此刻的回忆 2024-10-11 10:00:39

看来您的哈希码计算可能是问题所在。哈希码的计算通常比等于要便宜得多,因此首先进行比较。如果发生冲突并且两个不同的对象生成相同的哈希码,则检查 equals()。但是,如果两个对象具有不同的哈希码,则根据哈希码/等于约定,它们被假定为不同的对象。

集合中的实例是代理 - 这会影响哈希码计算吗?

It looks like your hashcode computation is probably the issue. Hashcode is usually a lot cheaper to calculate than equals, so it's compared first. If there's a collision and two different objects generate the same hashcode, then equals() is checked. But if two objects have different hashcodes then according to the hashcode/equals contract they are assumed to be different objects.

The instances in the collection are proxies - is that affecting the hashcode calculation?

花间憩 2024-10-11 10:00:39

检查实例的类。哈希码可能不是问题,但对象很可能是休眠代理,这才是问题所在。

检查 equals() 是否在 contains() 期间被调用

另外,将此 g:if 更改为

g:if test="${session.user.settings.id in
      documentInstance.downloadingOrganisations*.id}"

可能会修复它。

Check the class of the instances. The hash code is probably not the issue, but the objects are most likely hibernate proxies which is the issue.

Check if equals() is being called during contains()

Also, changing this g:if to

g:if test="${session.user.settings.id in
      documentInstance.downloadingOrganisations*.id}"

May fix it.

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