【发布时间】:2011-08-08 16:04:56
【问题描述】:
这是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,而是返回与该用户关联的所有地址。如果我将from="${account.user.cargoDestinations}" 更改为from="${account.user.physicalAddress}" 或from="${account.user.mailingAddress}",我会得到预期的结果,所以我知道我的问题与cargoDestinations 变量的映射方式有关。如何在不过多更改类文件的情况下解决此问题?
【问题讨论】:
-
几个问题:1)
User和Address关系是双向的吗? 2) 如果是这样,您的User域上是否有mappedBy? ...不确定是否会导致这种情况,但值得一看。 -
@Rob 我更新了我的问题,提供了有关我的课程的更多详细信息。
标签: grails groovy grails-orm