开发者

Grails Many-To-Many - Problems of dynamic finder

开发者 https://www.devze.com 2023-03-24 06:06 出处:网络
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 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:

cl开发者_StackOverflow社区ass 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号