1 Reply Latest reply on Dec 5, 2007 5:44 AM by Alex Grönholm

    How to retrieve foreign key instead of the object (want no l

    lpmon Novice

      Goal: ability to retrieve the foreign key without loading the related object/table row.

      Scenario:

      Entity bean contains a related entity bean.

      @Entity
      class Unit{ // Unit table

      // what kind of unit
      Model model; // related object in Model table, related column is modelId

      In the Unit table there is a column called modelId to point to the row in the Model table. If I want this key value I can use

      Unit u = .... somehow get it with EntityManager or whatever;

      int modelId = u.getModel().getId(); // causes read of Model table!

      Issue - this will cause a read of the model row from the DB (won't it, as in lazy loading will perform a JIT read)

      I only need the modelId which is in the Unit table. It seems there should be a way to do this and not load the related object.

      One can do this with EJBQL by using the reserved Class.id syntax. How can I take advantage of that in the ORM classes?

      I used a simple example. In my app if I can accomplish this I can easily avoid a lot of unwanted DB fetches.





        • 1. Re: How to retrieve foreign key instead of the object (want
          Alex Grönholm Novice

           


          int modelId = u.getModel().getId(); // causes read of Model table!

          Issue - this will cause a read of the model row from the DB (won't it, as in lazy loading will perform a JIT read)

          No it won't. You obviously haven't tried it, or you wouldn't be asking about it. The ID field of the association is special in the way that reading it will NOT trigger lazy loading. Any other operation on the lazy object, however, will.