简体   繁体   English

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

[英]Grails Many-To-Many - Problems of dynamic finder

I hope you can help me guys. 我希望你能帮助我。 Google unfortunately didn't helps me out and my search here at stackoverflow didn't as well :-( 不幸的是,谷歌并没有帮助我,我在stackoverflow上的搜索也没有:-(

I have two DomainClasses HumanResource and Task with a many-to-many relationship. 我有两个具有多对多关系的DomainClasses HumanResourceTask

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. 我还尝试使用mapping = {}在id字段上添加索引,但我也认为这不是解决方案,它没有帮助,我认为id字段上已经有索引。

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"]"! 任务来自服务,并且已经通过“ 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. 我总是收到带有SQL-GrammarException的错误“ org.springframework.dao.InvalidDataAccessResourceUsageException ”。 But I really don't know why. 但是我真的不知道为什么。 The "service.getTasks()" objects are fully filled (as I wrote with fetchMode = [tasks:"eager"])... “ service.getTasks()”对象已满(如我用fetchMode = [tasks:“ eager”]所写)...

It would be awesome if somebody could give me the winning hint. 如果有人能给我胜利的提示,那将是非常棒的。

Thanks a lot for your time. 非常感谢您的宝贵时间。

Best wishes, 最好的祝愿,

Marco 马可

This sort of query isn't supported - you'd need to use HQL or a criteria query in general. 不支持这种查询-通常,您需要使用HQL或条件查询。 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: 你可以得到所有的HumanResource实例为集合Tasks与此:

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. 它必须是一个Set因为同一个HumanResource实例可能会出现多次,因此您需要将List压缩为唯一的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM