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

x33g5p2x  于2022-01-25 转载在 其他  
字(11.9k)|赞(0)|评价(0)|浏览(154)

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

Metadata.buildSessionFactory介绍

[英]Short-hand form of building a org.hibernate.SessionFactory through the builder without any additional option overrides.
[中]

代码示例

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

public static SessionFactoryImplementor buildStandardSessionFactory(Consumer<StandardServiceRegistryBuilder> additionalSettings) {
  final StandardServiceRegistryBuilder ssrb = getStandardServiceRegistryBuilder();
  additionalSettings.accept( ssrb );
  final StandardServiceRegistry ssr = ssrb.build();
  return (SessionFactoryImplementor) new MetadataSources( ssr ).buildMetadata().buildSessionFactory();
}

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

public static SessionFactoryImplementor buildStandardSessionFactory(Consumer<StandardServiceRegistryBuilder> additionalSettings) {
  final StandardServiceRegistryBuilder ssrb = getStandardServiceRegistryBuilder();
  additionalSettings.accept( ssrb );
  final StandardServiceRegistry ssr = ssrb.build();
  return (SessionFactoryImplementor) new MetadataSources( ssr ).buildMetadata().buildSessionFactory();
}

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

private SessionFactoryImplementor buildSessionFactory(Class entityClass, boolean strict) {
  final Map settings = new HashMap();
  settings.put( AvailableSettings.USE_SECOND_LEVEL_CACHE, "true" );
  settings.put( AvailableSettings.JPA_SHARED_CACHE_MODE, SharedCacheMode.ENABLE_SELECTIVE );
  settings.put( AvailableSettings.GENERATE_STATISTICS, "true" );
  settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
  if ( strict ) {
    settings.put( AvailableSettings.JPA_CACHING_COMPLIANCE, "true" );
  }
  final StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
      .applySettings( settings );
  return (SessionFactoryImplementor) new MetadataSources( ssrb.build() )
      .addAnnotatedClass( Person.class )
      .addAnnotatedClass( VersionedPerson.class)
      .buildMetadata()
      .buildSessionFactory();
}

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

