-->
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.  [ 9 posts ] 
Author Message
 Post subject: problem with updating one-to-many set
PostPosted: Mon Mar 29, 2004 4:50 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
I have one-to-many relationship, master object that holds set of detail objects. Problem occures, while I am updating existing master object with new set of detail objects.

Hibernate don't delete old set of detail objects, only updates foreign key of detail objects to null and inserts new detail objects.

How can I force hibernate to delete old set of detail objects?
Because foreign key field in detail object is part of uniqnue constraint in my db model, so updating master object with new set of detail objects raises excption on constraint violation.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 29, 2004 7:31 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
cascade="all-delete-orphan"

_________________
Emmanuel


Top
 Profile  
 
 Post subject: what is wrong?
PostPosted: Mon Mar 29, 2004 8:27 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
The code below does not force hibernate to delete existing child records.
This code only trying to insert new childs.

Note: InstrumentBondStatic with instrumentId=710 is existing in database, and have also some child records.


Code:
            session.beginTransaction();

            InstrumentBondStatic bond = new InstrumentBondStatic();
            bond.setInstrumentId(new Long(710));
            bond.setCurrencyId("USD");
            bond.setName("test_bond");

            // create payments
            Set payments = new HashSet();

            InstrumentBondIR bondIR_0 = new InstrumentBondIR();
            bondIR_0.setAdjustment(new Double(11));
            bondIR_0.setAdjustmentDate(new java.util.Date());
            bondIR_0.setSwapBranch("d");
            bondIR_0.setInstrumentBondStatic(bond);
            payments.add(bondIR_0);

            InstrumentBondIR bondIR_1 = new InstrumentBondIR();
            bondIR_1.setAdjustment(new Double(21));
            bondIR_1.setAdjustmentDate(new java.util.Date());
            bondIR_1.setSwapBranch("e");
            bondIR_1.setInstrumentBondStatic(bond);
            payments.add(bondIR_1);

            InstrumentBondIR bondIR_2 = new InstrumentBondIR();
            bondIR_2.setAdjustment(new Double(31));
            bondIR_2.setAdjustmentDate(new java.util.Date());
            bondIR_2.setSwapBranch("f");
            bondIR_2.setInstrumentBondStatic(bond);
            payments.add(bondIR_2);

            // add new payments to bond
            bond.setPayments(payments);

            session.saveOrUpdate(bond);
            session.flush();
            session.connection().commit();


Mapping fragments
Parent:
Code:
    /**
     * @hibernate.set inverse="true" cascade="all-delete-orphan" lazy="true"
     * @hibernate.collection-key column="instrumentId"
     * @hibernate.collection-one-to-many class="com.domainmodel.InstrumentBondIR"
     */
    public Set getPayments() {
        return payments;
    }


Child:
Code:
    /**
     * @hibernate.many-to-one column="instrumentId" class="com.pxpfs.excom2.finance.domainmodel.InstrumentBondStatic"  not-null="true"
     */
    public InstrumentBondStatic getInstrumentBondStatic() {
        return instrumentBondStatic;
    }



