-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: about composite-id mapping
PostPosted: Sun Dec 10, 2006 9:38 pm 
Newbie

Joined: Mon Oct 30, 2006 2:31 am
Posts: 8
hi all,

i have a master which has an id of sequence generator and my detail has 3 primary keys, one of those is master id. my mapping are the followings:

my master
Code:
<hibernate-mapping auto-import="true" default-lazy="true">
    <class name="net.nationalpayment.pps.model.IncentiveProgram" table="Incentive">
        <id name="incentiveProgramId" column="Inc_IncProgId" type="long">
            <generator class="sequence"/>
        </id>

        <property name="agencyId" column="Inc_AgencyId" type="string" length="7"/>
        <property name="dealerId" column="Inc_DlrId" type="string" length="20"/>
        <property name="effectiveFrom" column="Inc_EffFrom" type="date"/>
        <property name="effectiveTo" column="Inc_EffTo" type="date"/>
        <property name="allDealerReps" column="Inc_AllDlrReps" type="integer"/>
        <property name="npnCycle1Fee" column="Inc_NPNCycle1Inc" type="double"/>
        <property name="npnCycle2Fee" column="Inc_NPNCycle2Inc" type="double"/>
        <property name="used" column="Inc_IsUsed" type="integer"/>
        <property name="comments" column="Inc_Comm" type="string" length="4000"/>

        <set name="incentiveProgramDetails" cascade="all, delete-orphan" lazy="false">
            <key column="Ind_Id"/>
            <one-to-many class="net.nationalpayment.pps.model.IncentiveProgramDetail"/>
        </set>

        <property name="auditInfo" type="net.nationalpayment.pps.model.AuditInfoUserType">
            <column name="LAST_UPDATED_ON"/>
            <column name="LAST_UPDATED_BY"/>
            <column name="CREATED_DATE"/>
            <column name="CREATED_BY"/>
        </property>
    </class>
</hibernate-mapping>


my detail
Code:
<hibernate-mapping auto-import="true" default-lazy="true">
    <class name="net.nationalpayment.pps.model.IncentiveProgramDetail" table="Incentive_Dtl">
      <composite-id name="incentiveDetailId" class="net.nationalpayment.pps.model.IncentiveProgramDetailId">
         <key-property name="incentiveProgramId" column="Ind_IncProgId" type="long"/>
         <key-property name="entityId" column="Ind_EntityId" type="string" length="30"/>
            <key-property name="entityType" column="Ind_EntityType" type="string" length="2"/>
        </composite-id>

        <property name="cycle1Fee" column="Ind_Cycle1Inc" type="double"/>
        <property name="cycle2Fee" column="Ind_Cycle2Inc" type="double"/>

        <many-to-one name="incentive" class="net.nationalpayment.pps.model.IncentiveProgram"
         column="Ind_Id" lazy="false"/>

        <property name="auditInfo" type="net.nationalpayment.pps.model.AuditInfoUserType">
            <column name="LAST_UPDATED_ON"/>
            <column name="LAST_UPDATED_BY"/>
            <column name="CREATED_DATE"/>
            <column name="CREATED_BY"/>
        </property>
    </class>
</hibernate-mapping>


my class that holds the primary keys
Code:
public class IncentiveProgramDetailId implements java.io.Serializable{
    private Long incentiveProgramId;
    private String entityId;
    private String entityType;

    public String getEntityId() {
        return entityId;
    }

    public void setEntityId(String entityId) {
        this.entityId = entityId;
    }

    public String getEntityType() {
        return entityType;
    }

    public void setEntityType(String entityType) {
        this.entityType = entityType;
    }

    public Long getIncentiveProgramId() {
        return incentiveProgramId;
    }
    public void setIncentiveProgramId(Long incentiveProgramId) {
        this.incentiveProgramId = incentiveProgramId;
    }

    public boolean equals(Object obj){

      if (obj == null ){
         return false;
      }
      if (!(obj instanceof net.nationalpayment.pps.model.IncentiveProgramDetailId))
      {
         return false;
      }
      else{
            net.nationalpayment.pps.model.IncentiveProgramDetailId detailId =
                    (net.nationalpayment.pps.model.IncentiveProgramDetailId) obj;
         if (null == this.getIncentiveProgramId() || null == detailId.getIncentiveProgramId() ||
            null == this.getEntityId() || null == detailId.getEntityId() ||
                null == this.getEntityType()|| null == detailId.getEntityType())
            return false;
         else
            return (this.getIncentiveProgramId().equals(detailId.getIncentiveProgramId()) &&
                   (this.getEntityId().equals(detailId.getEntityId())) &&
                        (this.getEntityType().equals(detailId.getEntityType())));
      }
   }
}


my questions are:
1) when i save the record, does the hibernate will automatically assign the master id to the detail? im confuse since master id is system generated

2) save is working now but i did this in my dao implementation:

Code:
Set details = incentiveProgram.getIncentiveProgramDetails();
        incentiveProgram.setIncentiveProgramDetails(null);

        IncentiveProgram result = (IncentiveProgram) this.getHibernateTemplate().merge(incentiveProgram);

        Iterator detail = details.iterator();
        while(detail.hasNext()){
            IncentiveProgramDetail incentive_detail = (IncentiveProgramDetail) detail.next();
            incentive_detail.getIncentiveDetailId().setIncentiveProgramId(result.getIncentiveProgramId());

            this.storeIncentiveProgramDetail(incentive_detail);
        }
        return this.loadIncentiveProgram(result.getIncentiveProgramId());


is there a better solution to this?

3) when i delete the record(s) only the master will be deleted. i dont know what happen...

any help are greatly appreciated!

regards,
think


Top
 Profile  
 
 Post subject: about composite-id mapping
PostPosted: Mon Dec 11, 2006 3:00 am 
Newbie

Joined: Mon Oct 30, 2006 2:31 am
Posts: 8
hi all,

i solved the problem... i found out in my dao implementation level

Code:
IncentiveProgram result = (IncentiveProgram) this.getHibernateTemplate().merge(incentiveProgram);

        Iterator detail = result.getIncentiveProgramDetails().iterator();
        while(detail.hasNext()){
            IncentiveProgramDetail incentive_detail = (IncentiveProgramDetail) detail.next();
            incentive_detail.getIncentiveDetailId().setIncentiveProgramId(result.getIncentiveProgramId());

            this.storeIncentiveProgramDetail(incentive_detail);
        }
        return this.loadIncentiveProgram(result.getIncentiveProgramId());


tnx anyway for reading my thread


regards,
think


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.