private SessionFactory buildSessionFactory() {
  Map settings = new HashMap();
  settings.putAll( Environment.getProperties() );
  TestingJtaBootstrap.prepare( settings );
  settings.put( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" );
  final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySettings( settings ).build();
  return new MetadataSources( ssr )
      .addAnnotatedClass( TestEntity.class )
      .buildMetadata()
      .buildSessionFactory();
}

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

@Test
public void testIt() {
  new MetadataSources().addAnnotatedClass( A.class ).buildMetadata().buildSessionFactory().close();
}

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

private SessionFactoryImplementor generateSessionFactory() {
  final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
      // should be the default, but lets be specific about which we want to test
      .applySetting( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jdbc" )
      .build();
  try {
    return (SessionFactoryImplementor) new MetadataSources( ssr ).buildMetadata().buildSessionFactory();
  }
  catch (Exception e) {
    StandardServiceRegistryBuilder.destroy( ssr );
    throw e;
  }
}

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

private void buildSessionFactory(Class<?> entity) {
  StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().build();
  try {
    new MetadataSources( serviceRegistry )
        .addAnnotatedClass( entity )
        .buildMetadata()
        .buildSessionFactory()
        .close();
  }
  finally {
    StandardServiceRegistryBuilder.destroy( serviceRegistry );
  }
}

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

@Test
@TestForIssue( jiraKey = "HHH-9897" )
@FailureExpected( jiraKey = "HHH-9897" )
public void testUseOfJoinColumnOrFormula() {
  Metadata metadata = new MetadataSources()
      .addAnnotatedClass( A.class )
      .addAnnotatedClass( D.class )
      .buildMetadata();
  // Binding to the mapping model works after the simple change for HHH-9897
  // But building the SessionFactory fails in the collection persister trying to
  // use the formula (it expects Columns too)
  metadata.buildSessionFactory().close();
}

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

@Before
public void setUp() throws Exception {
  serviceRegistry = new StandardServiceRegistryBuilder()
      .enableAutoClose()
      .applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" )
      .build();
  MetadataBuildingContext buildingContext = new MetadataBuildingContextTestingImpl( serviceRegistry );
  Properties properties = new Properties();
  properties.setProperty( SequenceGenerator.SEQUENCE, TEST_SEQUENCE );
  properties.setProperty( SequenceHiLoGenerator.MAX_LO, "3" );
  properties.put(
      PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER,
      buildingContext.getObjectNameNormalizer()
  );
  generator = new SequenceHiLoGenerator();
  generator.configure( StandardBasicTypes.LONG, properties, serviceRegistry );
  Metadata metadata = new MetadataSources( serviceRegistry ).buildMetadata();
  generator.registerExportables( metadata.getDatabase() );
  sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
  sequenceValueExtractor = new SequenceValueExtractor( sessionFactory.getDialect(), TEST_SEQUENCE );
}

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

private void tryBuildingSessionFactory(Class... annotatedClasses) {
  final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().build();
  try {
    final MetadataSources metadataSources = new MetadataSources( serviceRegistry );
    for ( Class annotatedClass : annotatedClasses ) {
      metadataSources.addAnnotatedClass( annotatedClass );
    }
    final Metadata metadata = metadataSources.buildMetadata();
    final SessionFactory sessionFactory = metadata.buildSessionFactory();
    sessionFactory.close();
  }
  finally {
    StandardServiceRegistryBuilder.destroy( serviceRegistry );
  }
}

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

@Before
public void acquireResources() {
  serviceRegistry = TestHelper.getStandardServiceRegistryBuilder()
      .applySetting( AvailableSettings.CACHE_REGION_PREFIX, "" )
      .applySetting( AvailableSettings.GENERATE_STATISTICS, "true" )
      .build();
  final Metadata metadata = new MetadataSources( serviceRegistry )
      .addAnnotatedClass( CacheableItem.class )
      .buildMetadata();
  TestHelper.createRegions( metadata, true, false );
  sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
}

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

@Before
@SuppressWarnings("unused")
public void acquireResources() {
  final StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
      .configure( "hibernate-config/hibernate.cfg.xml" );
  if ( H2Dialect.class.equals( Dialect.getDialect().getClass() ) ) {
    ssrb.applySetting( AvailableSettings.URL, "jdbc:h2:mem:db-mvcc;MVCC=true" );
  }
  ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" );
  serviceRegistry = ssrb
      .configure( "hibernate-config/hibernate.cfg.xml" )
      .applySetting( AvailableSettings.HBM2DDL_DATABASE_ACTION, Action.CREATE )
      .build();
  final MetadataSources metadataSources = new MetadataSources( serviceRegistry );
  metadataSources.addAnnotatedClass( ReadWriteCacheableItem.class );
  metadataSources.addAnnotatedClass( ReadWriteVersionedCacheableItem.class );
  metadataSources.addAnnotatedClass( NonStrictReadWriteCacheableItem.class );
  metadataSources.addAnnotatedClass( NonStrictReadWriteVersionedCacheableItem.class );
  final Metadata metadata = metadataSources.buildMetadata();
  TestHelper.createRegions( metadata, true );
  sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
}

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

@Test(expected = ServiceException.class)
public void testInvalidConnectionProvider() {
  ServiceRegistryImplementor serviceRegistry = null;
  try {
    serviceRegistry    = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder()
        .applySetting( Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA  )
        .applySetting( Environment.MULTI_TENANT_CONNECTION_PROVIDER, "class.not.present.in.classpath" )
        .build();
    new MetadataSources( serviceRegistry ).buildMetadata().buildSessionFactory().close();
  }
  finally {
    if ( serviceRegistry != null ) {
      try {
        StandardServiceRegistryBuilder.destroy( serviceRegistry );
      }
      catch (Exception ignore) {
      }
    }
  }
}

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

@Test
public void testEntityWithMultipleJoinFetchedBags() {
  StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().build();
  Metadata metadata = new MetadataSources( standardRegistry )
      .addAnnotatedClass( Post.class )
      .addAnnotatedClass( PostComment.class )
      .addAnnotatedClass( Tag.class )
      .getMetadataBuilder()
      .build();
  try {
    metadata.buildSessionFactory();
    fail( "MultipleBagFetchException should have been thrown." );
  }
  catch (MultipleBagFetchException expected) {
  }
}

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

@Before
public void setUp() {
  final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
      .enableAutoClose()
      .applySetting( AvailableSettings.HBM2DDL_AUTO, "drop" )
      .build();
  SessionFactoryImplementor sessionFactory = null;
  try {
    final Metadata metadata = new MetadataSources( serviceRegistry )
        .addAnnotatedClass( Customer.class )
        .buildMetadata();
    sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
  }
  finally {
    if ( sessionFactory != null ) {
      sessionFactory.close();
    }
    StandardServiceRegistryBuilder.destroy( serviceRegistry );
  }
}

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

@Test
  @TestForIssue( jiraKey = "HHH-9913" )
  public void testNonAggregatedCompositeIdWithPkClass() {
    // HHH-9913 reports a NPE when bootstrapping a SF with non-aggregated composite identifiers
    // in org.hibernate.cache.internal.CacheDataDescriptionImpl#decode
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();

    try {
      new MetadataSources( ssr )
          .addAnnotatedClass( ItWithPkClass.class )
          .getMetadataBuilder()
          .applySharedCacheMode( SharedCacheMode.ENABLE_SELECTIVE )
          .build()
          .buildSessionFactory();
    }
    finally {
      StandardServiceRegistryBuilder.destroy( ssr );
    }
  }
}

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

@After
public void tearDown() {
  final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
      .enableAutoClose()
      .applySetting( AvailableSettings.HBM2DDL_AUTO, "drop" )
      .build();
  SessionFactoryImplementor sessionFactory = null;
  try {
    final Metadata metadata = new MetadataSources( serviceRegistry )
        .addAnnotatedClass( Customer.class )
        .buildMetadata();
    sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
  }
  finally {
    if ( sessionFactory != null ) {
      sessionFactory.close();
    }
    StandardServiceRegistryBuilder.destroy( serviceRegistry );
  }
}

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

@Test
@TestForIssue( jiraKey = "HHH-9913" )
public void testNonAggregatedCompositeId() {
  // HHH-9913 reports a NPE when bootstrapping a SF with non-aggregated composite identifiers
  // in org.hibernate.cache.internal.CacheDataDescriptionImpl#decode
  StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
      .applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, true )
      .build();
  try {
    new MetadataSources( ssr )
        .addAnnotatedClass( It.class )
        .getMetadataBuilder()
        .applySharedCacheMode( SharedCacheMode.ENABLE_SELECTIVE )
        .build()
        .buildSessionFactory();
  }
  finally {
    StandardServiceRegistryBuilder.destroy( ssr );
  }
}

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

