What, precisely, do you mean by "connect across two different persistence units"?
You can connect to two persistence units by defining two 'EntityManager' instances, one for each PU.
Is that what you're asking for?
Which annotations did you have in mind? Certainly '@Entity' and '@EntityManager' will work with multiple persistence units. Of course, annotations like '@OneToMany' will not, AFAIK, since they map to foreign-key relationships in the DBMS.
I had in mind specifically the annotation in the subject. OneToOne. With
that, I think I could get the rest to work or understand the problems
better.
Although the OneToOne and OneToMany use foreign key information when you
generate entity classes from a database,in my testing, they do not
generate SQL joins. The JDBC driver for OpenVMS is one I wrote myself
and I have spent a considerable amount of time in the last three months
debugging and testing it to get it to work correctly with NetBeans and
JPA. The driver is currently in beta testing at a customer site and the
question about annotations came up.
If you have a class A with a OneToOne or a OneToMany relation to class
B, you get a query to load class A and then a separate query to load a
either class B or collection of class B. This works with either driver
as long as the foreign keys information is available. This could work
across two different persistence units as long as the right query gets
sent to the right driver and that information is in the persistence.xml
file.
So, to be precise, I have two different databases, both with JDBC
drivers. The persistence.xml file defines two persistence units and from
each I can create an EntityManagerFactory and get an EntityManager and
then do queries on each. Now there is the problem of annotations. Which
annotations to be precise? Well, how about the one in the subject,
OneToOne. Now I ask (quite precisely):
"Does someone have an example of using the JPA annotations to connect
across two different persistence units?"
But perhaps someone has an example of an annotation other that OneToOne.
That could also be helpful as I would like to understand more about how
the annotations work, so I left out of the question which annotation.
Since there is no way to define in either database a foreign key that
crossed the two databases, if an annotation could work, it would have to
be added manually. We know how to change the entity classes directly to
do this without using annotations, but would prefer the annotation
approach as it should be more clear, more consistent and (hopefully)
less code to debug.
Jeff Coffield