您可以使用单独的SessionFactorys 或EntityManagerFactorys,每个模式一个。既然您说用户选择模式 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);
}
}
Run Code Online (Sandbox Code Playgroud)