0

Here is how the problem occurs:

let's say I have an object A that contains a list of objects B. I first persist an A containing 1 B. Then I retrieve this object A (using find), I add a new object B to its list and I do a merge on A. When I do a find on my object A, I get my first object B well persisted, but the second one has only null fields.

Note that these B objects are instances of an FPML class, generated from the XML description of the library.

Please let me know if something is missing in my explanation.

Update:

The problem occurs with InstrumentId objects.

    @Test
    public void testInstrumentIdPersistenceAndUpdate () throws Exception {

        InstrumentId instrumentId = InstrumentIdUtils.produceInstrument("SX5E:IND", InstrumentIdScheme.BLOOMBERG);

        UnderlyingDefinition underlyingDefinition1 = new UnderlyingDefinition();
        underlyingDefinition1.setSymbol("SX5E:IND");
        underlyingDefinition1.setCurrency(CurrencyUtils.EUR);
        underlyingDefinition1.addInstrumentId(instrumentId);

        ProductDefinition productDefinition1 = new ProductDefinition("PUT");
        productDefinition1.addInstrumentDefinition(underlyingDefinition1);

        Universe universe = new Universe();
        universe.addProductDefinition(productDefinition1);
        universe.setName("channel.test-3");

        universe.setUri(new URI("urn:mapp3.channel.test-3"));

        entityManager.persist(universe);

        InstrumentId instrumentId1 = InstrumentIdUtils.produceInstrument("NES:IND", InstrumentIdScheme.BLOOMBERG);
        underlyingDefinition1.addInstrumentId(instrumentId1);

        entityManager.merge(universe);

        InstrumentId instrumentId2 = InstrumentIdUtils.produceInstrument("TOCH:IND", InstrumentIdScheme.BLOOMBERG);
        underlyingDefinition1.addInstrumentId(instrumentId2);

//        entityManager.merge(universe);

        Universe u = entityManager.find(Universe.class, "urn:mapp3.channel.test-3");


    }

mapping file

<entity name="InstrumentId" class="org.fpml.v57.InstrumentId">

      <table name="T_INSTRUMENT_ID"/>

      <attributes>

         <id name="value" access="PROPERTY">
            <column name="VALUE" length="100"/>
         </id>

         <id name="instrumentIdScheme" access="FIELD">
            <column name="INSTRUMENT_ID_SCHEME" length="100"/>
         </id>

      </attributes>

   </entity>

here is the generated pojo

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "InstrumentId", propOrder = {
    "value"
})
public class InstrumentId
    extends ModelObject
    implements Serializable
{

    @XmlValue
    @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
    protected String value;
    @XmlAttribute(name = "instrumentIdScheme", required = true)
    @XmlSchemaType(name = "anyURI")
    protected String instrumentIdScheme;
Oleg
  • 161
  • 1
  • 14
  • _"Please let me know if something is missing in my explanation."_ With all due respect, everything is missing in your explanation. Post some code for the beginning. – Dragan Bozanovic Sep 11 '15 at 12:21
  • let me know if my update helps... – Oleg Sep 11 '15 at 12:24
  • Yes, it's much better I think. It would not hurt to show the relevant mappings as well. _"...but the second one has only null fields."_ All fields? Including the PK? How did you check that the fields are `null` (you are sure that you did not inspect a Hibernate proxy with the IDE debugger)? – Dragan Bozanovic Sep 11 '15 at 12:29
  • I added the mapping file. Actually, it's when merging that the problem occurs, Hibernate complains that the value field cannot be null, because part of the primary key. – Oleg Sep 11 '15 at 12:49

1 Answers1

-1

Merge() works only if the object is already saved in the database. Hibernate looks for the object in its persistence bag. If it is not there(same ID) then you get an error.

Definition Hibernate doc: "Copy the state of the given object onto the persistent object with the same identifier." Session.merge()

so if you save Object A at first. with Session.saveOrUpdate(Object A) then you should be able to merge(Object B).

Also see difference between Session.save() and Session.persist(). Difference save and persist.

pls tell me if my answer helped you.

Community
  • 1
  • 1
Boermt-die-Buse
  • 94
  • 1
  • 3
  • 12
  • In this case, I actually have a longer chain of object composition (C has a list of A which has a list of B). If I persist a C, then add somme A to it and merge it, it works fine (not including any B). So merge works fine that way but I'll try to save my B before and see if it works, thanks. – Oleg Sep 11 '15 at 14:12
  • ok great, when persisting my B first it works fine. Strange since I could merge my C with new A in it without saving A before. Might be that now I'll be obliged to save my A first as well. I'll let you know. – Oleg Sep 11 '15 at 14:15