You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Andrey Razumovsky <ra...@gmail.com> on 2008/11/13 15:31:44 UTC
DataRow to CDO comparison
Suggest I have a DataRow with fresh data which I want to apply to my
database, and already a row with same id exists in DB, so
CayenneDataObject local = (CayenneDataObject)
DataObjectUtils.objectForPK(context, id);
is not null.
How can I now compare it with a DataRow? It's logical to use
DataObject fresh= context.objectFromDataRow(entityName, dataRow, resfresh);
BUT:
if refresh is false, "fresh" object contains old values (those from local,
not from dataRow).
if refresh is true, "fresh" and "local" objects get new values, but property
change operations are not registered, so context.commitChanges() does
nothing.
What's the elegant way to refresh object with new dataRow values?
Re: DataRow to CDO comparison
Posted by Andrus Adamchik <an...@objectstyle.org>.
Doesn't look too bad to me. Alternatively you can write a
PropertyVisitor and use it with ClassDescriptor:
ClassDescriptor cd = entityResolver.getClassDescriptor(...);
cd.visitProperties(new PropertyVisitor() {
public boolean visitAttribute(AttributeProperty property) {
...
// property.writeProperty(..);
return true;
}
public boolean visitToOne(ToOneProperty property) {
...
return true;
}
public boolean visitToMany(ToManyProperty property) {
...
return true;
}
});
Probably not much of a saving in terms of lines of code, but hopefully
will make things a bit more organized.
Andrus
On Nov 14, 2008, at 4:53 PM, Andrey Razumovsky wrote:
> I want to apply all changes and commit.
> My snippet isn't the best code in the world, but it seems to be
> working (my
> DataMap is quite simple).
>
> for (ObjAttribute attr : objEntity.getAttributes()) {
> if (!attr.isFlattened()) {
> local.writeProperty(attr.getName(),
> row.get(attr.getDbAttributeName()));
> }
> }
> for (ObjRelationship rel : objEntity.getRelationships()) {
> if (!rel.isToMany() && rel.getDbRelationships().size() == 1) {
> List<DbJoin> joins = rel.getDbRelationships().get(0).getJoins();
> if (joins.size() == 1) {
> String joinId = joins.get(0).getSourceName();
> Object linkedId = row.get(joinId);
> if (linkedId != null) {
> Object linked = DataObjectUtils.objectForPK(context,
> rel.getTargetEntityName(),
> linkedId);
> local.writeProperty(rel.getName(), linked);
> }
> else {
> local.writeProperty(rel.getName(), null);
> }
> }
> }
> }
>
> I'm sure I can improve it somehow..
>
>
> 2008/11/14, Andrus Adamchik <an...@objectstyle.org>:
>>
>> Yeah, 'objectFromDataRow' is intended for backend updates of the data
>> objects, so it works under assumption that the data in the DataRow
>> is a DB
>> data, and after a refresh, the object is still COMMITTED.
>>
>> So do you want to compare the values or actually apply all changes
>> to the
>> object with the goal of committing them?
>>
>> Andrus
>>
>>
>> On Nov 13, 2008, at 4:31 PM, Andrey Razumovsky wrote:
>>
>>> Suggest I have a DataRow with fresh data which I want to apply to my
>>> database, and already a row with same id exists in DB, so
>>> CayenneDataObject local = (CayenneDataObject)
>>> DataObjectUtils.objectForPK(context, id);
>>> is not null.
>>>
>>> How can I now compare it with a DataRow? It's logical to use
>>> DataObject fresh= context.objectFromDataRow(entityName, dataRow,
>>> resfresh);
>>>
>>> BUT:
>>> if refresh is false, "fresh" object contains old values (those
>>> from local,
>>> not from dataRow).
>>> if refresh is true, "fresh" and "local" objects get new values, but
>>> property
>>> change operations are not registered, so context.commitChanges()
>>> does
>>> nothing.
>>>
>>> What's the elegant way to refresh object with new dataRow values?
>>>
>>
>>
Re: DataRow to CDO comparison
Posted by Andrey Razumovsky <ra...@gmail.com>.
I want to apply all changes and commit.
My snippet isn't the best code in the world, but it seems to be working (my
DataMap is quite simple).
for (ObjAttribute attr : objEntity.getAttributes()) {
if (!attr.isFlattened()) {
local.writeProperty(attr.getName(), row.get(attr.getDbAttributeName()));
}
}
for (ObjRelationship rel : objEntity.getRelationships()) {
if (!rel.isToMany() && rel.getDbRelationships().size() == 1) {
List<DbJoin> joins = rel.getDbRelationships().get(0).getJoins();
if (joins.size() == 1) {
String joinId = joins.get(0).getSourceName();
Object linkedId = row.get(joinId);
if (linkedId != null) {
Object linked = DataObjectUtils.objectForPK(context,
rel.getTargetEntityName(),
linkedId);
local.writeProperty(rel.getName(), linked);
}
else {
local.writeProperty(rel.getName(), null);
}
}
}
}
I'm sure I can improve it somehow..
2008/11/14, Andrus Adamchik <an...@objectstyle.org>:
>
> Yeah, 'objectFromDataRow' is intended for backend updates of the data
> objects, so it works under assumption that the data in the DataRow is a DB
> data, and after a refresh, the object is still COMMITTED.
>
> So do you want to compare the values or actually apply all changes to the
> object with the goal of committing them?
>
> Andrus
>
>
> On Nov 13, 2008, at 4:31 PM, Andrey Razumovsky wrote:
>
>> Suggest I have a DataRow with fresh data which I want to apply to my
>> database, and already a row with same id exists in DB, so
>> CayenneDataObject local = (CayenneDataObject)
>> DataObjectUtils.objectForPK(context, id);
>> is not null.
>>
>> How can I now compare it with a DataRow? It's logical to use
>> DataObject fresh= context.objectFromDataRow(entityName, dataRow,
>> resfresh);
>>
>> BUT:
>> if refresh is false, "fresh" object contains old values (those from local,
>> not from dataRow).
>> if refresh is true, "fresh" and "local" objects get new values, but
>> property
>> change operations are not registered, so context.commitChanges() does
>> nothing.
>>
>> What's the elegant way to refresh object with new dataRow values?
>>
>
>
Re: DataRow to CDO comparison
Posted by Andrus Adamchik <an...@objectstyle.org>.
Yeah, 'objectFromDataRow' is intended for backend updates of the data
objects, so it works under assumption that the data in the DataRow is
a DB data, and after a refresh, the object is still COMMITTED.
So do you want to compare the values or actually apply all changes to
the object with the goal of committing them?
Andrus
On Nov 13, 2008, at 4:31 PM, Andrey Razumovsky wrote:
> Suggest I have a DataRow with fresh data which I want to apply to my
> database, and already a row with same id exists in DB, so
> CayenneDataObject local = (CayenneDataObject)
> DataObjectUtils.objectForPK(context, id);
> is not null.
>
> How can I now compare it with a DataRow? It's logical to use
> DataObject fresh= context.objectFromDataRow(entityName, dataRow,
> resfresh);
>
> BUT:
> if refresh is false, "fresh" object contains old values (those from
> local,
> not from dataRow).
> if refresh is true, "fresh" and "local" objects get new values, but
> property
> change operations are not registered, so context.commitChanges() does
> nothing.
>
> What's the elegant way to refresh object with new dataRow values?