Apache Cayenne > Index > Cayenne Examples > Multiple Databases Example

This example demonstrates how a single Cayenne mapping can be used transparently with multiple databases, even with relationships between tables across DBs.

How to Run

  • Download the example and unpack it somewhere.
  • cross-db-example folder you end up with is a self-contained Eclipse project, including Cayenne 1.2M10 jar and HSQLDB jar. Just import the folder in Eclipse (Eclipse 3.1 or newer is recommended).
  • From Eclipse run test.Main class as "Java Application". As in-memory HSQLDB is used, Cayenne generates the schema on the fly and no extra DB setup is required.

Mapping Details

You can open and inspect mapping in CayenneModeler. XML files are located under cross-db-example/src. Each database schema has its own DataMap, with a relationship spanning across two DataMaps:

Limitations

Doing a join across DBs generally doesn't work (although it may appear to work in simpler cases of matching of FK), as it will require Cayenne to do in memory cartesian product processing. So you'll have to do it manually.

So this will not work:

SelectQuery select = new SelectQuery(Person.class);
Expression expression = ExpressionFactory.matchExp(Person.DEPARTMENT_PROPERTY +
	"." + Department.NAME_PROPERTY, "IT Department");