1

I created a db some time ago using hibernate (JPA), however I didn't included @version fields for any entities. Turns out that this project became something important and now I need to use versioning (versioning with optimistic locking).

I know I need to add a new property (field) to every single entity with @version annotation.

Question: Does anyone know how can I update the real DB (mysql) to include version columns without data loss and too much work?

Thanks very much.

2 Answers 2

4

Create a SQL statement which create the column for the tables of all entities you want to and the @Version attribute. Then use an other query to set the value of this columns for all rows to 1.

An little bit other way is to create the column with an default value, and then remove the default. (Works for MYSQL)

ALTER TABLE xyz ADD version integer NOT NULL DEFAULT 1;
ALTER TABLE xyz CHANGE version version integer NOT NULL;

The purpuse of this approach is that as long as you not run the second statement (remove the default), the old application will run, even if it not set a value to the version column when it creates new objects.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much. I thought about doing something like this. However, I didn't know exactly if fact of inserting values manually for the version field would cause some trouble. Thanks very much again.
3

in Oracle DDL it would be alter table xyz add ( version NUMBER default 1 not null );

1 Comment

Useful for other users. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.