javafueled
javafueled

Reputation: 534

One Hibernate Entity shared across Two Schemas

I'm working in an inherited and quite legacy application that, for reasons, works across two database schemas, Schema A and Schema K. A user chooses a "context" to act in, and while the contexts are, ostensibly at least, considered to be identical, features have diverged and a database table has earned new columns in Schema A not in Schema K. Each schema, of course, has a table for the entity, but Schema A has a few more columns than Schema K. And this is the rub.

A rare bug exists and has been to user actions working with Schema A. However, in designing a solution to the bug, the team realized that the application is using a single Hibernate entity for both Schema A and Schema K.

Is it possible to annotate a column in the shared entity for Schema A that would not introduce a persistence error in Schema K? Schema K cannot be altered to introduce the new columns.

Upvotes: 0

Views: 147

Answers (1)

Christian Beikov
Christian Beikov

Reputation: 16430

What do you mean the schema can't be altered? Can you introduce a view for this purpose? The problem is that you can't map columns that don't exist in the other schema, otherwise you will encounter issues.

If you can at least add a view, you could use that view instead of the table. Databases usually support writing to views. As for the columns that don't exists in the other schema, you have two options. Mark the columns as insertable = false, updatable = false so you can only read them, or use @DynamicUpdate and ensure you don't change the values of these columns when you work with the schema that doesn't have these columns.

Without a view, you are as far as I can see out of luck. You would have to map both tables as different entities that each target a specific schema.

Upvotes: 2

Related Questions