@Test
public void testHHH10128() {
  final Metadata metadata = new MetadataSources( ssr )
      .addAnnotatedClass( Entity.class )
      .addAnnotatedClass( DescriptionEntity.class )
      .addAnnotatedClass( AddressLevel.class )
      .buildMetadata();
  final PersistentClass addressLevelBinding = metadata.getEntityBinding( AddressLevel.class.getName() );
  final Property natureProperty = addressLevelBinding.getProperty( "nature" );
  CustomType customType = assertTyping( CustomType.class, natureProperty.getType() );
  EnumType enumType = assertTyping( EnumType.class, customType.getUserType() );
  assertEquals( Types.VARCHAR, enumType.sqlTypes()[0] );
  SessionFactoryImplementor sf = (SessionFactoryImplementor) metadata.buildSessionFactory();
  try {
    EntityPersister p = sf.getEntityPersister( AddressLevel.class.getName() );
    CustomType runtimeType = assertTyping( CustomType.class, p.getPropertyType( "nature" ) );
    EnumType runtimeEnumType = assertTyping( EnumType.class, runtimeType.getUserType() );
    assertEquals( Types.VARCHAR, runtimeEnumType.sqlTypes()[0] );
  }
  finally {
    sf.close();
  }
}

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

(SessionFactoryImplementor) metadataBuilder.build().buildSessionFactory() ) {

相关文章