Getting the ID of a one-to-many loaded object with

2019-02-16 22:48发布

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.

1条回答
Bombasti
2楼-- · 2019-02-16 23:39

Instead of:

Long bid = a.b.id

use:

Long bid = a.bId

查看更多
登录 后发表回答