[英]Grails Many-To-Many - Problems of dynamic finder
我希望你能帮助我。 不幸的是,谷歌并没有帮助我,我在stackoverflow上的搜索也没有:-(
我有两个具有多对多关系的DomainClasses HumanResource和Task 。
型号定义:
任务 :
class Tasks {
String name
static belongsTo = [HumanResource]
static hasMany = [humanResources: HumanResource]
//also tried but didn't help -> static fetchMode = [humanResources:"eager"]
}
人力资源 :
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
}
我总是收到带有SQL-GrammarException的错误“ org.springframework.dao.InvalidDataAccessResourceUsageException ”。 但是我真的不知道为什么。 “ service.getTasks()”对象已满(如我用fetchMode = [tasks:“ eager”]所写)...
如果有人能给我胜利的提示,那将是非常棒的。
非常感谢您的宝贵时间。
最好的祝愿,
马可
不支持这种查询-通常,您需要使用HQL或条件查询。 但是,由于您具有双向关系,因此这一特定操作很容易。 你可以得到所有的HumanResource
实例为集合Tasks
与此:
def resources = service.getTasks().collect { it.humanResources }.flatten() as Set
它必须是一个Set
因为同一个HumanResource
实例可能会出现多次,因此您需要将List压缩为唯一的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.