Hi ,
I am new to hibernate and facing issues with composite key mapping.Please help,.While running class name Example,At line 36:-
List sites = session.createQuery("Select h from SiteStageDto h").list();
code is returning null pointer,the hql is returing right query but unable to fetch data to list.

I am also posting the configuration hbm.xml file for both my class SiteStageDto ans Site_Uda_Stage.

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.ericsson.hibernate.HibernateUtil;


public class Example
{
    private static SessionFactory factory;
      public static void main(String[] args) {

          try{
                 factory = HibernateUtil.buildSessionFactory();
              }catch (Throwable ex) { 
                 System.err.println("Failed to create sessionFactory object." + ex);
                 throw new ExceptionInInitializerError(ex); 
              }

          Example ex = new Example();
          ex.getsite();

    }
    private void getsite() {
        Session session = factory.openSession();

          Transaction tx = null;
        tx = session.beginTransaction();  
          List sites = session.createQuery("Select h from SiteStageDto h").list();


          for (Iterator iterator = sites.iterator(); iterator.hasNext();)
          {
                SiteStageDto sitestage = (SiteStageDto) iterator.next();  
              System.out.print("SITE_ID: " + sitestage.getSiteId()); 
              System.out.print("clli: " + sitestage.getClli());
              System.out.println("  NUM: " + sitestage.getNum());
              System.out.println("  BASE_NUM: " + sitestage.getBaseNum()); 
              System.out.println("  LATITUDE: " + sitestage.getLatitude()); 
              System.out.println("  LONGITUDE: " + sitestage.getLatitude()); 
              System.out.println("  RESTRICTIONS: " + sitestage.getRestrictions());
              System.out.println("  rdbName: " + sitestage.getRdbName());
              System.out.println("  sourceOfData: " + sitestage.getSourceOfData());
              System.out.println("  gamJobId: " + sitestage.getGamJobId());

              Set siteudastagedto = sitestage.getSiteudastagedto();

              for (Iterator iterator2 =  siteudastagedto.iterator(); iterator2.hasNext();)
                        {
                          SiteUdaStageDto siteuda = (SiteUdaStageDto) iterator2.next(); 
                          System.out.println("GamJobId: " + siteuda.getGamJobId()); 
                          System.out.println("RdbName: " + siteuda.getRdbName()); 
                          System.out.println("SourceOfData: " + siteuda.getSourceOfData()); 
                          System.out.println("Uda: " + siteuda.getUda()); 
                          System.out.println("Udg: " + siteuda.getUdg()); 
                          System.out.println("Value: " + siteuda.getValue()); 


         }
              tx.commit();

}
          //tx.commit();

    } 
}

Edited 3 Years Ago by pritaeas: Changed to thread. Not a code snippet if it's not a fully working example.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.ericsson.orm.pojo.SiteStageDto" table="SITE_STAGE">
        <meta attribute="class-description">
            This class contains the sitestage detail.
        </meta>
        <composite-id>
            <key-property column="SITE_ID" name="siteId" ></key>
            <key-property column="GAM_JOB_ID" name="gamJobId"  ></key>
            <key-property column="DATA_SOURCE_NAME" name="sourceOfData" ></key>
            <key-property column="RDB_NAME" name="rdbName"  ></key>
        </composite-id>
        <!--  <set name="siteudastagedto" cascade="all">
            <key>
            <column name="SITE_ID" />
            <column name="GAM_JOB_ID" />
            <column name="DATA_SOURCE_NAME" />
            <column name="RDB_NAME" />
            </key>
            <one-to-many class="com.ericsson.orm.pojo.SiteUdaStageDto" ></one>
        </set> --> 
        <property name="clli" type="java.lang.String" access="field">
            <column name="CLLI" />
        </property>
        <property name="num" type="java.lang.String" access="field">
            <column name="NUM" />
        </property>
        <property name="baseNum" type="java.lang.String" access="field">
            <column name="BASE_NUM" />
        </property>
        <property name="latitude" type="java.lang.String" access="field">
            <column name="LATITUDE" />
        </property>
        <property name="longitude" type="java.lang.String" access="field">
            <column name="LONGITUDE" />
        </property>
        <property name="restrictions" type="java.lang.String" access="field">
            <column name="RESTRICTIONS" />
        </property>



        </property>
    </class>


</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="com.ericsson.orm.pojo.SiteUdaStageDto" table="SITE_UDA_STAGE">
      <meta attribute="class-description">
         This class contains the SITE_UDA_STAGE records. 
      </meta>
    <composite-id>
            <key-property column="SITE_ID" name="siteId" type="java.lang.String" />
            <key-property column="GAM_JOB_ID" name="gamJobId"
                type="java.lang.Integer" />
            <key-property column="DATA_SOURCE_NAME" name="sourceOfData"
                type="java.lang.String" />
            <key-property column="RDB_NAME" name="rdbName" type="java.lang.String" />
        </composite-id>

        <property name="udg" type="java.lang.String" access="field">
            <column name="UDG" />
        </property>
        <property name="value" type="java.lang.String" access="field">
            <column name="VALUE" />
        </property>
        <property name="uda" type="java.lang.String" access="field">
            <column name="UDA" />
        </property>


   </class>
</hibernate-mapping>

I am got resolved the problem at line 36,it started giving the data in list from sitestage table,but now it fetching null data from site udastage table so now giving error at line 56:
Set siteudastagedto = sitestage.getSiteudastagedto();

Can anyone help me with composite key mapping in hbm.xml

This question has already been answered. Start a new discussion instead.