2 Replies Latest reply on Mar 2, 2007 2:32 AM by Henning Eiben

    Computed Columns in Entities

    Henning Eiben Newbie


      I would like to include a computed column in my entity. I have two tables Order and Orderitems, so I want to have a property in my Order entity that contains a COUNT(Orderitems.*).

      Is that possible? If yes, how? If now, what would be a possible solution?

        • 1. Re: Computed Columns in Entities
          Jan Lessner Newbie

          The only portable way I know is by queries which return both, the actual entity and the computed value as two different result elements, e.g.

          EntityManager em;

          Query q = em.createQuery("SELECT o, COUNT(o.positions) FROM OrderEntity o ...");

          List<Object[]> results = q.getResultList();

          for (Object[] result: results) {
          OrderEntity order = (OrderEntity)result[0];
          //... do something with your order

          This is of course somehow selfmade but maybe it helps :-)


          • 2. Re: Computed Columns in Entities
            Henning Eiben Newbie

            Hmm, I was afraid someone would suggest something like this.

            Well I could also do something like

            public int getItemsCount()
            public float getItemTotal()
             float retVal = 0;
             for (Orderitem currentOrder : this.getOrderitems())
             retVal += currentOrder.getQuantity() . currentOrder.getItem().getPrice();
             return retVal;

            but I would consider this as sub-optimal, because doing this kind of computation could also be done using the database, and I would say that it would be a little bit faster ...