コード例 #1
0
 private SessionFactory createTestSessionFactory() throws Exception {
   // create a FactoryBean to help create a Hibernate SessionFactory
   LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
   factoryBean.setDataSource(createTestDataSource());
   Resource[] mappingLocations =
       new ClassPathResource[] {
         new ClassPathResource("Account.hbm.xml", Account.class),
         new ClassPathResource("Beneficiary.hbm.xml", Beneficiary.class)
       };
   factoryBean.setMappingLocations(mappingLocations);
   factoryBean.setHibernateProperties(createHibernateProperties());
   // initialize according to the Spring InitializingBean contract
   factoryBean.afterPropertiesSet();
   // get the created session factory
   return (SessionFactory) factoryBean.getObject();
 }
コード例 #2
0
  public void testLocalSessionFactoryBeanWithDataSourceAndProperties() throws Exception {
    final DriverManagerDataSource ds = new DriverManagerDataSource();
    final Set invocations = new HashSet();
    LocalSessionFactoryBean sfb =
        new LocalSessionFactoryBean() {
          protected Configuration newConfiguration() {
            return new Configuration() {
              public Configuration addInputStream(InputStream is) {
                try {
                  is.close();
                } catch (IOException ex) {
                }
                invocations.add("addResource");
                return this;
              }
            };
          }

          protected SessionFactory newSessionFactory(Configuration config) {
            assertEquals(
                LocalDataSourceConnectionProvider.class.getName(),
                config.getProperty(Environment.CONNECTION_PROVIDER));
            assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource());
            assertEquals("myValue", config.getProperty("myProperty"));
            invocations.add("newSessionFactory");
            return null;
          }
        };
    sfb.setMappingLocations(
        new Resource[] {new ClassPathResource("/org/springframework/beans/factory/xml/test.xml")});
    sfb.setDataSource(ds);
    Properties prop = new Properties();
    prop.setProperty(Environment.CONNECTION_PROVIDER, "myClass");
    prop.setProperty("myProperty", "myValue");
    sfb.setHibernateProperties(prop);
    sfb.afterPropertiesSet();
    assertTrue(sfb.getConfiguration() != null);
    assertTrue(invocations.contains("addResource"));
    assertTrue(invocations.contains("newSessionFactory"));
  }