关于使用GORM加入具有附加条件的相关实体的误解

时间:2018-10-27 22:15:54

标签: join grails gorm

在我们的应用中,可以通过将deletedAt设置为删除日期来对所有实体进行软删除。

在其中一种情况下,我试图用一种关系查询对象,但仅包括“未删除”的对象。

我要查询的对象是Goal

class Goal extends AuditableEntity {

    String  title
    String  definition
    RrmGoalType goalType

    Project project

    static hasMany = [perceptions: Perception]

    static hasOne = [utilization: RrmUtilization]

    static mapping = {
        table 'rrm_goal'

        title column: 'title', type: 'text'
        definition type: 'text'
        goalType column: 'goal_type', enumType: 'string'

        perceptions sort: 'definition', order: 'asc'

        autoTimestamp true
    }

...
}

AuditableEntity在哪里:

abstract class AuditableEntity implements Serializable {

    Date dateCreated
    User createdBy
    Date lastUpdated
    User updatedBy
    Date deletedAt
    User deletedBy

    static constraints = {
        createdBy nullable: true
        updatedBy nullable: true
        deletedBy nullable: true
        dateCreated nullable: true
        lastUpdated nullable: true
        deletedAt nullable: true
    }
}

我需要查询是否附有perceptions

我要尝试使用的是namedQuery,如下所示:

notDeletedByProjectIdAndIdAndProjectOwnerId { Long projectId, Long goalId, Long userId ->
            isNull 'deletedAt'

            eq 'id', goalId

            createAlias('perceptions', 'perceptions', JoinType.LEFT_OUTER_JOIN)

            isNull 'perceptions.deletedAt'

            project {
                eq 'id', projectId
                isNull 'deletedAt'

                owner {
                    eq 'id', userId
                }
            }
        }

它执行,但是由于某种原因,它仅从感知列表中返回一个元素。

我想念什么?

一些进展

我认为我已经开始了解该机制。我正在这样获取目标:

Goal.notDeletedByProjectIdAndIdAndProjectOwnerId(projectId, goalId, userId).get()

gorm将结果集中的第一个结果归还给我,因此仅附加了一个知觉。

如何查询目标对象并附加所有未删除的感知(在单个查询中)?

从这种类型的查询中获取对象图的正确方法是什么?

另一个问题:是否可以将渴望获取配置为仅包含未删除的对象(或其他条件)?

0 个答案:

没有答案