Trollbane
Trollbane

Reputation: 327

EclipseLink/JPA and multiple databases with similar structures

Due decisions out of my control, I have two existing databases which are almost clones.

"Almost" as in there are tables/columns in #1 which does not exist in #2, and vice versa.

What is the best approach here?

The "brute force" route seem to be "create all entities for one, copy to new package and add/remove for second database". Which is not really a route I'd like to go.


Oh, and JPA is definitely not allowed to alter the schema in either database.

Besides EclipseLink, I use Spring 3.1 and SpringData.

Upvotes: 0

Views: 672

Answers (2)

James
James

Reputation: 18379

You might want to look into EclipseLink's extensibility support.

You can add a properties Map to the class, and use an orm.xml to map the additional attributes in the other database into this map.

See, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Extensible_Entities

Upvotes: 1

siebz0r
siebz0r

Reputation: 20329

If your databases differ you need to make different classes/mappings. For example, if you have a table Foo in both databases which has 3 columns in db1 (id, baz, pleh) and 3 other columns in db2 (id, baz, qux) you cannot do this:

@Entity
public class Foo
{
    @Column(name = "id")
    private Long id;
    @Column(name = "baz")
    private String baz;
    @Column(name = "pleh")
    private String pleh;
    @Column(name = "qux")
    private String qux;
}

Doing so will result in exceptions as JPA will generate insert statements like:

insert into Foo (id, baz, pleh, qux) values (?, ?, ?, ?)

For DB1 you need to create a class Foo like this:

@Entity
public class Foo
{
    @Column(name = "id")
    private Long id;
    @Column(name = "baz")
    private String baz;
    @Column(name = "pleh")
    private String pleh;
}

And for DB2 like this:

@Entity
public class Foo
{
    @Column(name = "id")
    private Long id;
    @Column(name = "baz")
    private String baz;
    @Column(name = "qux")
    private String qux;
}

Upvotes: 0

Related Questions