0

Let's say that i have the two Entities below :

@Entity
public class Employee {
String name;
@ManyToOne Department department;
}

@Entity
public class Department {
// persisted flieds ...
@OneToMany(mappedBy="department") Collection<Employee> employees;
}

and their corresponding tables are defined in two different schema oldSchema and newSchema with different content like this :

  • oldschema.Department, oldSchema.Employee
  • newSchema.Department, newSchema.Employee

I'm working with the newSchema, what i'm trying to do is to retrieve a Department entity object from the oldSchema with it's Collection of Employee from the oldSchema too.
What i'm getting is the Department Entity from the oldSchema but with the Collection of Employee from the newSchema.

The code i'm using:

 Query q = entityManager.createNativeQuery("SELECT * FROM 
           oldSchema.Department d where d.departmentID = ?", Department.class);
 q.setParameter(1, depID);
 try{
 Department d = (Department) q.getSingleResult();
 }catch(exception){}

the object d contains Collection of Employees which are stored in newSchema.Employee

And this is due to the automatic schema evolution as explained here for ObjectDB https://www.objectdb.com/java/jpa/entity/schema which seems the same as for EclipseLink in my case , correct me if i'm wrong.

I'll apreciate your help!

user771221
  • 127
  • 9
  • Are you using objectdb as your jpa implementation? – Michael Wiles Feb 26 '19 at 11:58
  • i'am using EclipseLink(JPA 2.1) – user771221 Feb 26 '19 at 12:15
  • 1
    hmmm it appears that your reference to schema evolution is specific to objectdb's JPA implementation. Not a guarantee that eclipselink will do the same – Michael Wiles Feb 26 '19 at 13:20
  • Why dont you refer to the docs for your chosen JPA provider? You presumably chose that one for a reason, so it seems weird to then use the docs for some other JPA provider, including their own vendor extensions –  Feb 26 '19 at 13:30
  • @MichaelWiles i'm refering to that link just to tell that i'm getting the same behaviour ! – user771221 Feb 26 '19 at 13:44
  • If you have some object in a different schema to what the class is configured to use then you need a separate EntityManagerFactory for the other schema, and don't put schema info in annotations (use orm.xml for each EMF instead). –  Feb 26 '19 at 16:04

1 Answers1

2

Create more persistence units in persistence.xml or in orm.xml or whatever are you using. One persistence unit per schema. One entity manager / factory per schema.

Peter Šály
  • 2,848
  • 2
  • 12
  • 26
  • The schema definition can be defined in an ORM.xml (or eclipselink-orm.xml) file, allowing you to use the same java classes in multiple persistence units. Use a different EMF/EntityManager when you want to obtain data from the 'other' schema. Just be sure to remember where you got your instances from and don't try to mix and match references. – Chris Feb 26 '19 at 16:35