Exemple #1
0
 @Bean
 public SessionFactory sessionFactory() {
   AnnotationSessionFactoryBean asFactoryBean = new AnnotationSessionFactoryBean();
   asFactoryBean.setDataSource(dataSource());
   // additional config
   return asFactoryBean.getObject();
 }
    /*@Bean
    		EmbeddedDatabaseFactoryBean embededDataSource(){
    			EmbeddedDatabaseFactoryBean databaseFactoryBean = new EmbeddedDatabaseFactoryBean();
    			//注意需要在类路径中加入 h2数据库的 jar包哦
    			databaseFactoryBean.setDatabaseType(EmbeddedDatabaseType.H2);

    			return databaseFactoryBean;
    		}
    */
    @Bean
    AnnotationSessionFactoryBean sessionFactoryBean(/*DataSource dataSource*/ ) {
      AnnotationSessionFactoryBean factoryBean = new AnnotationSessionFactoryBean();

      /*Properties hibernateProperties = new Properties();
      ClassPathResource resource = new ClassPathResource("hibernate.properties");
      try {
      	hibernateProperties.load(resource.getInputStream());
      } catch (IOException e) {
      	throw new IllegalStateException("Read File hibernate.properties failed");
      }*/

      // using the utility class(PropertiesLoaderUtils) in spring!!!
      Properties hibernateProperties = new Properties();
      try {
        PropertiesLoaderUtils.loadProperties(new ClassPathResource("hibernate.properties"));
      } catch (IOException e) {
        throw new IllegalStateException("Read File hibernate.properties failed", e);
      }

      //			factoryBean.setDataSource(dataSource);
      //			factoryBean.setDataSource(c3p0DataSource());
      factoryBean.setDataSource(dataSource());

      factoryBean.setPackagesToScan(new String[] {"cjava.walker.entity"});
      factoryBean.setHibernateProperties(hibernateProperties);

      return factoryBean;
    }
 @Bean
 public AnnotationSessionFactoryBean sessionFactory() {
   AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
   sessionFactoryBean.setPackagesToScan(new String[] {"model"});
   sessionFactoryBean.setDataSource(dataSource());
   sessionFactoryBean.setHibernateProperties(hibernateProperties());
   return sessionFactoryBean;
 }
 private SessionFactory createTestSessionFactory() throws Exception {
   // create a FactoryBean to help create a Hibernate SessionFactory
   AnnotationSessionFactoryBean factoryBean = new AnnotationSessionFactoryBean();
   factoryBean.setDataSource(createTestDataSource());
   factoryBean.setAnnotatedClasses(new Class[] {Account.class, Beneficiary.class});
   factoryBean.setHibernateProperties(createHibernateProperties());
   // initialize according to the Spring InitializingBean contract
   factoryBean.afterPropertiesSet();
   // get the created session factory
   return (SessionFactory) factoryBean.getObject();
 }
Exemple #5
0
 @Bean
 public SessionFactory sessionFactory() throws Exception {
   // Hibernate 3 use this class but 4 use LocalSessionFactoryBean
   AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
   sessionFactory.setDataSource(dataSource());
   Properties properties = new Properties();
   properties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
   properties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
   properties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
   properties.setProperty("hibernate.format_sql", env.getProperty("hibernate.format_sql"));
   sessionFactory.setHibernateProperties(properties);
   sessionFactory.setPackagesToScan(new String[] {"com.demo.curd.entity"});
   sessionFactory
       .afterPropertiesSet(); // if don't call this method you will can't get right SessionFactory
                              // object
   return sessionFactory.getObject();
 }