Grails 多对多 - 动态查找器的问题

发布于 2024-11-27 01:38:24 字数 1243 浏览 1 评论 0原文

我希望你们能帮助我。不幸的是,Google 没有帮助我,我在 stackoverflow 上的搜索也没有帮助:-(

我有两个 DomainClass HumanResourceTask ,具有多对多关系

模型定义:

任务:

class Tasks {

    String name

    static belongsTo = [HumanResource]
    static hasMany = [humanResources: HumanResource]
    //also tried but didn't help -> static fetchMode = [humanResources:"eager"]
}

HumanResource

class HumanResource {

    String name

    static hasMany = [tasks: Tasks]

}

我还尝试使用mapping={}在id字段上添加索引,但我也认为这不是解决方案,它没有帮助,我认为 id 字段上已经有一个索引,

所以,我现在所做的和没有做的是找到给定任务的所有人力资源,并且任务来自服务并且它们已经被获取。带有“static fetchMode = [tasks:"eager"]”的服务模型

def listHumanResourcesFromTasks = {
        def list = HumanResource.findAllByTasks(service.getTasks())

        //and I tried also with an own HashMap but didn't work as well

}

我总是收到错误“org.springframework.dao.InvalidDataAccessResourceUsageException” SQL-GrammarException。但我真的不知道为什么“service.getTasks()”对象已完全填充(正如我用 fetchMode = [tasks:"eager"] 编写的那样)...

如果有人可以的话那就太棒了。给我获胜的提示。

非常感谢您抽出时间。

最美好的祝愿,

马可

I hope you can help me guys. Google unfortunately didn't helps me out and my search here at stackoverflow didn't as well :-(

I have two DomainClasses HumanResource and Task with a many-to-many relationship.

Model-Definitions:

Task:

class Tasks {

    String name

    static belongsTo = [HumanResource]
    static hasMany = [humanResources: HumanResource]
    //also tried but didn't help -> static fetchMode = [humanResources:"eager"]
}

HumanResource:

class HumanResource {

    String name

    static hasMany = [tasks: Tasks]

}

I also tried to add an index on the id-field with mapping={} but I also think that's not the solution, it didn't help and I think there is already an index on the id-field.

So, what I did and not works is now to find all human resources for the given tasks! And the tasks comes from Services and they are already fetched in the service model with "static fetchMode = [tasks:"eager"]"!

Controller-Code:

def listHumanResourcesFromTasks = {
        def list = HumanResource.findAllByTasks(service.getTasks())

        //and I tried also with an own HashMap but didn't work as well

}

I always get an error "org.springframework.dao.InvalidDataAccessResourceUsageException" with an SQL-GrammarException. But I really don't know why. The "service.getTasks()" objects are fully filled (as I wrote with fetchMode = [tasks:"eager"])...

It would be awesome if somebody could give me the winning hint.

Thanks a lot for your time.

Best wishes,

Marco

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

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

发布评论

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

评论(1

淡紫姑娘! 2024-12-04 01:38:24

不支持此类查询 - 通常您需要使用 HQL 或条件查询。但这个特殊的关系很容易,因为你们有双向关系。您可以通过以下方式获取 Tasks 集合的所有 HumanResource 实例:

def resources = service.getTasks().collect { it.humanResources }.flatten() as Set

它需要是一个 Set,因为相同的 HumanResource 实例可能会出现多次,因此您需要将列表压缩为唯一的实例。

This sort of query isn't supported - you'd need to use HQL or a criteria query in general. But this particular one is easy since you have a bidirectional relationship. You can get all of the HumanResource instances for a collection of Tasks with this:

def resources = service.getTasks().collect { it.humanResources }.flatten() as Set

It needs to be a Set since the same HumanResource instance may appear multiple times so you need to condense the List into unique instances.

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