In postgresql
, using the schema attribute of the table annotation, this can be done with joined table inheritance:
@Entity
@Table(schema = "`DB2`", name="BASE_EMPLOYEES")
@Inheritance(strategy = InheritanceType.JOINED)
public class BaseEmployee {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
protected Long id;
private String name;
private String address;
private String department;
...
}
@Entity
@Table(schema = "`DB1`", name = "FULL_EMPLOYEES")
public class FullEmployee extends BaseEmployee {
private Integer age;
private String qualification;
private String experience;
...
}
I have tested this in a postgresql
database, and this configuration works OK, there is no need to define multiple entity manager factories and datasources.
Note: In other databases you might not need the quotes around the schema name.
In case the database support synonyms or equivalent notion:
In the case for example of Oracle, the most convenient solution is at the level of the database, by making the tables of one schema visible in another schema via public synonyms.
If there are different schema names per environment:
In that case set the schema names in different orm.xml files, and configure your build process to put only one of those files in the classpath depending on the environment, see this answer.