org.hibernate.cfg.Configuration.buildSessionFactory()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(12.1k)|赞(0)|评价(0)|浏览(156)

本文整理了Java中org.hibernate.cfg.Configuration.buildSessionFactory()方法的一些代码示例,展示了Configuration.buildSessionFactory()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Configuration.buildSessionFactory()方法的具体详情如下:
包路径:org.hibernate.cfg.Configuration
类名称:Configuration
方法名:buildSessionFactory

Configuration.buildSessionFactory介绍

[英]Create a SessionFactory using the properties and mappings in this configuration. The SessionFactory will be immutable, so changes made to this Configuration after building the SessionFactory will not affect it.
[中]使用此配置中的属性和映射创建SessionFactory。SessionFactory是不可变的,因此在构建SessionFactory后对此配置所做的更改不会影响它。

代码示例

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Create a {@link SessionFactory} using the properties and mappings in this configuration. The
 * {@link SessionFactory} will be immutable, so changes made to {@code this} {@link Configuration} after
 * building the {@link SessionFactory} will not affect it.
 *
 * @return The build {@link SessionFactory}
 *
 * @throws HibernateException usually indicates an invalid configuration or invalid mapping information
 */
public SessionFactory buildSessionFactory() throws HibernateException {
  log.debug( "Building session factory using internal StandardServiceRegistryBuilder" );
  standardServiceRegistryBuilder.applySettings( properties );
  return buildSessionFactory( standardServiceRegistryBuilder.build() );
}

代码示例来源:origin: hibernate/hibernate-orm

protected void buildSessionFactory(Consumer<Configuration> configurationAdapter) {
  // for now, build the configuration to get all the property settings
  configuration = constructAndConfigureConfiguration();
  if ( configurationAdapter != null ) {
    configurationAdapter.accept(configuration);
  }
  BootstrapServiceRegistry bootRegistry = buildBootstrapServiceRegistry();
  serviceRegistry = buildServiceRegistry( bootRegistry, configuration );
  // this is done here because Configuration does not currently support 4.0 xsd
  afterConstructAndConfigureConfiguration( configuration );
  sessionFactory = ( SessionFactoryImplementor ) configuration.buildSessionFactory( serviceRegistry );
  afterSessionFactoryBuilt();
}

代码示例来源:origin: hibernate/hibernate-orm

private SessionFactoryImplementor buildSessionFactory(List<Class<?>> classesUnderTest, List<String> configFiles) {
  assert classesUnderTest != null;
  assert configFiles != null;
  Configuration cfg = new Configuration();
  for ( Class<?> clazz : classesUnderTest ) {
    cfg.addAnnotatedClass( clazz );
  }
  for ( String configFile : configFiles ) {
    try ( InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( configFile ) ) {
      cfg.addInputStream( is );
    }
    catch (IOException e) {
      throw new IllegalArgumentException( e );
    }
  }
  return ( SessionFactoryImplementor ) cfg.buildSessionFactory();
}

代码示例来源:origin: hibernate/hibernate-orm

