这很奇怪。根据来源
HibernateProvider#createEntityManagerFactory
,该方法返回一个新实例:
public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
Ejb3Configuration cfg = new Ejb3Configuration();
Ejb3Configuration configured = cfg.configure( persistenceUnitName, properties );
return configured != null ? configured.buildEntityManagerFactory() : null;
}
在这个虚拟测试中,我肯定不会得到相同的实例:
@Test
public void testCreateTwoDifferentEMF() {
Map properties1 = new HashMap();
EntityManagerFactory emf1 = Persistence.createEntityManagerFactory("MyPu", properties1);
Map properties2 = new HashMap();
properties2.put("javax.persistence.jdbc.user", "foo");
EntityManagerFactory emf2 = Persistence.createEntityManagerFactory("MyPu", properties2);
assertFalse(emf1 == emf2); //passes
}
实际上,它只是起作用(第二个实例使用被重写的属性)。