这是
this问题的延续.
我有一个Address类,其中包含基本的街道地址信息.我还有一个User类,它具有physicalAddress,mailingAddress,cargoDestinations和cargoSources属性. User类看起来像这样:
class User { String username String password String firstName String lastName String businessName String phoneNumber Address physicalAddress Address mailingAddress static hasMany = [accounts:Account,cargoSources:Address,cargoDestinations:Address,cargoes:Cargo,loadsLogged:Load,loadsDelivered:Load] Set accounts,cargoSources,cargoDestinations,cargoes static mappedBy = [loadsLogged:"loggedBy",loadsDelivered:"deliveredBy"] //some other stuff after this
Address类看起来像这样:
class Address { static belongsTo = [user:User] String streetAddress String city String state String zip BigDecimal taxRate //some other stuff after this
我大部分时间都遵循了教程here.在第5步中,我的模板如下所示:
<g:select from="${account.user.cargoDestinations}" name="cargoDestinations" value=""> </g:select>
问题是,模板不是仅返回cargoDestinations,而是返回与该用户关联的所有地址.如果我从=“${account.user.cargoDestinations}”更改为=“${account.user.physicalAddress}”或者=“${account.user.mailingAddress}”,我会得到预期的结果,所以我知道我的问题与cargoDestinations变量的映射方式有关.如何在不更改类文件的情况下修复此问题?
解决方法
映射地址的方式,它们都链接回user_id列上的用户.您需要向Address添加一些字段以区分它们与User的关联方式,类似于您映射Loads的方式.例如:
class Address { static belongsTo = [cargoSourceFor: User,cargoDestinationFor: User] ... } class User { ... static hasMany = [cargoSources:Address,cargoDestinations:Address] static mappedBy = [cargoSources: "cargoSourceFor",cargoDestinations: "cargoDestinationFor"] ... }
如果您熟悉SQL,那么在设置映射时执行grails schema-export并查看target / ddl.sql会很有帮助.