@Before
public void buildSessionFactory() {
  Configuration cfg = new Configuration()
      .addAnnotatedClass( TheEntity.class );
  cfg.getProperties().put( AvailableSettings.DIALECT, NoTupleSupportDialect.class.getName() );
  cfg.getProperties().put( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
  sessionFactory = cfg.buildSessionFactory();
}

代码示例来源:origin: hibernate/hibernate-orm

@TransactionAttribute(TransactionAttributeType.NEVER)
public void start() {
  try {
    Configuration configuration = new Configuration();
    configuration = configuration.configure( "hibernate.cfg.xml" );
    configuration.addAnnotatedClass( WildFlyDdlEntity.class );
    sessionFactory = configuration.buildSessionFactory();
  }
  catch (Throwable ex) {
    System.err.println( "Initial SessionFactory creation failed." + ex );
    throw new ExceptionInInitializerError( ex );
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testDeclarativeMix() throws Exception {
  Configuration cfg = new Configuration();
  cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
  cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
  SessionFactory sf = cfg.buildSessionFactory();
  assertNotNull( sf );
  Session s = sf.openSession();
  Transaction tx = s.beginTransaction();
  Query q = s.createQuery( "from Boat" );
  assertEquals( 0, q.list().size() );
  q = s.createQuery( "from Plane" );
  assertEquals( 0, q.list().size() );
  tx.commit();
  s.close();
  sf.close();
}
 @Test

代码示例来源:origin: hibernate/hibernate-orm

@BeforeClass
public static void buildSessionFactory() {
  Configuration cfg = new Configuration();
  cfg.setProperty( AvailableSettings.DIALECT, DIALECT.getClass().getName() );
  ServiceRegistry serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() );
  SESSION_FACTORY = (SessionFactoryImplementor) cfg.buildSessionFactory( serviceRegistry );
}

代码示例来源:origin: dropwizard/dropwizard

protected DAOTest buildDAOTest() {
    final Configuration config = new Configuration();
    config.setProperty(AvailableSettings.URL, url);
    config.setProperty(AvailableSettings.USER, username);
    config.setProperty(AvailableSettings.PASS, password);
    config.setProperty(AvailableSettings.DRIVER, driver);
    config.setProperty(AvailableSettings.HBM2DDL_AUTO, hbm2ddlAuto);
    config.setProperty(AvailableSettings.SHOW_SQL, String.valueOf(showSql));
    config.setProperty(AvailableSettings.USE_SQL_COMMENTS, String.valueOf(useSqlComments));
    // Use the same configuration as in the Hibernate bundle to reduce differences between
    // testing and production environments.
    config.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "managed");
    config.setProperty(AvailableSettings.USE_GET_GENERATED_KEYS, "true");
    config.setProperty(AvailableSettings.GENERATE_STATISTICS, "true");
    config.setProperty(AvailableSettings.USE_REFLECTION_OPTIMIZER, "true");
    config.setProperty(AvailableSettings.ORDER_UPDATES, "true");
    config.setProperty(AvailableSettings.ORDER_INSERTS, "true");
    config.setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
    config.setProperty("jadira.usertype.autoRegisterUserTypes", "true");
    entityClasses.forEach(config::addAnnotatedClass);
    properties.forEach(config::setProperty);
    configurationCustomizer.accept(config);
    return new DAOTest(config.buildSessionFactory());
  }
}

代码示例来源:origin: hibernate/hibernate-orm

public void start() {
  Configuration configuration = new Configuration();
  configuration = configuration.configure( "hibernate.cfg.xml" );
  configuration.addAnnotatedClass( WildFlyDdlEntity.class );
  // creating the SF should run schema creation
  sessionFactory = configuration.buildSessionFactory();
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testHbmWithSubclassExtends() throws Exception {
  Configuration cfg = new Configuration();
  cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
  cfg.addClass( Ferry.class );
  cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
  SessionFactory sf = cfg.buildSessionFactory();
  assertNotNull( sf );
  Session s = sf.openSession();
  Transaction tx = s.beginTransaction();
  Query q = s.createQuery( "from Ferry" );
  assertEquals( 0, q.list().size() );
  q = s.createQuery( "from Plane" );
  assertEquals( 0, q.list().size() );
  tx.commit();
  s.close();
  sf.close();
}
 @Test

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testQuery() {
  Configuration cfg = new Configuration();		
  cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
  cfg.addInputStream(new ReaderInputStream(new StringReader(NAMED_QUERY_HBM_XML)));
  SessionFactory sessionFactory = cfg.buildSessionFactory();
  sessionFactory.close();
}

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Tests that nothing crazy happens if the hibernate-envers jar happens
 * to be on the classpath but we have no audited entities
 */
@Test
@TestForIssue( jiraKey = "HHH-9675" )
public void testNoAudited() {
  new Configuration().buildSessionFactory().close();
  new Configuration().addAnnotatedClass( SimpleNonAuditedEntity.class ).buildSessionFactory().close();
}

代码示例来源:origin: dropwizard/dropwizard

private SessionFactory buildSessionFactory(HibernateBundle<?> bundle,
                      PooledDataSourceFactory dbConfig,
                      ConnectionProvider connectionProvider,
                      Map<String, String> properties,
                      List<Class<?>> entities) {
  final Configuration configuration = new Configuration();
  configuration.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "managed");
  configuration.setProperty(AvailableSettings.USE_SQL_COMMENTS, Boolean.toString(dbConfig.isAutoCommentsEnabled()));
  configuration.setProperty(AvailableSettings.USE_GET_GENERATED_KEYS, "true");
  configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, "true");
  configuration.setProperty(AvailableSettings.USE_REFLECTION_OPTIMIZER, "true");
  configuration.setProperty(AvailableSettings.ORDER_UPDATES, "true");
  configuration.setProperty(AvailableSettings.ORDER_INSERTS, "true");
  configuration.setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
  configuration.setProperty("jadira.usertype.autoRegisterUserTypes", "true");
  for (Map.Entry<String, String> property : properties.entrySet()) {
    configuration.setProperty(property.getKey(), property.getValue());
  }
  addAnnotatedClasses(configuration, entities);
  bundle.configure(configuration);
  final ServiceRegistry registry = new StandardServiceRegistryBuilder()
      .addService(ConnectionProvider.class, connectionProvider)
      .applySettings(configuration.getProperties())
      .build();
  configure(configuration, registry);
  return configuration.buildSessionFactory(registry);
}

