2 Replies Latest reply on Jan 26, 2006 11:33 AM by darren hartford

    How to create an POJO View made of other POJOs

    dornus Novice

      Let's say I have 1 POJO called PojoA.

      I want to make a view (called MyPojoView) which contains the POJO plus some additional fields.

      In MySql, when I create the view I have to select every column from the POJO as well as the additional fields I want.

      In MyPojoView I'd rather reference MyPojo instead of all the fields that comprise MyPojo.

      Is there a way to achieve this? I don't know how to reference an existing POJO as a primary key. What do I have to do with the @Id field to get it to work?

      Something like

      package par.view;
      
      import javax.persistence.Entity;
      import javax.persistence.Table;
      import javax.persistence.Id;
      
      @Entity
      @Table(name = "my_view")
      public class MyPojoView {
      
       private MyPojo myPojo;
       private int additionalFieldOne;
      
       @Id
       public MyPojo getMyPojo() {
       return myPojo;
       }
      
       public void setMyPojo(MyPojo myPojo) {
       this.myPojo = myPojo;
       }
      
       public int getAdditionalFieldOne() {
       return additionalFieldOne;
       }
      
       public void setAdditionalFieldOne(int additionalFieldOne){
       this.additionalFieldOne = additionalFieldOne;
       }
      }
      
      


      Also, should my MySql view only select the primary key of the MyPojo class, or should it select all columns used to create a MyPojo object?


      Thanks!

        • 1. Re: How to create an POJO View made of other POJOs
          dornus Novice

          Another question I have is how efficient is it to embed a POJO within a view as opposed to having a variable for each field within the POJO.

          For example, if I am going through a loop of results

           List<MyPojoView> myResultList = ...;
           for (MyPojoView myPojo : myResultList) {
           myPojo.getPojo.getSomeField();
           //etc..get a bunch of fields this way
           }
          

          am I executing a new query each time getPojo.getSomeField() is called?

          If myPojoView was set up so that the fields were directly in that class, then it would be
           List<MyPojoView> myResultList = ...;
           for (MyPojoView myPojo : myResultList) {
           myPojo.getSomeField();
           //etc..get a bunch of fields this way
           }
          


          Which would not have any additional queries.

          So, I'd assume it would be more efficient to place all variables of MyPojo in the MyPojoView class instead of adding myPojo to MyPojoView.

          Any comments? I'm curious to see what others think


          • 2. Re: How to create an POJO View made of other POJOs
            darren hartford Expert

            I'm also interested in this question from the 'Business Delegate' point of view.

            i.e. Build an application that has 'Business Delegate' POJO's, but the application talks to diverse database schemas that may contain the same information.