0

I am pretty new to EclipseLink.

Iwould like to know :

  1. is it possible to create entities from 2 differents databases ? if yes, how ? (example please)

  2. let's say I have Database1 and Database2, is it possible to create composite unit whose one field of entity 1 from database 1 is an entity 2 from database 2. if yes, how (example please)

Thank you very much

mlwacosmos
  • 4,391
  • 16
  • 66
  • 114

1 Answers1

1

Assuming two different schemas on the same server, you should be able to do this using the @SecondaryTable annotation which allows you to map one entity to 2 or more tables. The annotation allows you to specify to the schema or catalog containing the secondary table.

https://docs.oracle.com/javaee/5/api/index.html?javax/persistence/SecondaryTable.html

Would look something like:

@Entity
@Table(name = "main_table")
@SecondaryTable(name="secondary_table", schema="secondary_schema")
public class MyEntity{

    @Column(name = "my_field", table="secondary_table")
    private String fieldFromSecondaryTable;
}

If you are talking about two different servers then you can look at doing something at the database level which would allow you to create a DB view and then map an entity to this view. This would work for read operations but not sure about writing...

In SQL Server, for example, you would be looking at creating a Linked Server:

Selecting data from two different servers in SQL Server

Community
  • 1
  • 1
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • This is very interesting but how do I specify that this secondary table is from another data source ? – mlwacosmos Dec 08 '14 at 10:01
  • How do I specify 2 connections in persistance.xml? – mlwacosmos Dec 08 '14 at 10:02
  • If you are talking mapping one entity to two different database servers rather than 2 schemas on the same server then I don't think the standard mappings will help you with that. – Alan Hay Dec 08 '14 at 10:06
  • In Oracle I could have a dblink but it does not manage functional integrity... My question is : is it only possible with JPA? Create a foreigh key on another database for JPA so it would build object through those two databases. – mlwacosmos Dec 09 '14 at 08:54