This discussion is archived
1 Reply Latest reply: Jul 19, 2012 11:59 AM by cdelahun RSS

TopLink update a child

546521 Newbie
Currently Being Moderated
Hi
We have a pretty old TopLink config Project in which a Customer, trade and tradecode objects defined as parent-child (oneToMany).
Whenever we change a trade code the code is getting updated in the trade table. This change is reflecting on the other customers, who are referring this trade code. This is how its built.

{
TOPLink.Public.PublicInterface.Descriptor descriptor = new TOPLink.Public.PublicInterface.Descriptor();
descriptor.setJavaClass(mdb.trade.Trade.class);
Vector vector = new Vector();
vector.addElement("Trade");
descriptor.setTableNames(vector);
descriptor.setIdentityMapClass(TOPLink.Private.IdentityMaps.HardCacheWeakIdentityMap.class);
descriptor.setExistenceChecking("Check cache");
descriptor.setIdentityMapSize(1000);
descriptor.createCopyPolicy("constructor");
descriptor.createInstantiationPolicy("constructor");

mapping = new TOPLink.Public.Mappings.DirectToFieldMapping();
mapping.setAttributeName("tradeCodeId");
mapping.setFieldName("trade.tradeCodeId");
mapping.setIsReadOnly(true);
mapping.setNullValue(new Long(0));
mapping.setGetMethodName("getTradeCodeId");
mapping.setSetMethodName("setTradeCodeId");
descriptor.addPrimaryKeyFieldName("trade.tradeCodeId");
descriptor.addMapping(mapping);

...

}

Users can change the trade code from UI. It is getting updated the trade code for that tradeCodeId.
Now we need to update the tradeCodeId for this customer. we need to search for a matching codeId and replace with that codeId, if not create a new one and update with the new Id. Can this be done using the config?
Thanks a bunch.
  • 1. Re: TopLink update a child
    cdelahun Pro
    Currently Being Moderated
    I'm not sure of your background or experience with TopLink, but the old TOPLink config you are using should work and look pretty similar to the Oracle TopLink or native EclipseLink config.

    What I'm missing from your description is what you mean when you discuss what you change and works vs the changes you need to perform now. You only show the Trade object and its "tradeCodeId" pk, so I can only guess what you mean when you say you want to update a tradeCodeId for the customer. Do you mean that the Customer table has a tradeCodeId or a reference to a Trade object?
    As long as the field is not appart of the primary key, it should be able to be changed. The only problem I forsee you might encounter is with creating a new Trade instance and persisting it in the database - the primary key mapping for tradeCodeId has setIsReadOnly(true) called on it, which might cause problems in trying to set this value.

    Other than that, you would just use an expression query in the same way the application might look up a customer now. Something like:
    ReadQuery query = new ReadObjectQuery(Trade.class);
    query.setSelectionCriteria(query.getExpressionBuilder().get("tradeCodeId").equal(query.getExpressionBuilder().getParameter("idParameter")));
    query.addArgument("idParameter");
    Vector arguments = new Vector();
    arguments.add(yourIdValue);
    Trade trade = (Trade)session.executeQuery(query, arguments);

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points