Hi,
since a week I have been trying to find a solution to a strange problem.
We are using ObjectDB 2.6.8_02 im embedded mode.
The problem is that data gets lost when updating an entity.
1-) Background
Consider Three entities: ObjectValue, Attribute and Value.
ObjectValue has a one to many relationship to Attribute and Attribute has a one to one relationship to Value.
All relationships are annoted with Cascade.ALL. That is, when merging an ObjectValue, the merge operation is propagated to the referenced Attributes and from the Attributes to the Values.
Updating an ObjectValue means updating the referenced Attributes.
Updating an Attribute means replacing a referenced Value with a new Value entity.
2-) Problem Description
I initialized the database with some ObjectValues. Persisting new ObjectValues (with new Attributes and new Values)
works fine. But, update does not work. The data gets lost. I attached a formatted log file.
In the following, I consider the most important part of the log file. Before the update operation,
I dump the properties of the object I want to update.
ObjectValue ID:=100, TYPE:=Person, REVISION:=0, isPersisted:=true, LockMode:=PESSIMISTIC_WRITE]]
...
Attribute ID:=1400, Name:=profileName, Value:=ID:=1400, PrimitiveValue:=ProfileName 99, REVISION:=0, isPersisted:=true
...
After the update Operation I also dump the properties of the object.
ObjectValue ID:=100, TYPE:=Person, REVISION:=2, isPersisted:=true, LockMode:=PESSIMISTIC_WRITE]]
...
Attribute ID:=1400, Name:=profileName, Value:=ID:=null, PrimitiveValue:=ProfileName 99 (Just a test update...), REVISION:=1, isPersisted:=true
...
As you can see, I changed from "ProfileName 99" to "ProfileName 99 (Just a test update...)"
The id of the new Value object is null "Value:=ID:=null". This is the first strange thing. Because, after persisting
an object, it should have an id!!!
When retrieving the ObjectValue from the database, all values are lost.
ObjectValue ID:=100, TYPE:=Person, REVISION:=2, isPersisted:=true, LockMode:=null
...
Attribute ID:=1400, Name:=null, Value:=null, REVISION:=null, isPersisted:=false
...
To confirm that the values are lost, I started the explorer and searched for the ObjectValue with revision 2
( See attached picture). Indeed, all values are lost, not just the values of the updated attribute.
3-) What I did
I thought first, that ObjectDB has a problem with cascade, so I persisted manually all referenced Attributes and the
corresponding values. But I still got the same problem.
I tried several other alternatives. But it was useless.
Here is the code:
@Override
public ObjectValue updateObjectValue(ObjectValue ov, ObjectValue... ovList) {
// getting transaction
System.out.println("Trying to get transaction " + ov.getId());
// edit all locked objects
System.out.println("Trying to merge first object " + em.getLockMode(ov));
/*
* Trying to persist manually does not work.
for(Attribute attr: ov.getAttributes()){
if(em.contains(attr.getValue())){
em.merge(attr.getValue());
}else{
em.persist(attr.getValue());
}
em.flush();
if(em.contains(attr)){
em.merge(attr);
}else{
em.persist(attr);
}
em.flush();
}
*/
ObjectValue persistedObjectValue = em.merge(ov);
persistedObjectValue.setRevision(persistedObjectValue.getRevision() + 1);
System.out.println("Trying to commit");
em.flush();
System.out.println("After flush " + em.getLockMode(ov));
persistedObjectValue.setRevision(persistedObjectValue.getRevision() + 1);
return persistedObjectValue;
}
Did someone already faced a similar problem ?
Any hint is welcome ?