13 Replies Latest reply: Oct 9, 2012 1:57 PM by 435209 RSS

    'Calculated field' returning null after row refresh.

    934805
      Hello,

      I am litle bit new in BC4J technolgy.

      I am facing problem with view object's calculated field.

      Whenever I use values from viewObject at first time, its showing me the correct values, but whenever I apply Row r.refresh(Row.REFRESH_WITH_DB_FORGET_CHANGES) function on its row, the fields that are calculated field, becomes null.

      Because of this issue, my application turn to NullPointerException. :(

      Here in program, Gender is a calculted field, rest are EO fields.

      public class Client_02

      {
      public Client_02()

      {
      super();
      }

      public static void main(String[] args) throws Exception

      {
      Hashtable env = new Hashtable(2);
      env.put(Context.INITIAL_CONTEXT_FACTORY, JboContext.JBO_CONTEXT_FACTORY);
      env.put(JboContext.DEPLOY_PLATFORM, JboContext.PLATFORM_LOCAL);
      Context context = new InitialContext(env);

      ApplicationModuleHome home = (ApplicationModuleHome)context.lookup("com.training.bc4j.AMEmployeeDepartment");

      AMEmployeeDepartment empDeptAM = (AMEmployeeDepartment)home.create();

      empDeptAM.getTransaction().connect("jdbc:oracle:thin:xyz/xyz@192.168.172.100:1521:nki");

      ViewObject vEmpDept = empDeptAM.findViewObject("VEmp");

      while (vEmpDept.hasNext()) {

      Row r = vEmpDept.next();
      System.out.println("1Emp Name = " + r.getAttribute("Name"));
      System.out.println("Emp Salary = " + r.getAttribute("Salary"));
      System.out.println("Dept Name = " + r.getAttribute("Name"));
      System.out.println("Gender = " + r.getAttribute("Gender"));


      r.refresh(Row.REFRESH_WITH_DB_FORGET_CHANGES); //here after refresh, Gender field becomes null as it is the only field which is 'Calculated'

      System.out.println("Emp Name = " + r.getAttribute("Name"));
      System.out.println("Emp Salary = " + r.getAttribute("Salary"));
      System.out.println("Dept Name = " + r.getAttribute("Name"));
      System.out.println("Gender = " + r.getAttribute("Gender")); //Null value returns here
      System.out.println("---------------");
      }
      }
      }



      Please refer it and suggest any solution so that it wont become null.



      ---

      Best Regards,

      Mukesh Gangurde.
        • 1. Re: 'Calculated field' returning null after row refresh.
          Frank Nimphius-Oracle
          Hi,

          try basing the transient view object attribute on a transient entity attribute. Note that refresh with db forget only sets the value back to the value originally read from the database. There is no new query issued and therefor transient attributes are not re-calculated. What you can try though is to set the transent attribute dependency to the attribute that it depends on so the attribute is refreshed when the dependent attribute changes.

          Frank
          • 2. Re: 'Calculated field' returning null after row refresh.
            934805
            Hi Frank,

            I have checked your above given solution. But its not working here..
            Its returning null after row.refresh() function for calculated values.
            • 3. Re: 'Calculated field' returning null after row refresh.
              Vinay Agarwal
              Can you try defaulting the value for the transient attribute? Did you add the transient attr from VO editor? The NPE should be coming from your own code I guess. Adding NPE check is another alternative.
              Another thing I observed is that AppModule should be tested/debugged with App Module tester and not with a main(). The AM instance is created by the framework.

              Edited by: Vinay Agarwal on May 7, 2012 1:05 AM
              • 4. Re: 'Calculated field' returning null after row refresh.
                934805
                yes, GENDER attribute is added in VO through query.
                query is as given below...

                SELECT Emp.DEPT_ID,
                Emp.EMP_ID,
                Emp.NAME,
                Emp.SALARY,
                Emp.GENDER
                FROM EMP Emp

                where GENDER is not an attribute of EO, but it is present in the table EMP.
                • 5. Re: 'Calculated field' returning null after row refresh.
                  Vinay Agarwal
                  And what is the reason for you not having made it an EO attribute even though it exists in DB table?
                  • 6. Re: 'Calculated field' returning null after row refresh.
                    Frank Nimphius-Oracle
                    .. .I would have the same question ? Note that an additional atribute in an EO doesn't burn fuel, so you don't save anything by not adding it

                    Frank
                    • 7. Re: 'Calculated field' returning null after row refresh.
                      934805
                      it is just because, such situation is here at my original application.
                      I have a VO which is having all attributes from EO, and 1 attribute from different table, it is only in query using combination of foreign key of VO.

                      at that location gets problem for that attribute after row.refresh().
                      • 8. Re: 'Calculated field' returning null after row refresh.
                        Vinay Agarwal
                        I dont really understand your case. If you can explain your case along with VO examples / FK detail you may get better suggestions.
                        • 9. Re: 'Calculated field' returning null after row refresh.
                          934805
                          here, I am giving details of EO, VO and DB table emp

                          table Emp
                          -------------
                          CREATE TABLE EMP
                          (
                          "EMP_ID" NUMBER,
                          "NAME" VARCHAR2(20 BYTE),
                          "SALARY" NUMBER,
                          "GENDER" VARCHAR2(10 BYTE),
                          )
                          ================================


                          EO Emp.xml
                          ------------------------------------------------------------------------
                          <?xml version="1.0" encoding="windows-1252" ?>
                          <!DOCTYPE Entity SYSTEM "jbo_03_01.dtd">
                          <!---->
                          <Entity
                          xmlns="http://xmlns.oracle.com/bc4j"
                          Name="Emp"
                          Version="11.1.2.60.17"
                          DBObjectType="TABLE"
                          DBObjectName="xyz.EMP"
                          AliasName="Emp"
                          BindingStyle="OracleName"
                          UseGlueCode="false">
                          <Attribute
                          Name="EmpId"
                          ColumnName="EMP_ID"
                          SQLType="NUMERIC"
                          Type="java.lang.Integer"
                          ColumnType="NUMBER"
                          TableName="xyz.EMP"
                          PrimaryKey="true"/>
                          <Attribute
                          Name="Name"
                          Precision="20"
                          ColumnName="NAME"
                          SQLType="VARCHAR"
                          Type="java.lang.String"
                          ColumnType="VARCHAR2"
                          TableName="NWSROOMPOT_INDIA.EMP">
                          <DesignTime>
                          <Attr Name="_DisplaySize" Value="20"/>
                          </DesignTime>
                          </Attribute>
                          <Attribute
                          Name="Salary"
                          ColumnName="SALARY"
                          SQLType="NUMERIC"
                          Type="java.lang.Integer"
                          ColumnType="NUMBER"
                          TableName="xyz.EMP"/>
                          <Key
                          Name="EmpPrimaryKey"
                          PrimaryKey="true">
                          <DesignTime>
                          <Attr Name="_DBObjectName" Value="EMP_PRIMARY_KEY"/>
                          </DesignTime>
                          <AttrArray Name="Attributes">
                          <Item Value="com.training.bc4j.Emp.EmpId"/>
                          </AttrArray>
                          </Key>
                          <Key
                          Name="SysC00170829">
                          <DesignTime>
                          <Attr Name="_referencedKey" Value="SYS_C00170828"/>
                          <Attr Name="_isForeign" Value="true"/>
                          <Attr Name="_DBObjectName" Value="SYS_C00170829"/>
                          </DesignTime>
                          <AttrArray Name="Attributes">
                          <Item Value="com.training.bc4j.Emp.DeptId"/>
                          </AttrArray>
                          </Key>
                          </Entity>

                          =================================




                          VO VEmp.xml
                          -----------------------------------------------------------------
                          <?xml version="1.0" encoding="windows-1252" ?>
                          <!DOCTYPE ViewObject SYSTEM "jbo_03_01.dtd">
                          <!---->
                          <ViewObject
                          xmlns="http://xmlns.oracle.com/bc4j"
                          Name="VEmp"
                          Version="11.1.2.60.17"
                          BindingStyle="OracleName"
                          CustomQuery="true"
                          PageIterMode="Full"
                          UseGlueCode="false">
                          <DesignTime>
                          <Attr Name="_isExpertMode" Value="true"/>
                          </DesignTime>
                          <Properties>
                          <SchemaBasedProperties>
                          <LABEL
                          ResId="com.training.bc4j.VEmp_LABEL"/>
                          </SchemaBasedProperties>
                          </Properties>
                          <ViewAccessor
                          Name="VEmp1"
                          ViewObjectName="com.training.bc4j.VEmp"
                          RowLevelBinds="true"/>
                          <SQLQuery><![CDATA[SELECT
                                 Emp.EMP_ID,
                                 Emp.NAME,
                                 Emp.SALARY,
                                 Emp.GENDER
                          FROM EMP Emp]]></SQLQuery>
                          <EntityUsage
                          Name="Emp"
                          Entity="com.training.bc4j.Emp"/>
                          <ViewAttribute
                          Name="EmpId"
                          PrecisionRule="true"
                          EntityAttrName="EmpId"
                          EntityUsage="Emp"
                          AliasName="EMP_ID"/>
                          <ViewAttribute
                          Name="Name"
                          PrecisionRule="true"
                          EntityAttrName="Name"
                          EntityUsage="Emp"
                          AliasName="NAME"/>
                          <ViewAttribute
                          Name="Salary"
                          PrecisionRule="true"
                          EntityAttrName="Salary"
                          EntityUsage="Emp"
                          AliasName="SALARY"/>
                          <ViewAttribute
                          Name="Gender"
                          IsPersistent="false"
                          PrecisionRule="true"
                          Precision="10"
                          Type="java.lang.String"
                          ColumnType="VARCHAR2"
                          AliasName="GENDER"
                          SQLType="VARCHAR"
                          IsQueriable="false"
                          Expression="GENDER"
                          IsUpdateable="false"
                          SDOHidden="true">
                          <DesignTime>
                          <Attr Name="_DisplaySize" Value="10"/>
                          </DesignTime>
                          <RecalcCondition><![CDATA[true]]></RecalcCondition>
                          </ViewAttribute>
                          <ViewAttribute
                          Name="Sumation"
                          IsUpdateable="false"
                          IsPersistent="false"
                          PrecisionRule="true"
                          Type="java.lang.Integer"
                          ColumnType="NUMBER"
                          AliasName="SUMATION"
                          SQLType="INTEGER"
                          IsQueriable="false"/>
                          <ResourceBundle>
                          <PropertiesBundle
                          PropertiesFile="com.training.bc4j.TrainingProBundle"/>
                          </ResourceBundle>
                          </ViewObject>
                          ==============================


                          please refer above given EO, VO, DB table emp and an application program for testing written in 1st post.

                          and give some appropriate reply if possible.

                          thanks in advance

                          Mukesh.

                          Edited by: MukeshGangurde on May 8, 2012 5:25 AM
                          • 10. Re: 'Calculated field' returning null after row refresh.
                            Vinay Agarwal
                            I could see the issue you are reporting. All I can say is:

                            1. Include the GENDER attribute in Entity.
                            2. I have filed a bug 14052179 for this.
                            • 11. Re: 'Calculated field' returning null after row refresh.
                              934805
                              It is not always possible to put the attribute in Entity Object. For example, in case we have to have an attribute for the full name of an employee, we would rather do it like --

                              SELECT first_name || ' ' || middle_name || ' ' || last_name AS full_name
                              FROM emp

                              There is no need to have such attribute in the entity object as we are not persisting it in database.

                              However, thanks for raising a bug for it. I have reported this to my seniors and we hope that it will be resolved soon.

                              Please let me know if you need more information and do let us know in case you have a workaround for this issue.

                              Thanks,
                              Mukesh Gangurde
                              • 12. Re: 'Calculated field' returning null after row refresh.
                                Vinay Agarwal
                                Just in case you haven't found a solution to get around the issue here is a workaround.

                                1. Create a new VO EmployeeView1. Remove all its attribute except the PK and GENDER.
                                2. Create a Required bind variable in EmployeeView1.Query section say bindEmployeeID.
                                3. Update the VO query where clause to use the bind variable. The query will now read "Select Employees.EMPLOYEE_ID, Employees.GENDER FROM EMPLOYEES Employees WHERE Employees.Employee_ID = :bindEmployeeID"
                                4. Goto EmployeeView VO editor. Remove the Gender attribute from default list i.e. keep the query to default, no SQL based attribute.
                                5. In ViewAccessors section add EmployeeView1 as accessor (as EmployeeView1_1) and edit the definition of this to make use of EmployeeView.EmployeeId for bind variable EmployeeView1.bindEmployeeID.
                                5. Now add a transient attribute say "GenderTransient" as String and select the default value of it as expression as EmployeeView1_1.first().getAttribute("GENDER")

                                This is it. Hope this helps.
                                • 13. Re: 'Calculated field' returning null after row refresh.
                                  435209
                                  Any update on this? I am also facing the same issue