@Bean public AnnotationSessionFactoryBean sessionFactoryBean() { ... AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean(); bean.setAnnotatedClasses(new Class[]{Region.class}); ... return bean; }
AnnotationSessionFactoryBean lsfb = new AnnotationSessionFactoryBean(); Class [] annotatedClasses = {Student.class}; lsfb.setAnnotatedClasses(annotatedClasses);
AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean(); //set annotated classes. sessionFactoryBean.setAnnotatedClasses(new Class<?>[]{VO1.class,VO2.class}); Properties props = new Properties(); //put all your hibernate configurations here props.setProperty("dataSource.show_sql", "true"); props.setProperty("dataSource.dialect", "org.hibernate.dialect.MySQL5Dialect"); .... sessionFactoryBean.setHibernateProperties(props); //Create DataSource Dynamically BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(...); dataSource.setUrl(...); dataSource.setUsername(...); dataSource.setPassword(...); sessionFactoryBean.setDataSource(ds); //tell sessionFactoryBean that you are ready. sessionFactoryBean.afterPropertiesSet(); SessionFactory sessionFactory = sessionFactoryBean.getObject(); HibernateTemplate template = new HibernateTemplate(); template.setSessionFactory(sessionFactory);
@Bean public LocalSessionFactoryBean sessionFactoryBean() { AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource()); sessionFactoryBean.setAnnotatedClasses(Item.class, PaymentInfo.class, PurchaseOrder.class); Properties prop = new Properties(); prop.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect"); sessionFactoryBean.setHibernateProperties(prop); return sessionFactoryBean; }
public WarningRecordRepository(String jdbcDriver, String jdbcConnection, String jdbcUser, String jdbcPassword, String hibernateDialect) { try { Class.forName(jdbcDriver); } catch (ClassNotFoundException e) { throw new RuntimeException("Cannot load specified JDBC driver: " + jdbcDriver, e); } DriverManagerDataSource dataSource=new DriverManagerDataSource(jdbcConnection, jdbcUser, jdbcPassword); AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); Properties config = new Properties(); config.setProperty("hibernate.dialect", hibernateDialect); config.setProperty("hibernate.connection.autocommit", "true"); config.setProperty("hibernate.hbm2ddl.auto", "update"); sessionFactoryBean.setHibernateProperties(config); sessionFactoryBean.setAnnotatedClasses(new Class<?>[]{WarningRecord.class}); try { sessionFactoryBean.afterPropertiesSet(); } catch (Exception e) { throw new RuntimeException("Could not set up database connection", e); } hibernateTemplate = new HibernateTemplate((SessionFactory) sessionFactoryBean.getObject()); }