Search in sources :

Example 31 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder in project hibernate-orm by hibernate.

the class AttributeConverterTest method testEnumConverter.

@Test
@TestForIssue(jiraKey = "HHH-8866")
public void testEnumConverter() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop").build();
    try {
        MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(EntityWithConvertibleField.class).getMetadataBuilder().applyAttributeConverter(ConvertibleEnumConverter.class, true).build();
        // first lets validate that the converter was applied...
        PersistentClass tester = metadata.getEntityBinding(EntityWithConvertibleField.class.getName());
        Property nameProp = tester.getProperty("convertibleEnum");
        SimpleValue nameValue = (SimpleValue) nameProp.getValue();
        Type type = nameValue.getType();
        assertNotNull(type);
        assertTyping(BasicType.class, type);
        if (!AttributeConverterTypeAdapter.class.isInstance(type)) {
            fail("AttributeConverter not applied");
        }
        AbstractStandardBasicType basicType = assertTyping(AbstractStandardBasicType.class, type);
        assertTyping(EnumJavaTypeDescriptor.class, basicType.getJavaTypeDescriptor());
        assertEquals(Types.VARCHAR, basicType.getSqlTypeDescriptor().getSqlType());
        // then lets build the SF and verify its use...
        final SessionFactory sf = metadata.buildSessionFactory();
        try {
            Session s = sf.openSession();
            s.getTransaction().begin();
            EntityWithConvertibleField entity = new EntityWithConvertibleField();
            entity.setId("ID");
            entity.setConvertibleEnum(ConvertibleEnum.VALUE);
            String entityID = entity.getId();
            s.persist(entity);
            s.getTransaction().commit();
            s.close();
            s = sf.openSession();
            s.beginTransaction();
            entity = (EntityWithConvertibleField) s.load(EntityWithConvertibleField.class, entityID);
            assertEquals(ConvertibleEnum.VALUE, entity.getConvertibleEnum());
            s.getTransaction().commit();
            s.close();
            JavaConstantNode javaConstantNode = new JavaConstantNode();
            javaConstantNode.setExpectedType(type);
            javaConstantNode.setSessionFactory((SessionFactoryImplementor) sf);
            javaConstantNode.setText("org.hibernate.test.converter.AttributeConverterTest$ConvertibleEnum.VALUE");
            final String outcome = javaConstantNode.getRenderText((SessionFactoryImplementor) sf);
            assertEquals("'VALUE'", outcome);
            s = sf.openSession();
            s.beginTransaction();
            s.createQuery("FROM EntityWithConvertibleField e where e.convertibleEnum = org.hibernate.test.converter.AttributeConverterTest$ConvertibleEnum.VALUE").list();
            s.getTransaction().commit();
            s.close();
            s = sf.openSession();
            s.beginTransaction();
            s.delete(entity);
            s.getTransaction().commit();
            s.close();
        } finally {
            try {
                sf.close();
            } catch (Exception ignore) {
            }
        }
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) JavaConstantNode(org.hibernate.hql.internal.ast.tree.JavaConstantNode) AnnotationException(org.hibernate.AnnotationException) SimpleValue(org.hibernate.mapping.SimpleValue) BasicType(org.hibernate.type.BasicType) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) Type(org.hibernate.type.Type) Property(org.hibernate.mapping.Property) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 32 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder in project hibernate-orm by hibernate.

the class QueryReturnTest method testQueryReturn.

@Test
public void testQueryReturn() {
    StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder().applySetting("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
    StandardServiceRegistry standardServiceRegistry = serviceRegistryBuilder.build();
    MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);
    try {
        metadataSources.addInputStream(new ReaderInputStream(new StringReader(QUERY_RETURN_HBM_XML)));
        Metadata metadata = metadataSources.buildMetadata();
        NamedSQLQueryDefinition myQuery = metadata.getNamedNativeQueryDefinition("myQuery");
        Assert.assertNotNull(myQuery);
        NativeSQLQueryReturn[] myQueryReturns = myQuery.getQueryReturns();
        Assert.assertNotNull(myQueryReturns);
        Assert.assertEquals(1, myQueryReturns.length);
        Assert.assertTrue(NativeSQLQueryRootReturn.class.isInstance(myQueryReturns[0]));
        NativeSQLQueryRootReturn myQueryRootReturn = (NativeSQLQueryRootReturn) myQueryReturns[0];
        Assert.assertEquals("e", myQueryRootReturn.getAlias());
        Assert.assertEquals("org.hibernate.test.hbm.query.QueryReturnTest$Bar", myQueryRootReturn.getReturnEntityName());
    } finally {
        if (standardServiceRegistry instanceof StandardServiceRegistryImpl) {
            ((StandardServiceRegistryImpl) standardServiceRegistry).destroy();
        }
    }
}
Also used : ReaderInputStream(org.hibernate.engine.jdbc.ReaderInputStream) NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) NativeSQLQueryReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn) MetadataSources(org.hibernate.boot.MetadataSources) StringReader(java.io.StringReader) Metadata(org.hibernate.boot.Metadata) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 33 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder in project hibernate-orm by hibernate.

