org.springframework.orm.hibernate5.LocalSessionFactoryBean Java Examples
The following examples show how to use
org.springframework.orm.hibernate5.LocalSessionFactoryBean.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HibernateTransactionManagerConfiguration.java From hypersistence-optimizer with Apache License 2.0 | 9 votes |
@Bean public LocalSessionFactoryBean sessionFactory() { LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean(); localSessionFactoryBean.setDataSource(dataSource()); localSessionFactoryBean.setPackagesToScan(packagesToScan()); localSessionFactoryBean.setHibernateProperties(additionalProperties()); return localSessionFactoryBean; }
Example #2
Source File: InfrastructureConfig.java From Learning-Path-Spring-5-End-to-End-Programming with MIT License | 7 votes |
@Bean public LocalSessionFactoryBean sessionFactory(DataSource dataSource) { LocalSessionFactoryBean sfb = new LocalSessionFactoryBean(); sfb.setDataSource(dataSource); sfb.setPackagesToScan(new String[] { "com.packt.patterninspring.chapter8.bankapp.model" }); Properties props = new Properties(); props.setProperty("dialect", "org.hibernate.dialect.H2Dialect"); sfb.setHibernateProperties(props); return sfb; }
Example #3
Source File: SessionFactoryImpl.java From tephra with MIT License | 6 votes |
private SessionFactory createSessionFactory(Properties properties, DataSource dataSource, String[] packagesToScan) { if (dataSource == null) throw new NullPointerException("数据源不存在,无法初始化Hibernate环境!"); LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); sessionFactoryBean.setHibernateProperties(properties); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setPackagesToScan(packagesToScan); try { sessionFactoryBean.afterPropertiesSet(); return sessionFactoryBean.getObject(); } catch (IOException e) { logger.warn(e, "初始化Hibernate环境[{}]时发生异常!", converter.toString(packagesToScan)); return null; } }
Example #4
Source File: DdlExporter.java From sample-boot-micro with MIT License | 6 votes |
private void outputDdl(String packageName, String dialect, String fileName) { LocalSessionFactoryBean sfBean = sfBean(packageName, dialect); StandardServiceRegistry serviceRegistry = sfBean.getConfiguration().getStandardServiceRegistryBuilder().build(); try { String outputFile = OutputRoot + fileName; Files.deleteIfExists(Paths.get(outputFile)); Metadata metadata = metadata(serviceRegistry, sfBean.getMetadataSources()); SchemaExport export = new SchemaExport(); export.setDelimiter(";"); export.setFormat(FormatSql); export.setOutputFile(outputFile); export.create(EnumSet.of(TargetType.SCRIPT, TargetType.STDOUT), metadata); } catch (Exception e) { throw new InvocationException(e); } finally { StandardServiceRegistryBuilder.destroy(serviceRegistry); } }
Example #5
Source File: DatabaseConfig.java From spring-security-jwt-rest-stateless with MIT License | 6 votes |
@Bean(name = "sessionFactory") public LocalSessionFactoryBean hibernate5SessionFactoryBean(){ LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean(); localSessionFactoryBean.setDataSource(appContext.getBean(HikariDataSource.class)); localSessionFactoryBean.setAnnotatedClasses( AppUser.class ); Properties properties = new Properties(); properties.put("hibernate.dialect","org.hibernate.dialect.MySQLDialect"); //properties.put("hibernate.current_session_context_class","thread"); properties.put("hibernate.hbm2ddl.auto","update"); localSessionFactoryBean.setHibernateProperties(properties); return localSessionFactoryBean; }
Example #6
Source File: DdlExporter.java From sample-boot-hibernate with MIT License | 6 votes |
private void outputDdl(String packageName, String dialect, String fileName) { LocalSessionFactoryBean sfBean = sfBean(packageName, dialect); StandardServiceRegistry serviceRegistry = sfBean.getConfiguration().getStandardServiceRegistryBuilder().build(); try { String outputFile = OutputRoot + fileName; Files.deleteIfExists(Paths.get(outputFile)); Metadata metadata = metadata(serviceRegistry, sfBean.getMetadataSources()); SchemaExport export = new SchemaExport(); export.setDelimiter(";"); export.setFormat(FormatSql); export.setOutputFile(outputFile); export.create(EnumSet.of(TargetType.SCRIPT, TargetType.STDOUT), metadata); } catch (Exception e) { throw new InvocationException(e); } finally { StandardServiceRegistryBuilder.destroy(serviceRegistry); } }
Example #7
Source File: HiberConfig.java From Spring-5.0-Cookbook with MIT License | 5 votes |
@Bean("sessionFactory") public LocalSessionFactoryBean localSessionFactory(DataSource dataSource, Properties hibernateProperties) { LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setPackagesToScan(new String[] { "org.packt.hiber.core.model.data" }); sessionFactory.setHibernateProperties(hibernateProperties); return sessionFactory; }
Example #8
Source File: HiberConfig.java From Spring-5.0-Cookbook with MIT License | 5 votes |
@Bean public HibernateTransactionManager db1TransactionManager(DataSource dataSource,LocalSessionFactoryBean localSessionFactory) { HibernateTransactionManager txManager = new HibernateTransactionManager(); txManager.setSessionFactory(localSessionFactory.getObject()); txManager.setDataSource(dataSource); return txManager; }
Example #9
Source File: SessionFactoryRegistry.java From jeesupport with MIT License | 5 votes |
private SessionFactoryImpl createSessionFactoryBean( String _name, AtomikosDataSourceBean _ds ) { String head = "jees.jdbs.config." + _name + "."; String hibernate = head + "hibernate."; String orm = CommonConfig.getString( head + "orm" ); String bean = _name + "SessionFactory"; BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(LocalSessionFactoryBean.class); if( orm.equalsIgnoreCase("hibernate") ){ Properties hibernateProperties = new Properties(); hibernateProperties.setProperty("hibernate.dialect", CommonConfig.getString( hibernate + "dialect","org.hibernate.dialect.MySQL55Dialect") ); hibernateProperties.setProperty("hibernate.show_sql", CommonConfig.getString( hibernate + "showSql","true" ) ); hibernateProperties.setProperty("hibernate.transaction.factory_class", CommonConfig.getString( hibernate + "factoryClass","org.hibernate.transaction.JTATransactionFactory" ) ); hibernateProperties.setProperty("hibernate.hbm2ddl.auto", CommonConfig.getString( hibernate + "hbm2ddl", "none" ) ); hibernateProperties.setProperty("hibernate.transaction.jta.platform", CommonConfig.getString( hibernate + "platform", "com.jees.core.database.config.AtomikosJtaPlatform" ) ); hibernateProperties.setProperty("hibernate.transaction.coordinator_class", CommonConfig.getString( hibernate + "coordinatorClass","jta" ) ); beanDefinitionBuilder.addPropertyValue("dataSource", _ds); beanDefinitionBuilder.addPropertyValue("packagesToScan", CommonConfig.getString( head + "packagesToScan" ) ); beanDefinitionBuilder.addPropertyValue("hibernateProperties", hibernateProperties); } ConfigurableApplicationContext context = (ConfigurableApplicationContext) CommonContextHolder.getApplicationContext(); DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory(); log.debug("--创建LocalSessionFactoryBean[" + bean + "]。"); beanFactory.registerBeanDefinition(bean, beanDefinitionBuilder.getBeanDefinition()); return CommonContextHolder.getBean( bean ); }
Example #10
Source File: HibernateConfig.java From spring4-hibernate5-example with MIT License | 5 votes |
@Bean public LocalSessionFactoryBean getSessionFactory() { LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(getDataSource()); sessionFactory.setPackagesToScan(new String[] { "com.bytestree.model" }); sessionFactory.setHibernateProperties(getHibernateProperties()); return sessionFactory; }
Example #11
Source File: TransactionManagerHolder.java From framework with Apache License 2.0 | 5 votes |
/** * Description: 获取session 工厂 <br> * * @author 王伟<br> * @taskId <br> * @return <br> * @throws IOException */ public static SessionFactory getSessionFactory() { synchronized (sessionFactoryHolder) { String dbCode = DynamicDataSourceManager.getDataSourceCode(); SessionFactory sessionFactory = sessionFactoryHolder.get(dbCode); if (sessionFactory == null) { DataSource dataSource = DataSourceUtil.getDataSource(dbCode); Assert.notNull(dataSource, ErrorCodeDef.DB_DATASOURCE_NOT_SET, dbCode); LocalSessionFactoryBean bean = new LocalSessionFactoryBean(); bean.setDataSource(dataSource); Map<String, String> map = PropertyHolder.getProperties(); Properties properties = new Properties(); int prefixLength = dbCode.length() + 1; String prefix = dbCode + ".hibernate"; for (Entry<String, String> entry : map.entrySet()) { if (entry.getKey().startsWith(prefix)) { properties.setProperty(entry.getKey().substring(prefixLength, entry.getKey().length()), entry.getValue()); } } bean.setHibernateProperties(properties); bean.setPackagesToScan(getBasePackage()); try { bean.afterPropertiesSet(); } catch (IOException e) { throw new InitializationException(e); } sessionFactory = bean.getObject(); sessionFactoryHolder.put(dbCode, sessionFactory); } return sessionFactory; } }
Example #12
Source File: HibernateTransactionManagerConfiguration.java From high-performance-java-persistence with Apache License 2.0 | 5 votes |
@Bean public LocalSessionFactoryBean sessionFactory() { LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource()); sessionFactoryBean.setPackagesToScan(packagesToScan()); sessionFactoryBean.setHibernateProperties(additionalProperties()); sessionFactoryBean.setHibernateIntegrators(new ClassImportIntegrator(Arrays.asList(PostDTO.class))); return sessionFactoryBean; }
Example #13
Source File: Config.java From spring-data-jpa-datatables with Apache License 2.0 | 5 votes |
@Bean public SessionFactory entityManagerFactory(DataSource dataSource) throws Exception { LocalSessionFactoryBean factory = new LocalSessionFactoryBean(); factory.setPackagesToScan(Config.class.getPackage().getName()); factory.setDataSource(dataSource); factory.afterPropertiesSet(); return factory.getObject(); }
Example #14
Source File: SpringConfig.java From quickperf with Apache License 2.0 | 4 votes |
@Bean public PlatformTransactionManager hibernateTransactionManager(LocalSessionFactoryBean sessionFactory) { HibernateTransactionManager transactionManager = new HibernateTransactionManager(); transactionManager.setSessionFactory(sessionFactory.getObject()); return transactionManager; }