public ConfiguredOrmPersistenceUnit<SessionFactory, Configuration> getConfiguredOrmPersistenceUnit(Object testObject, OrmConfig entityManagerConfig) { LocalSessionFactoryBean factoryBean = createSessionFactoryBean(testObject, entityManagerConfig); SessionFactory entityManagerFactory = (SessionFactory) factoryBean.getObject(); Configuration hibernateConfiguration = factoryBean.getConfiguration(); return new ConfiguredOrmPersistenceUnit<SessionFactory, Configuration>(entityManagerFactory, hibernateConfiguration); }
@Inject DataSource datasource; @Bean @SuppressWarnings("deprecation") public SessionFactory sessionFactory() throws IOException{ LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); sessionFactoryBean.setPackagesToScan("nl.your.model"); sessionFactoryBean.setHibernateProperties(hibernateProperties()); sessionFactoryBean.setDataSource(datasource); sessionFactoryBean.afterPropertiesSet(); return sessionFactoryBean.getObject(); }
@Bean public LocalSessionFactoryBean sessionFactoryBean() { LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource()); sessionFactoryBean.setMappingResources("hibernate/Item.hbm.xml", "hibernate/Order.hbm.xml"); Properties prop = new Properties(); prop.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect"); sessionFactoryBean.setHibernateProperties(prop); return sessionFactoryBean; }
protected SessionFactory buildSessionFactory() throws Exception { Configuration config = newConfiguration(); DataSource dataSource = getDataSource(); if (dataSource != null) { if (isExposeTransactionAwareSessionFactory()) { if (isUseTransactionAwareDataSource() || dataSource instanceof TransactionAwareDataSourceProxy) { providerClass = TransactionAwareDataSourceConnectionProvider.class; postProcessMappings(config); config.buildMappings(); postProcessConfiguration(config); return newSessionFactory(config);
@Configuration public class BeanConfig { @Autowired private LocalSessionFactoryBean sessionFactoryBean; @Bean(name="myBeanId") public MyBeanClass createMyBeanClass() { MyBeanClass mbc = new MyBeanClass(); mbc.setSessionFactory((SessionFactory) sessionFactoryBean.getObject()); .... return mbc; } }
public void setUp() throws Exception { hibernateTemplate = new HibernateTemplate(); LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.getHibernateProperties().put("hibernate.dialect", TEST_DIALECT); sessionFactory.getHibernateProperties().put("hibernate.connection.driver_class", TEST_DRIVER); sessionFactory.getHibernateProperties().put("hibernate.connection.password", TEST_PASSWORD); sessionFactory.getHibernateProperties().put("hibernate.connection.url", TEST_URL); sessionFactory.getHibernateProperties().put("hibernate.connection.username", TEST_USER); sessionFactory.getHibernateProperties().put("hibernate.hbm2ddl.auto", "create"); sessionFactory.getHibernateProperties().put("hibernate.show_sql", "true"); sessionFactory.getHibernateProperties().put("hibernate.jdbc.batch_size", "0"); sessionFactory.getHibernateProperties().put("hibernate.cache.use_second_level_cache", "false"); sessionFactory.setMappingDirectoryLocations(new Resource[]{new ClassPathResource("database")}); sessionFactory.afterPropertiesSet(); hibernateTemplate.setSessionFactory(sessionFactory.getObject()); transactionTemplate = new TransactionTemplate(new HibernateTransactionManager(sessionFactory.getObject()));
@Autowired private LocalSessionFactoryBean localSessionFactoryBean; @Bean public DataSource getDataSource() { //return the data source here } @Bean(name = "hibernateConfig") public org.hibernate.cfg.Configuration getConfig() { return localSessionFactoryBean.getConfiguration(); }
@Override public Object doInHibernate(Session session) throws HibernateException, SQLException { Connection con = session.connection(); String[] sql = getConfiguration().generateDropSchemaScript(dialect); executeSchemaScript(con, sql); return null; } }
Configuration conf = super.newConfiguration(); log.trace("Starting SPI registry based configuration"); if (preloadRegistries != null) {
@Override public void afterPropertiesSet() throws Exception { Thread thread = Thread.currentThread(); ClassLoader cl = thread.getContextClassLoader(); try { thread.setContextClassLoader(classLoader); super.afterPropertiesSet(); } finally { thread.setContextClassLoader(cl); } }
/** * To be implemented by subclasses that want to to perform custom * post-processing of the Configuration object after this FactoryBean * performed its default initialization. * * @param config the current Configuration object * @throws org.hibernate.HibernateException * in case of Hibernate initialization errors */ protected void postProcessConfiguration(Configuration config) throws HibernateException { super.postProcessConfiguration(config); Map beanMap = applicationContext.getBeansOfType(AdditionalHibernateMappings.class, true, true); if (beanMap == null) { return; } Collection beans = beanMap.values(); try { for (Iterator i = beans.iterator(); i.hasNext();) { AdditionalHibernateMappings mappings = (AdditionalHibernateMappings) i.next(); mappings.processConfig(config); } } catch (IOException e) { logger.error("", e); throw new OspException(e); } }
try { SessionFactory sf = super.newSessionFactory(configuration);
@Bean public PurchaseOrderRepository purchaseOrderRepository() { HibernatePurchaseOrderRepository purchaseOrderRepository = new HibernatePurchaseOrderRepository(); purchaseOrderRepository.setSessionFactory(sessionFactoryBean().getObject()); return purchaseOrderRepository; } }
@Autowired private LocalSessionFactoryBean localSessionFactoryBean; @Bean(name = "hibernateConfig") public org.hibernate.cfg.Configuration getConfig() { return localSessionFactoryBean.getConfiguration(); }
@Override public Object doInHibernate(Session session) throws HibernateException, SQLException { Connection con = session.connection(); String[] sql = getConfiguration().generateSchemaCreationScript(dialect); executeSchemaScript(con, sql); return null; } }
Configuration conf = super.newConfiguration(); log.trace("Starting SPI registry based configuration"); if (preloadRegistries != null) {
properties .put("dynamic-update","true"); LocalSessionFactoryBean factory = new LocalSessionFactoryBean(); factory.setPackagesToScan(new String[] { Constants.DOMAIN_MODEL_PACKAGE }); factory.setDataSource(dataSource); factory.setHibernateProperties(properties); factory.afterPropertiesSet(); return factory.getObject();
@Autowired private LocalSessionFactoryBean sessionFactoryBean; @PostConstruct public void init() throws ClassNotFoundException { SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) sessionFactoryBean.getObject(); SessionFactoryServiceRegistry serviceRegistry = (SessionFactoryServiceRegistry)sessionFactory.getServiceRegistry(); Configuration configuration = sessionFactoryBean.getConfiguration(); new JpaIntegrator().integrate(configuration, sessionFactory, serviceRegistry); }
@Bean public PlatformTransactionManager transactionManager() { HibernateTransactionManager txMgr = new HibernateTransactionManager(); txMgr.setSessionFactory(sessionFactoryBean().getObject()); return txMgr; }
@Autowired public HibernateAwareObjectMapper(LocalSessionFactoryBean sessionFactoryBean) { Hibernate4Module hibernate4Module = new Hibernate4Module(sessionFactoryBean.getConfiguration().buildMapping()); hibernate4Module.configure(Hibernate4Module.Feature.SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS, true); registerModule(hibernate4Module); }