Hibernate:合并在数据库中给出空值
Hibernate: merge gives null values in DB
问题是这样发生的:
假设我有一个包含对象 B 列表的对象 A。
我先坚持一个包含1个B的A。
然后我检索这个对象 A(使用查找),我将一个新对象 B 添加到它的列表中,然后我对 A 进行合并。
当我对我的对象 A 进行查找时,我的第一个对象 B 得到了很好的持久化,但第二个对象只有空字段。
请注意,这些 B 对象是 FPML class 的实例,根据库的 XML 描述生成。
如果我的解释中有什么遗漏,请告诉我。
更新:
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");
}
映射文件
<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>
这里是生成的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() 仅在对象已保存在数据库中时才起作用。 Hibernate 在它的持久包中寻找对象。如果它不存在(相同的 ID),那么你会得到一个错误。
定义 Hibernate 文档:
"Copy the state of the given object onto the persistent object with the same identifier."
Session.merge()
所以如果你先保存对象A。使用 Session.saveOrUpdate(Object A) 那么你应该能够合并(Object B)。
另请参阅 Session.save() 和 Session.persist() 之间的区别。
Difference save and persist.
如果我的回答对您有帮助,请告诉我。
问题是这样发生的:
假设我有一个包含对象 B 列表的对象 A。 我先坚持一个包含1个B的A。 然后我检索这个对象 A(使用查找),我将一个新对象 B 添加到它的列表中,然后我对 A 进行合并。 当我对我的对象 A 进行查找时,我的第一个对象 B 得到了很好的持久化,但第二个对象只有空字段。
请注意,这些 B 对象是 FPML class 的实例,根据库的 XML 描述生成。
如果我的解释中有什么遗漏,请告诉我。
更新:
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");
}
映射文件
<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>
这里是生成的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() 仅在对象已保存在数据库中时才起作用。 Hibernate 在它的持久包中寻找对象。如果它不存在(相同的 ID),那么你会得到一个错误。
定义 Hibernate 文档: "Copy the state of the given object onto the persistent object with the same identifier." Session.merge()
所以如果你先保存对象A。使用 Session.saveOrUpdate(Object A) 那么你应该能够合并(Object B)。
另请参阅 Session.save() 和 Session.persist() 之间的区别。 Difference save and persist.
如果我的回答对您有帮助,请告诉我。