the class DisabledForeignKeyTest method expandedTests.

@Test
@TestForIssue(jiraKey = "HHH-9704")
public void expandedTests() {
    StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder();
    StandardServiceRegistry standardRegistry = registryBuilder.build();
    try {
        final MetadataSources sources = new MetadataSources(standardRegistry);
        sources.addAnnotatedClass(ManyToManyOwner.class);
        sources.addAnnotatedClass(ManyToManyTarget.class);
        final MetadataImplementor metadata = (MetadataImplementor) sources.buildMetadata();
        metadata.validate();
        // export the schema
        new SchemaExport().execute(EnumSet.of(TargetType.DATABASE), SchemaExport.Action.BOTH, metadata);
        try {
            // update the schema
            new SchemaUpdate().execute(EnumSet.of(TargetType.DATABASE), metadata);
        } finally {
            // drop the schema
            new SchemaExport().execute(EnumSet.of(TargetType.DATABASE), SchemaExport.Action.DROP, metadata);
        }
    } finally {
        StandardServiceRegistryBuilder.destroy(standardRegistry);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SchemaUpdate(org.hibernate.tool.hbm2ddl.SchemaUpdate) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 34 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder in project hibernate-orm by hibernate.

the class Db2GenerationTest method testLegacyGeneratorTableCreationOnDb2.

@Test
@TestForIssue(jiraKey = "HHH-9850")
public void testLegacyGeneratorTableCreationOnDb2() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DIALECT, DB2Dialect.class.getName()).build();
    try {
        Metadata metadata = new MetadataSources(ssr).buildMetadata();
        assertEquals(0, metadata.getDatabase().getDefaultNamespace().getTables().size());
        MultipleHiLoPerTableGenerator generator = new MultipleHiLoPerTableGenerator();
        Properties properties = new Properties();
        generator.configure(IntegerType.INSTANCE, properties, ssr);
        generator.registerExportables(metadata.getDatabase());
        assertEquals(1, metadata.getDatabase().getDefaultNamespace().getTables().size());
        final Table table = metadata.getDatabase().getDefaultNamespace().getTables().iterator().next();
        final String[] createCommands = new DB2Dialect().getTableExporter().getSqlCreateStrings(table, metadata);
        assertContains("sequence_name varchar(255) not null", createCommands[0]);
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : MultipleHiLoPerTableGenerator(org.hibernate.id.MultipleHiLoPerTableGenerator) Table(org.hibernate.mapping.Table) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) Properties(java.util.Properties) DB2Dialect(org.hibernate.dialect.DB2Dialect) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 35 with StandardServiceRegistryBuilder

use of org.hibernate.boot.registry.StandardServiceRegistryBuilder in project hibernate-orm by hibernate.

the class Db2GenerationTest method testNewGeneratorTableCreationOnDb2.

@Test
@TestForIssue(jiraKey = "HHH-9850")
public void testNewGeneratorTableCreationOnDb2() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DIALECT, DB2Dialect.class.getName()).build();
    try {
        Metadata metadata = new MetadataSources(ssr).buildMetadata();
        assertEquals(0, metadata.getDatabase().getDefaultNamespace().getTables().size());
        TableGenerator generator = new TableGenerator();
        Properties properties = new Properties();
        generator.configure(IntegerType.INSTANCE, properties, ssr);
        generator.registerExportables(metadata.getDatabase());
        assertEquals(1, metadata.getDatabase().getDefaultNamespace().getTables().size());
        final Table table = metadata.getDatabase().getDefaultNamespace().getTables().iterator().next();
        final String[] createCommands = new DB2Dialect().getTableExporter().getSqlCreateStrings(table, metadata);
        assertContains("sequence_name varchar(255) not null", createCommands[0]);
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : Table(org.hibernate.mapping.Table) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) TableGenerator(org.hibernate.id.enhanced.TableGenerator) MultipleHiLoPerTableGenerator(org.hibernate.id.MultipleHiLoPerTableGenerator) Properties(java.util.Properties) DB2Dialect(org.hibernate.dialect.DB2Dialect) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)167 MetadataSources (org.hibernate.boot.MetadataSources)106 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)94 Test (org.junit.Test)92 Metadata (org.hibernate.boot.Metadata)46 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)34 PersistentClass (org.hibernate.mapping.PersistentClass)27 Before (org.junit.Before)26 Properties (java.util.Properties)24 TestForIssue (org.hibernate.testing.TestForIssue)23 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)17 SessionFactory (org.hibernate.SessionFactory)15 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)14 Property (org.hibernate.mapping.Property)14 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)14 File (java.io.File)13 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)13 Map (java.util.Map)12 HashMap (java.util.HashMap)11 ServiceRegistry (org.hibernate.service.ServiceRegistry)11