Grails select 将不会返回正确的数据
这是这个问题的延续。
我有一个 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
我按照教程 此处 大部分内容。在步骤 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
变量的映射方式有关。我怎样才能在不改变我的类文件太多的情况下解决这个问题?
This a continuation of this question.
I have an Address
class which contains basic street address information. I also have a User
class which has the attributes physicalAddress
, mailingAddress
, cargoDestinations
, and cargoSources
. The User
class looks something like this:
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
And the Address
class looks something like this:
class Address {
static belongsTo = [user:User]
String streetAddress
String city
String state
String zip
BigDecimal taxRate
//some other stuff after this
I followed the tutorial here for the most part. In step 5 my template looks like this:
<g:select
from="${account.user.cargoDestinations}"
name="cargoDestinations" value="">
</g:select>
The problem is that instead of returning only cargoDestinations
, the template returns ALL addresses associated with that user. If I change from="${account.user.cargoDestinations}"
to from="${account.user.physicalAddress}"
or from="${account.user.mailingAddress}"
I get the expected result, so I know my problem has something to do with how the cargoDestinations
variable is mapped. How can I go about fixing this without changing my class files too much?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您映射地址的方式,它们都链接回 user_id 列上的用户。您需要向地址添加一些字段,以区分它们与用户的关系,类似于映射负载的方式。例如:
如果您熟悉 SQL,那么在设置映射时执行
grails schema-export
并查看target/ddl.sql
会很有帮助。The way you have your addresses mapped, they all link back to the user on the user_id column. You'll need to add some fields to Address to distinguish how they're related to User, similar to how you've mapped Loads. For example:
If you're familiar with SQL, doing a
grails schema-export
and looking attarget/ddl.sql
can be helpful when setting up mappings.我最终在我的地址类中添加了几个布尔字段,这使得设计更简单并且更容易使用。这样我只需要一个类,而不是几个几乎相同的类。
Address
类中的布尔字段现在指示地址是否是物理地址、邮寄地址、货物来源等或以上全部。正如 @ataylor 指出的那样,该系统使得地址对象只能与我的User
类中的一个用户关联,但这似乎永远不会成为问题。最糟糕的情况是多个用户在现实生活中具有相同的地址,并且我的程序需要为每个用户创建一个单独的地址对象,即使所讨论的地址是相同的。I ended up adding several boolean fields to my address class, which made the design simpler and much easier to work with. This way I only need one class instead of several nearly identical classes. The boolean fields in the
Address
class now indicate if an address is a physical address, mailing address, cargo source, etc., or all of the above. As @ataylor pointed out, this system makes it so an address object can only be associated with one user from myUser
class, but it does not seem like that will ever be an issue. The worst case is that a multiple users would have the same address in real life, and my program would require the creation of a separate address object for each of those users, even though the addresses in question would be identical.