获取一对多加载对象的ID,而无需使用GORM再次访问数据库 [英] Getting the ID of a one-to-many loaded object without another trip to the DB with GORM

查看:143
本文介绍了获取一对多加载对象的ID,而无需使用GORM再次访问数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须去GORM域名,A& B,与数据库表有关。 A与B有一对多的关系。正因为如此,这些类看起来类似于:

  class A {
b b
Long id
}

class B {
Long id
}

当我检索A的一个实例时,从数据库中检索到B的相应实例的ID。但是,当我试图通过类似的方式访问该ID时:

  A a = A.get(11)
Long bid = abid

整个对象从数据库加载。在某些情况下,我只想要B(已经被检索到)的ID,并且不想从数据库加载整个实例。有没有办法从B获取ID而无需返回并获取整个对象。



注意:我知道它正在对 abid ,因为我可以在我的DataSource文件中打开 loggingSql 选项后看到生成的SQL。

解决方案

而不是:


= abid

使用:


Long bid = a.bId



I have to GORM domains, A & B, that relate to database tables. A has a one-to-many relationship with B. Because of this, the classes look similar to:

class A {
   B b
   Long id
}

class B {
   Long id
}

When I retrieve an instance of A the ID of the corresponding instance of B is retrieved from the database. However, when I attempt to access that ID via something like:

A a = A.get(11)
Long bid = a.b.id

the whole object is loaded from the database. In some cases I only want the ID of B (which has already been retrieved) and do not want to load the whole instance from the database. Is there a way to get the ID from B without going back and fetching the whole object.

NOTE: I know that it is doing an extra fetch on the line with a.b.id above because I can see the SQL being generated since I turned on the loggingSql option in my DataSource file.

解决方案

Instead of:

Long bid = a.b.id

use:

Long bid = a.bId

这篇关于获取一对多加载对象的ID,而无需使用GORM再次访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