代码示例来源:origin: hibernate/hibernate-orm

@BeforeClassOnce
public void init() throws URISyntaxException {
  config = new Configuration();
  URL url = Thread.currentThread().getContextClassLoader().getResource( getHibernateConfigurationFileName() );
  config.configure( new File( url.toURI() ) );
  String auditStrategy = getAuditStrategy();
  if ( auditStrategy != null && !"".equals( auditStrategy ) ) {
    config.setProperty( EnversSettings.AUDIT_STRATEGY, auditStrategy );
  }
  config.setProperty( Environment.USE_NEW_ID_GENERATOR_MAPPINGS, "true" );
  config.setProperty( EnversSettings.USE_REVISION_ENTITY_WITH_NATIVE_ID, "false" );
  addProperties( config );
  this.initMappings();
  serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( config.getProperties() );
  sessionFactory = config.buildSessionFactory( serviceRegistry );
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testFirstTypeThenEntity(){
  cfg.addResource("org/hibernate/test/mapping/usertypes/TestEnumType.hbm.xml")
    .addResource("org/hibernate/test/mapping/usertypes/TestEntity.hbm.xml");
  SessionFactory sessions=cfg.buildSessionFactory(serviceRegistry);
  Assert.assertNotNull(sessions);
  sessions.close();
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void testFirstEntityThenType(){
  cfg.addResource("org/hibernate/test/mapping/usertypes/TestEntity.hbm.xml")
    .addResource("org/hibernate/test/mapping/usertypes/TestEnumType.hbm.xml");
  
  SessionFactory sessions=cfg.buildSessionFactory(serviceRegistry);
  Assert.assertNotNull(sessions);
  sessions.close();
}

代码示例来源:origin: hibernate/hibernate-orm

@Before
public void beforeTest() {
  Configuration cfg = new Configuration()
      .addAnnotatedClass( Item.class )
      .setProperty( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
  cfg.addAuxiliaryDatabaseObject( ProcedureDefinitions.INSTANCE );
  sf = cfg.buildSessionFactory();
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
  @TestForIssue( jiraKey = "HHH-8167" )
  public void testAnnotatedFormWithBeanValidationNotNull() {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass( AnnotatedMaster.class ).addAnnotatedClass( AnnotatedDetail.class );
    cfg.buildSessionFactory().close();
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
  @TestForIssue( jiraKey = "HHH-7446" )
  public void testUniqueConstraintAnnotationOnNaturalIds() throws Exception {
    Configuration configuration = new Configuration();
    configuration.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
    configuration.addAnnotatedClass(Month.class);
    SessionFactory sf = null;
    try {
      sf = configuration.buildSessionFactory();
      sf.close();
    }
    catch (ConcurrentModificationException e) {
      fail(e.toString()); 
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
  public void testInitialization() throws Exception {
    Configuration configuration = new Configuration();
    configuration.addAnnotatedClass( A.class );
    configuration.addAnnotatedClass( B.class );
    configuration.addAnnotatedClass( C.class );
    StandardServiceRegistryImpl serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( configuration.getProperties() );
    try {
      SessionFactory sessionFactory = configuration.buildSessionFactory( serviceRegistry );
      sessionFactory.close();
    }
    finally {
      serviceRegistry.destroy();
    }
  }
}

相关文章

Configuration类方法