Java Code Examples for org.hibernate.boot.Metadata#getEntityBinding()

The following examples show how to use org.hibernate.boot.Metadata#getEntityBinding() . 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: SchemaUtils.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Gets the Spanner {@link Table} by entity class.
 */
public static Table getTable(Class<?> entityClass, Metadata metadata) {
  PersistentClass pc = metadata.getEntityBinding(entityClass.getCanonicalName());
  if (pc != null) {
    return pc.getTable();
  }

  throw new IllegalArgumentException(
      String.format("Could not find table for entity class %s.", entityClass.getName()));
}
 
Example 2
Source File: LegacyJpaImplNamingIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenLegacyJpaImplNamingStrategy_whenCreateDatabase_thenGetStrategyNames() {
    Metadata metadata = MetadataExtractorIntegrator.INSTANCE.getMetadata();
    String entity = Account.class.getCanonicalName();
    PersistentClass persistentClass = metadata.getEntityBinding(entity);
    Table table = persistentClass.getTable();
    String physicalNameExpected = "Secondary_Email";
    String implicitNameExpected = "defaultEmail";
    String tableNameExpected = "Account";

    String tableNameCreated = table.getName();
    boolean columnNameIsQuoted = table
      .getColumn(3)
      .isQuoted();
    String physicalNameCreated = table
      .getColumn(3)
      .getName();
    String implicitNameCreated = table
      .getColumn(2)
      .getName();

    SoftAssertions.assertSoftly(softly -> {
        softly
          .assertThat(columnNameIsQuoted)
          .isTrue();
        softly
          .assertThat(tableNameCreated)
          .isEqualTo(tableNameExpected);
        softly
          .assertThat(physicalNameCreated)
          .isEqualTo(physicalNameExpected);
        softly
          .assertThat(implicitNameCreated)
          .isEqualTo(implicitNameExpected);
    });
}
 
Example 3
Source File: SpringBootDefaultNamingIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenDefaultBootNamingStrategy_whenCreateDatabase_thenGetStrategyNames() {
    Metadata metadata = MetadataExtractorIntegrator.INSTANCE.getMetadata();
    String entity = Account.class.getCanonicalName();
    PersistentClass persistentClass = metadata.getEntityBinding(entity);
    Table table = persistentClass.getTable();
    String physicalNameExpected = "secondary_email";
    String implicitNameExpected = "default_email";
    String tableNameExpected = "account";

    String tableNameCreated = table.getName();
    String physicalNameCreated = table
      .getColumn(3)
      .getName();
    String implicitNameCreated = table
      .getColumn(2)
      .getName();

    SoftAssertions softly = new SoftAssertions();
    softly
      .assertThat(tableNameCreated)
      .isEqualTo(tableNameExpected);
    softly
      .assertThat(physicalNameCreated)
      .isEqualTo(physicalNameExpected);
    softly
      .assertThat(implicitNameCreated)
      .isEqualTo(implicitNameExpected);
    softly.assertAll();
}
 
Example 4
Source File: StrategyLegacyHbmImplIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenLegacyHbmImplNamingNamingStrategy_whenCreateDatabase_thenGetStrategyNames() {
    Metadata metadata = MetadataExtractorIntegrator.INSTANCE.getMetadata();
    String entity = Preference.class.getCanonicalName();
    PersistentClass persistentClass = metadata.getEntityBinding(entity);
    Collection<Table> tables = metadata
      .getDatabase()
      .getDefaultNamespace()
      .getTables();
    Table preferenceTable = persistentClass.getTable();
    String tableNameExpected = "Account_preferences";
    Table accountPreferencesTable = tables
      .stream()
      .filter(table -> table
        .getName()
        .equals(tableNameExpected))
      .findFirst()
      .get();
    String implicitNameExpected = "account";

    String implicitNameCreated = preferenceTable
      .getColumn(3)
      .getName();
    String tableNameCreated = accountPreferencesTable.getName();

    SoftAssertions.assertSoftly(softly -> {
        softly
          .assertThat(implicitNameCreated)
          .isEqualTo(implicitNameExpected);
        softly
          .assertThat(tableNameCreated)
          .isEqualTo(tableNameExpected);
    });
}