You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Hugi Thordarson <hu...@karlmenn.is> on 2020/05/05 15:26:02 UTC

Totally ignoring updates to a certain dbAttribute

Hi all,
I have a field in the DB that once set should never change (specifically, never be set to null).

For … reasons … however, it will sometimes be set to null as part of changing some relationships (where it takes part in one of many joins)

I've worked around this on the SQL side, by creating my own implementation/override of UpdateBatchTranslator.createSQL() that wraps the field in a coalesce function ("setting" the columns current value if null is passed in the bindings). That works fine and makes the DB happy.

if( thatBloodyAttribute ) {
	buffer.append( " = COALESCE( ?, " + attribute.getName() + " )" );
}
else {
	buffer.append( " = ?" );
}

However, things are still unresolved on the Cayenne side of things, since Cayenne will think the column is null.

Is there any way for me to ask Cayenne politely to totally ignore any changes to this column, once it's been set? Perhaps a way to intercept the value change and stopping it on it's way down to the ObjectStore?

I've tried exposing the DbAttribute and setting it manually after updating the relationship—but the change doesn't seem to "take". I.e. if I do this…

Object previousAttributeValue = object.thatBloodyAttribute();
object.setRelationshipThatUsesThatBloodyAttribute( null );
setThatBloodyAttribute( previousAttributeValue );

…"thatBloodyAttribute" will still be null.

I realize I'm asking for something of a clusterfuck here, but it's unavoidable since I'll be running alongside an older system for a while and can't change the DB structure :/.

Cheers,
- hugi