How to implement this with Hibernate?

Currently I am using hibernate with Spring Data JPA to replicate data from a Postgres database into Oracle.

I have an entity called EntityA (table A) with attributes Attribute1, Attribute2, Attribute3

I have another entity called EntityB (table B) with attributes Attribute1, Attribute2, Attribute3

table B is exclusive to my end of the replication it doesn’t exist in the source I’m replicating from.

EntityA and EntityB share the same primary key.

EntityB is essentially a clone of EntityA except that it may contain attributes (Attriibute1) whose values are exclusive to my end of the replication. That is these attributes are no longer maintained in the upstream system ie. EntityA will no longer have these attributes populated and so I’ve taken ownership of them in EntityB. Legacy values exist for these attributes in the upstream system so when I first replicate I want to copy those attributes but after the initial replication I will no longer be expecting any new values in those attributes from the upstream system in EntityA

The reason two entities exist is so that I can update EntityA without overwriting what’s been maintained locally in EntityB. but I still want to initially pick up all the legacy values that were found in the older EntityAs when I create the EntityB.

I do this by ensuring that Attribute1 on EntityB has insertable=true, updateable=false

This way I ensure that I don’t accidentally clobber any Attribute1 values I’ve defined on EntityB if I revisit it’s associated EntityA that didn’t have this attribute populated.

I can expect to find new EntityAs just not with Attribute1 populated and when I do I want to create it’s associated EntityB. Later a part of my program will allow me to populate EntityB’s Attribute1 and I do this by using a different Entity called EntityC mapped to the same table but which doesn’t have any restrictions on the @Column annotation for Attribute1 effectively insertable=true, updateable=true

hope that’s clear.

The replication is defined to be incremental but I can also wind back and revisit old EntityA’s again. When that happens I want to ensure that for those EntityA’s that I have EntityB’s for that I don’t want the null for EntityA’s Attribute1 to overwrite an existing Attribute1 value I’ve populated on EntityB.

Is this the best way to achieve this or is there better pattern to employ?

I need to able to truncate EntityA without affecting EntityB but is there a way to use MapsId or PrimaryKeyJoinColumn

Note to mods re: anti-spam If I’m using annotations and they don’t map to a user’s name then perhaps don’t interpret them a user references and block my post.

What a wonderful idea, if only we had thought about doing something about it.

As you probably know, the Hibernate team didn’t implement Discourse and its behavior. As you might not know, we’re facing a lot of spam, so these anti-spam tools are useful.

Of course if you want to help, and if you want to annotations not to be interpreted as mentions, just format them properly as code, like the error message probably explained to you:


Is this the best way to achieve this or is there better pattern to employ?

Sounds reasonable to me. Not sure what kind of answer you are seeking here, but it seems to me that this is fine and works for you. Or do you have issues with this approach?