SQL log without deleting old child records
    Hibernate: select InstrumentBondIRId_SEQ.NEXTVAL from dual
    Hibernate: select InstrumentBondIRId_SEQ.NEXTVAL from dual
    Hibernate: select InstrumentBondIRId_SEQ.NEXTVAL from dual
    Hibernate: insert into INSTRUMENT_BOND_IR_TBL (instrumentId, swapBranch, cashFlowDate, interestRateType, fixedRate, interestRateId, adjustmentDate, addValue, mulValue, floor, cap, collar, adjustment, cashFlowFixing, periodEndDate, instrumentBondIRId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    Hibernate: update INSTRUMENT_STATIC_TBL set marketId=?, exchangeId=?, exchangeRouteId=?, currencyId=?, isinCode=?, symbol=?, name=?, longName=?, externalIDType=?, externalId=?, wknNr=?, status=?, lastUpdatedDate=?, lastUpdatedTime=?, implementationDate=?, implementationTime=?, internalType=?, roundLot=?, oddLot=?, tickSize=?, tradableFrom=?, tradableFromTime=?, tradableUpTo=?, tradableUpToTime=?, issuerId=?, issuerName=?, issueDate=?, issueTime=?, instType=?, termStructure=?, instrumentFilter=?, RVTradable=?, RVVisibility=?, assetClassId=?, calendarId=?, internalRating=?, isSpecialRiskFree=? where instrumentId=?
    Hibernate: update INSTRUMENT_BOND_STATIC_TBL set subType=?, issueRate=?, maturityDate=?, maturityRate=?, faceValue=?, bookValue=?, adjustment=?, termModel=?, initIndex=?, initRate=? where instrumentId=?


Top
 Profile  
 
 Post subject: not nice solution
PostPosted: Mon Mar 29, 2004 11:17 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
Not nice solution is that before update I read parent object form db, delete all childs, evict this loaded object and than do saveOrUpdate(parent) etc....

parent is bond (InstrumentBondStatic)
child is payment (InstrumentBondIR)

Code:
            InstrumentBondStatic bond = new InstrumentBondStatic();
            bond.setInstrumentId(new Long(710));
            bond.setCurrencyId("USD");
            bond.setName("test_bond");
            bond.setPayments(new HashSet());

            InstrumentBondIR bondIR_0 = new InstrumentBondIR();
            bondIR_0.setAdjustment(new Double(13));
            bondIR_0.setAdjustmentDate(new java.util.Date());
            bondIR_0.setSwapBranch("a");
            bondIR_0.setInstrumentBondStatic(bond);
            bond.getPayments().add(bondIR_0);

            InstrumentBondIR bondIR_1 = new InstrumentBondIR();
            bondIR_1.setAdjustment(new Double(23));
            bondIR_1.setAdjustmentDate(new java.util.Date());
            bondIR_1.setSwapBranch("b");
            bondIR_1.setInstrumentBondStatic(bond);
            bond.getPayments().add(bondIR_1);
            /**
             * At this point we have parent object (bond) fullfiled with childs (payments)
             * This is simulation of real application, at because code above is running
             * in in client, another layer, another computer, virtual machine, etc.
             */
           
           
            // remove all childs of parent
            InstrumentBondStatic bondFromDb = (InstrumentBondStatic) session.load(InstrumentBondStatic.class, bond.getInstrumentId());
            bondFromDb.getPayments().removeAll(bondFromDb.getPayments());
            session.flush();
            session.evict(bondFromDb);

            // persist object to db
            session.saveOrUpdate(bond);
            session.flush();
            session.connection().commit();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 9:16 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
all-delete-orphan only work on collection that has been filled by Hibernate. Hibernate has to keep child state to achieve the delete-orphan capability.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: any recomened solution?
PostPosted: Wed Mar 31, 2004 11:18 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
What is recommended solution in multitier environment (session bean, webservices, etc..)

I would like to send bunch of data (parent with childs) from client to server (session bean, webservice, etc.) and persist it on server side.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 01, 2004 3:38 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You probably could do what you want using an interceptor reimplementing the delete-orphan capability to fit your needs).

_________________
Emmanuel


Top
 Profile  
 
 Post subject: delete orphan
PostPosted: Fri Jun 11, 2004 8:54 pm 
Newbie

Joined: Thu May 20, 2004 6:44 pm
Posts: 4
Location: USA
This is a very common scenario in 3 tier architecture i.e., updating children in a separte session. Hibernate should provide some sort "select before update" feature so that the children could be reloaded to properly enforce delete-orphan

Pranab

_________________
Pranab Ghosh


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 12, 2004 8:16 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Have you tried using the saveOrUpdateCopy method available on the session? That does exactly what you're looking for.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.