JPA / Hibernate transition scheme programmatically

I am currently working on a new project that has the following requirement:

Several database schemas contain the same tables with the same structure (in short: one entity for multiple schemas).

Can I switch between these circuits by code? I want to achieve:

The user selects circuit B and updates some of the objects in this. After that, he inserts into circuit A and so on. I know that I could do this using basic JDBC by providing the schema to the operators, but if I could avoid it, I would do it.

Maybe some other Java ORM can do this? I am familiar with JPA / Hibernate.

Hi

+5
source share
1

SessionFactory EntityManagerFactory s, . , A B, - :

public enum Schema {
    A, B
}

public EntityDaoImpl {

    // Create and populate the map at DAO creation time (Spring etc.).
    private Map<Schema, SessionFactory> sessionFactoryBySchema = ...; 

    private Session getSession(Schema schema) {
        SessionFactory sessionFactory = sessionFactoryBySchema.get(schema);
        return sessionFactory.getCurrentSession(); // ... or whatever
    }

    public void saveEntity(Schema schema, Entity entity) {
        getSession(schema).save(entity);
    }
}
+2

All Articles