简体   繁体   English

休眠:合并在数据库中给出空值

[英]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. 假设我有一个对象A,其中包含对象B的列表。我首先持久化一个包含1 B的A。然后检索此对象A(使用find),将新对象B添加到其列表中,然后对A.当我在对象A上进行查找时,我的第一个对象B保持良好状态,但是第二个对象只有空字段。

Note that these B objects are instances of an FPML class, generated from the XML description of the library. 请注意,这些B对象是从库的XML描述生成的FPML类的实例。

Please let me know if something is missing in my explanation. 如果我的解释中有遗漏,请告诉我。

Update: 更新:

The problem occurs with InstrumentId objects. 与InstrumentId对象一起出现问题。

    @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 这是生成的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. 仅当对象已保存在数据库中时,Merge()才起作用。 Hibernate looks for the object in its persistence bag. Hibernate在其持久性包中寻找对象。 If it is not there(same ID) then you get an error. 如果不存在(相同的ID),则会出现错误。

Definition Hibernate doc: "Copy the state of the given object onto the persistent object with the same identifier." 定义Hibernate doc:“将给定对象的状态复制到具有相同标识符的持久对象上。” Session.merge() Session.merge()

so if you save Object A at first. 因此,如果您首先保存对象A。 with Session.saveOrUpdate(Object A) then you should be able to merge(Object B). 使用Session.saveOrUpdate(Object A),则应该能够合并(Object B)。

Also see difference between Session.save() and Session.persist(). 另请参见Session.save()和Session.persist()之间的区别。 Difference save and persist. 差异保存并持续。

pls tell me if my answer helped you. 请告诉我我的回答是否对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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