1

現在、次のようにJPA entity定義されています。

@Entity
@Table(name="Foo")
public class Foo extends ParentClass {
...
}

Foo一部のオブジェクトがすでにデータベースに永続化されていると想像してください。それらの永続化されたレコードをタイプの兄弟サブクラスにプログラムで変更するための最良の方法は何でしょうBarか?

@Entity
@Table(name="Bar")
public class Bar extends ParentClass {
...
}

データベーススキーマを変更するのではなく、すでに永続化されているFooオブジェクトのエンティティタイプのみを変更します。私が考えていた唯一の方法よりも優れた方法があることを望んでいます。それは、Fooオブジェクトを手動で取得し、に再マッピングしBar、再永続化することです。

4

1 に答える 1

1

Particularly I prefer to do it using pure SQL migration. In my company we use liquibase to version our database schema. So, to do the migration we just create changelogs for it.

Some argue that it's not the best solution, but I find it simple and quite powerful. In my opnion, for big projects it's the way to go.


EDIT:

Now that I see that you can't change the schema. I don't think this is possible in an easy way programmatically. You would need somekind of "TransformerService" between Foo and Bar, that simply copies the common parent fields, deletes the old Foo and persist the new Bar.

What I would do is to break the hierarchy and componentize ParentClass, so you can transfer only it between Foo and Bar, viceversa. This way you won't need to copy the fields or anything like that. It may need a lot of refactoring, but componentizing is better than creating big/complex hierarchies.

Or you can just run an SQL that changes the object directly at database. It's ugly, but it's the only way I see you could do something like that.

于 2013-01-25T17:02:08.740 に答える