Grails hasOne and hasMany same domain

痴心易碎 提交于 2019-12-19 04:56:16

问题


I have domain like this:

class Team {
 hasOne [leader: Person]
 hasMany [member: Person]
}

class Person {
 belongsTo [team: Team]
}

But when the tables are generated, there is not a column like leader_id in the team table. And thus the leader relation is not persisted.

How should I fix it?


回答1:


I figured that, what I need is

class Team {
 belongsTo [leader: Person]
 hasMany [member: Person]
}

class Person {
 belongsTo [team: Team]
}

so that the Team table can have the desired "leader" reference back to Person.




回答2:


Per the documentation:

Use a hasOne association to store the foreign key reference in child table instead of the parent in a bidirectional one-to-one.

You're child table here is Person and your parent is Team. Grails is working as expected.



来源:https://stackoverflow.com/questions/11640996/grails-hasone-and-hasmany-same-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!