我想在 hibernate 中拥有多个数据库模式。我尝试在单个 Tomcat 应用程序上使用多个 Spring 上下文(每个测试场景一个上下文):
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(LocalBankConfig.class);
Map<String, Object> myProps = new HashMap<>();
myProps.put("simulationId", simulationId);
ctx.getEnvironment().getPropertySources().addFirst(new MapPropertySource("simulation.properties", myProps));
ctx.refresh();
我在业务模型中使用Spring注解:
@PersistenceContext
private EntityManager em;
…
@Transactional
method
但是这个方法似乎行不通。仅注入(inject)最后创建的 String 上下文中的 EntityManager。 欢迎任何关于如何解决这个问题的想法。
最佳答案
您不应该尝试拥有多个持久性上下文。但是您可以通过 @Table 注解轻松管理多个架构。来自javadoc:
Example:
@Entity
@Table(name="CUST", schema="RECORDS")
public class Customer { ... }
关于java - 在单个 Spring 应用程序中增加数据库模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24754954/