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();

    } 
}

Dani AI

Generated

Diagnosis and concise fixes for ’s thread

The outer query is returning SITE_STAGE rows but the child set is coming back null because the collection mapping in SiteStageDto is not active and the composite-key pieces must match exactly between parent and child. In the SiteStageDto hbm you posted the <set> mapping is commented out, and there’s also a stray extra closing </property> that can break the mapping file — both will prevent Hibernate from wiring the child collection into the entity instance.

Checklist of the concrete fixes to apply

  • Re-enable a collection mapping on SiteStageDto (a set or list). The parent mapping must declare a <key> that lists ALL composite columns (SITE_ID, GAM_JOB_ID, DATA_SOURCE_NAME, RDB_NAME) and a <one-to-many> pointing at SiteUdaStageDto. Make sure the mapping element name exactly matches the Java field/getter (siteudastagedto) and that access (field vs property) is consistent.
  • Make types identical for each composite-id component in both mappings and in the Java properties. For example, if GAM_JOB_ID is an integer in DB then map/use java.lang.Integer everywhere; a type mismatch prevents Hibernate from matching rows.
  • If SITE_UDA_STAGE’s PK is also a foreign key to SITE_STAGE, model those components on the child side as key-many-to-one entries (or otherwise make the FK relationship explicit) so Hibernate understands the association as more than independent keyed fields.

Runtime / debugging tips

  • Move tx.commit() out of the loop (commit after processing) so lazy collections can be accessed while the session is open.
  • To avoid lazy-loading surprises, fetch children in the same query: e.g.
    select h from SiteStageDto h left join fetch h.siteudastagedto

    or call Hibernate.initialize(...) before commit.

  • Validate the two hbm.xml files for well-formed XML and check SessionFactory startup logs — mapping errors there will explain why the collection wasn’t created (Hibernate will supply an empty persistent set when mapped correctly, not null).
<?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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.