简体   繁体   中英

Hibernate: merge gives null values in DB

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM