Search code examples
jpaopenjpa

JPA: load a related entity from another persistence context?


I have an entity called Route, which has a related entity called Employee:

public class Route {
    @ManyToOne
    private Employee driver;

    // more fields...
}

public class Employee {

    // more fields...
}

These two entities are stored in different databases. So, they live in two different persistence contexts. Because of that, when I load a Route entity, its "driver" field is null.

Is it possible to load a Route and automatically fetch its "driver" field somehow?


Solution

  • No, it's not possible. How would JPA execute such a basic query?

    select r from Route r where r.driver.name = :name
    

    You'll have to store the ID of the driver in the Route entity, load it explicitely, and hope it's there (because you don't have any foreign key constraint).