Search in sources :

Example 56 with Metadata

use of org.hibernate.boot.Metadata in project jbosstools-hibernate by jbosstools.

the class ConfigurationFacadeTest method testReadFromJDBC.

@Test
public void testReadFromJDBC() throws Exception {
    Connection connection = DriverManager.getConnection("jdbc:h2:mem:test");
    Statement statement = connection.createStatement();
    statement.execute("CREATE TABLE FOO(id int primary key, bar varchar(255))");
    JdbcMetadataConfiguration jdbcMdCfg = new JdbcMetadataConfiguration();
    jdbcMdCfg.setProperty("hibernate.connection.url", "jdbc:h2:mem:test");
    configurationFacade = FACADE_FACTORY.createConfiguration(jdbcMdCfg);
    Metadata metadata = jdbcMdCfg.getMetadata();
    Assert.assertNull(metadata);
    jdbcMdCfg = new JdbcMetadataConfiguration();
    jdbcMdCfg.setProperty("hibernate.connection.url", "jdbc:h2:mem:test");
    configurationFacade = FACADE_FACTORY.createConfiguration(jdbcMdCfg);
    configurationFacade.readFromJDBC();
    metadata = jdbcMdCfg.getMetadata();
    Iterator<PersistentClass> iterator = metadata.getEntityBindings().iterator();
    PersistentClass persistentClass = (PersistentClass) iterator.next();
    Assert.assertEquals("Foo", persistentClass.getClassName());
    statement.execute("DROP TABLE FOO");
    connection.close();
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcMetadataConfiguration(org.jboss.tools.hibernate.runtime.v_5_3.internal.util.JdbcMetadataConfiguration) Metadata(org.hibernate.boot.Metadata) PersistentClass(org.hibernate.mapping.PersistentClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) Test(org.junit.Test)

Example 57 with Metadata

use of org.hibernate.boot.Metadata in project jbosstools-hibernate by jbosstools.

the class ConfigurationFacadeTest method testConfigure.

@Test
public void testConfigure() {
    String fooClassName = "org.jboss.tools.hibernate.runtime.v_5_3.internal.test.Foo";
    Metadata metadata = MetadataHelper.getMetadata(configuration);
    Assert.assertNull(metadata.getEntityBinding(fooClassName));
    configurationFacade.configure();
    metadata = MetadataHelper.getMetadata(configuration);
    Assert.assertNotNull(metadata.getEntityBinding(fooClassName));
}
Also used : Metadata(org.hibernate.boot.Metadata) Test(org.junit.Test)

Example 58 with Metadata

use of org.hibernate.boot.Metadata in project hibernate-orm by hibernate.

the class InterceptorTest method testConfiguredSessionInterceptorWithSessionFactory.

@Test
public void testConfiguredSessionInterceptorWithSessionFactory() {
    StandardServiceRegistryImpl standardRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().build();
    SessionFactory sessionFactory = null;
    try {
        MetadataSources metadataSources = new MetadataSources(standardRegistry);
        for (Class annotatedClass : getAnnotatedClasses()) {
            metadataSources.addAnnotatedClass(annotatedClass);
        }
        Metadata metadata = metadataSources.getMetadataBuilder().build();
        SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
        sessionFactoryBuilder.applyStatelessInterceptor(LocalExceptionInterceptor.class);
        sessionFactory = sessionFactoryBuilder.build();
        final SessionFactory sessionFactoryInstance = sessionFactory;
        Supplier<SessionFactory> sessionFactorySupplier = () -> sessionFactoryInstance;
        Item i = new Item();
        i.setName("Laptop");
        try {
            doInHibernate(sessionFactorySupplier, session -> {
                session.persist(i);
                fail("No interceptor");
                return null;
            });
        } catch (IllegalStateException e) {
            assertEquals(LocalExceptionInterceptor.LOCAL_EXCEPTION_MESSAGE, e.getMessage());
        }
    } finally {
        if (sessionFactory != null) {
            sessionFactory.close();
        }
        standardRegistry.destroy();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Item(org.hibernate.jpa.test.Item) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) Test(org.junit.Test)

Example 59 with Metadata

use of org.hibernate.boot.Metadata in project hibernate-orm by hibernate.

the class SimpleXmlOverriddenTest method baseline.

/**
 * A baseline test, with an explicit @Convert annotation that should be in effect
 */
@Test
public void baseline() {
    Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata();
    PersistentClass pc = metadata.getEntityBinding(TheEntity.class.getName());
    Type type = pc.getProperty("it").getType();
    AttributeConverterTypeAdapter adapter = assertTyping(AttributeConverterTypeAdapter.class, type);
    assertTrue(SillyStringConverter.class.isAssignableFrom(adapter.getAttributeConverter().getConverterJavaTypeDescriptor().getJavaType()));
}
Also used : StringType(org.hibernate.type.StringType) Type(org.hibernate.type.Type) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test)

Example 60 with Metadata

use of org.hibernate.boot.Metadata in project hibernate-orm by hibernate.

the class SimpleXmlOverriddenTest method testDefinitionAtAttributeLevel.

/**
 * Test outcome of applying overrides via orm.xml, specifically at the attribute level
 */
@Test
public void testDefinitionAtAttributeLevel() {
    // NOTE : simple-override.xml applied disable-conversion="true" at the attribute-level
    Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).addResource("org/hibernate/test/converter/simple-override.xml").buildMetadata();
    PersistentClass pc = metadata.getEntityBinding(TheEntity.class.getName());
    Type type = pc.getProperty("it").getType();
    assertTyping(StringType.class, type);
}
Also used : StringType(org.hibernate.type.StringType) Type(org.hibernate.type.Type) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test)

Aggregations

Metadata (org.hibernate.boot.Metadata)148 MetadataSources (org.hibernate.boot.MetadataSources)124 Test (org.junit.Test)103 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)73 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)56 PersistentClass (org.hibernate.mapping.PersistentClass)46 TestForIssue (org.hibernate.testing.TestForIssue)31 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)18 Session (org.hibernate.Session)14 HashMap (java.util.HashMap)13 SchemaCreatorImpl (org.hibernate.tool.schema.internal.SchemaCreatorImpl)13 Property (org.hibernate.mapping.Property)12 RootClass (org.hibernate.mapping.RootClass)12 Map (java.util.Map)11 IdentifierGenerator (org.hibernate.id.IdentifierGenerator)11 SessionFactory (org.hibernate.SessionFactory)10 Collection (org.hibernate.mapping.Collection)10 ServiceRegistryImplementor (org.hibernate.service.spi.ServiceRegistryImplementor)10 ServiceRegistry (org.hibernate.service.ServiceRegistry)8 Before (org.junit.Before)8