本文整理汇总了Java中org.hibernate.service.ServiceRegistryBuilder类的典型用法代码示例。如果您正苦于以下问题:Java ServiceRegistryBuilder类的具体用法?Java ServiceRegistryBuilder怎么用?Java ServiceRegistryBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ServiceRegistryBuilder类属于org.hibernate.service包,在下文中一共展示了ServiceRegistryBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createSessionFactory
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
/**
* Configuration of session factory with Fabric integration.
*/
public static SessionFactory createSessionFactory(String fabricUrl, String username, String password, String fabricUser, String fabricPassword)
throws Exception {
// creating this here allows passing needed params to the constructor
FabricMultiTenantConnectionProvider connProvider = new FabricMultiTenantConnectionProvider(fabricUrl, "employees", "employees", username, password,
fabricUser, fabricPassword);
ServiceRegistryBuilder srb = new ServiceRegistryBuilder();
srb.addService(org.hibernate.service.jdbc.connections.spi.MultiTenantConnectionProvider.class, connProvider);
srb.applySetting("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");
Configuration config = new Configuration();
config.setProperty("hibernate.multiTenancy", "DATABASE");
config.addResource("com/mysql/fabric/demo/employee.hbm.xml");
return config.buildSessionFactory(srb.buildServiceRegistry());
}
示例2: saveByOpenSession
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
public void saveByOpenSession(T t) {
//读取配置文件
configuration = new Configuration().configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = null;
try {
session = sessionFactory.openSession();
//开启事务
session.beginTransaction();
session.save(t);
//提交事务
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
} finally {
if (session != null) {
if (session.isOpen()) {
//关闭session
session.close();
}
}
}
}
示例3: init
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
/**
* Inits the.
*/
private static void init() {
//Change the path to your deployed config file !
File configFile = new File("/Users/matthiasivantsits/git/AnSoMia/AnSoMnia/src/hibernate.cfg.xml");
//File configFile = new File("/Users/matthiasivantsits/git/AnSoMia/AnSoMnia/src/hibernate.cfg.xml");
Configuration configuration = new Configuration();
//add all classes you want to annotate
configuration.addAnnotatedClass(Index.class);
configuration.addAnnotatedClass(Company.class);
configuration.addAnnotatedClass(MarketValue.class);
configuration.addAnnotatedClass(KeyPerformanceIndicator.class);
configuration.addAnnotatedClass(News.class);
configuration.addAnnotatedClass(NewsDetail.class);
configuration.addAnnotatedClass(SentenceInformation.class);
configuration.addAnnotatedClass(IndustrySector.class);
configuration.addAnnotatedClass(CompanyInformation.class);
configuration.addAnnotatedClass(EntityInformation.class);
configuration.configure(configFile);
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
示例4: main
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
// set up a Hibernate Session
final Configuration configuration = new Configuration();
configuration.addAnnotatedClass(PersonNst.class);
configuration.configure();
final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
final SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
final Session session = sessionFactory.openSession();
session.beginTransaction();
new HibernateSessionExample().run(new DbSessionHibernateImpl(session));
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
示例5: main
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
// set up a Hibernate Session
final Configuration configuration = new Configuration();
configuration.addAnnotatedClass(PersonCtt.class);
configuration.addAnnotatedClass(PersonOrganisationalTreePath.class);
configuration.addAnnotatedClass(PersonFunctionalTreePath.class);
configuration.configure();
final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
final SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
final Session session = sessionFactory.openSession();
session.beginTransaction();
new HibernateSessionExample().run(new DbSessionHibernateImpl(session));
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
示例6: rebuildSessionFactory
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
/**
* Abstract: Build SessionFactory object
*/
public static void rebuildSessionFactory() {
try {
// Initial application using configuration file
configuration = new Configuration();
configuration.configure(configFile);
// Create SessionFactory object according to the configuration
// Data model can be created in MySQL automatically after execute
// this method
ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder();
ServiceRegistry serviceRegistry = serviceRegistryBuilder.applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.configure().buildSessionFactory(serviceRegistry);
} catch (Exception e) {
e.printStackTrace();
}
}
示例7: setUpBeforeClass
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
@BeforeClass
public static void setUpBeforeClass() throws Exception {
// service
commonServiceImpl = new CommonServiceImpl();
// dao
commonDaoImpl = new CommonDaoImpl();
// 建構HibernateTemplate,因HibernateDaoSupporter需要
HibernateTemplate hibernateTemplate = new HibernateTemplate();
Configuration config = new Configuration().configure("hibernate.cfg.xml");
// SessionFactory sessionFactory = config.buildSessionFactory();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
.buildServiceRegistry();
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
hibernateTemplate.setSessionFactory(sessionFactory);
commonDaoImpl.setHibernateTemplate(hibernateTemplate);
//
commonServiceImpl.setCommonDao(commonDaoImpl);
commonServiceImpl.start();
}
示例8: openSession
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
@Test
public void openSession() throws Exception {
Configuration config = new Configuration().configure("hibernate.cfg.xml");
// SessionFactory sessionFactory = config.buildSessionFactory();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
.buildServiceRegistry();
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
System.out.println("sessionFactory: " + sessionFactory);
Session session = sessionFactory.openSession();
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
System.out.println("connection: " + connection);
System.out.println("getAutoCommit: " + connection.getAutoCommit());
System.out.println("getTransactionIsolation: " + connection.getTransactionIsolation());
}
});
}
示例9: obtainSessionFactory
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
/**
* 生成一个新的SessionFactory,将模块的的model解析.
* 这里将来可以做成从资源配置文件中动态解析生成.
* @param entityClasses
* @return
*/
private SessionFactory obtainSessionFactory(Collection<Class> entityClasses){
LocalSessionFactoryBean localSessionFactoryBean =(LocalSessionFactoryBean) SpringContextUtil.getBean("&defaultSessionFactory");
Configuration configuration = localSessionFactoryBean.getConfiguration();
synchronized(configuration){//避免并发操作导致configuration重复添加相同的entityClass
for(Class entityClass:entityClasses){
if(configuration.getClassMapping(entityClass.getName())==null){
configuration.addAnnotatedClass(entityClass);
}
}
}
Thread.currentThread().setContextClassLoader(this);//设置当前的Classloader不然会报找不到类的错误
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
SessionFactory newSessionFactory = configuration.buildSessionFactory(serviceRegistry);
log.info("为模块中{}个类重新生成一个新的SessionFactory",entityClasses.size());
Map<String,SessionFactory> sessionFactoryMap =(Map<String,SessionFactory>) SpringContextUtil.getBean("sessionFactoryMap");
sessionFactoryMap.put(getModulesBean().getName(),newSessionFactory);
return newSessionFactory;
}
示例10: DNEHibernateSessionFactory
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
public DNEHibernateSessionFactory(String configFile) {
if ((configFile == null) || (configFile.equals(""))) {
LOG.info("Inicializando a DNE sem conexão com o banco de dados.");
this.customFactory = null;
} else {
try {
LOG.debug("Carregando DNE no Hibernate pelo arquivo: "+configFile);
Configuration config = new Configuration();
config
.configure("dne.hibernate.mappings.xml")
.configure(configFile)
;
ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder()
.applySettings(config.getProperties());
this.customFactory = config.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
} catch (Exception ex) {
LOG.error("Um erro ocorreu ao tentar estabelecer a conexão com a DNE pelo arquivo: "
+configFile,ex);
LOG.info("Inicializando a DNE sem conexão com o banco de dados devido à erros.");
this.customFactory = null;
}
}
}
示例11: buildSessionFactory
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
public static synchronized void buildSessionFactory(String configFile_)
throws HibernateException
{
try {
File configFile = new File(configFile_);
// Create the SessionFactory from hibernate.cfg.xml
Configuration config = new Configuration();
ServiceRegistry serviceRegistry;
MetadataSources metadataSources;
config.configure(configFile);
serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
_sessionFactory = config.buildSessionFactory(serviceRegistry);
} catch(HibernateException e) {
throw e;
}
}
示例12: buildSessionFactory
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
/**
* Connect to the database and create a sessionFactory object
*
* @return the newly created session factory
*/
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
// avoid slow startup: http://stackoverflow.com/questions/10075081/hibernate-slow-to-acquire-postgres-connection
// configuration.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
return configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
示例13: init
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
/**
* Initializes the hibernate system.
*/
public static void init() {
logger.debug("Initializing hibernate...");
Configuration configuration = new Configuration().configure();
logger.debug("Performing schema update");
new SchemaUpdate(configuration).execute(true, true);
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
logger.debug("Hibernate initialized");
}
示例14: start
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
@Override
public void start( ) throws StartException {
try {
File file = new File("/");
File[] files = file.listFiles();
for ( File f : files ) {
logger.debug(f.getAbsolutePath());
}
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
factory = configuration.buildSessionFactory(serviceRegistry);
}
catch ( HibernateException ex ) {
throw new StartException("Exception while starting hibernate", ex);
}
}
示例15: configureSessionFactory
import org.hibernate.service.ServiceRegistryBuilder; //导入依赖的package包/类
private static SessionFactory configureSessionFactory()
throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
Properties properties = configuration.getProperties();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
applySettings(properties).buildServiceRegistry();
SessionFactory sessionFactory = configuration.
buildSessionFactory(serviceRegistry);
return sessionFactory;
}