Search in sources :

Example 81 with StandardServiceRegistry

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

the class ForeignKeyMigrationTest method testMigrationOfForeignKeys.

@Test
@TestForIssue(jiraKey = "HHH-9716")
public // @FailureExpected( jiraKey = "HHH-9716" )
void testMigrationOfForeignKeys() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(Box.class).addAnnotatedClass(Thing.class).buildMetadata();
        metadata.validate();
        // first create the schema...
        new SchemaExport().create(EnumSet.of(TargetType.DATABASE), metadata);
        try {
            // try to update the just created schema
            new SchemaUpdate().execute(EnumSet.of(TargetType.DATABASE), metadata);
        } finally {
            // clean up
            new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata);
        }
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
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 82 with StandardServiceRegistry

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

the class ClosedFactoryTests method testClosedChecks.

@Test
public void testClosedChecks() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.JPA_CLOSED_COMPLIANCE, "true").build();
    try {
        final SessionFactoryBuilderImplementor factoryBuilder = (SessionFactoryBuilderImplementor) new MetadataSources(ssr).buildMetadata().getSessionFactoryBuilder();
        final SessionFactory sf = factoryBuilder.build();
        sf.close();
        assertTrue(sf.isClosed());
        try {
            sf.getCache();
            fail("#getCache did not fail");
        } catch (IllegalStateException expected) {
        // this is the expected outcome
        } catch (Exception e) {
            fail("#getCache failed, but not with the expected IllegalStateException : " + e.toString());
        }
        try {
            sf.getMetamodel();
            fail("#getMetamodel did not fail");
        } catch (IllegalStateException expected) {
        // this is the expected outcome
        } catch (Exception e) {
            fail("#getMetamodel failed, but not with the expected IllegalStateException : " + e.toString());
        }
        try {
            sf.getCriteriaBuilder();
            fail("#getCriteriaBuilder did not fail");
        } catch (IllegalStateException expected) {
        // this is the expected outcome
        } catch (Exception e) {
            fail("#getCriteriaBuilder failed, but not with the expected IllegalStateException : " + e.toString());
        }
        try {
            sf.getProperties();
            fail("#getProperties did not fail");
        } catch (IllegalStateException expected) {
        // this is the expected outcome
        } catch (Exception e) {
            fail("#getProperties failed, but not with the expected IllegalStateException : " + e.toString());
        }
        try {
            sf.getPersistenceUnitUtil();
            fail("#getPersistenceUnitUtil did not fail");
        } catch (IllegalStateException expected) {
        // this is the expected outcome
        } catch (Exception e) {
            fail("#getPersistenceUnitUtil failed, but not with the expected IllegalStateException : " + e.toString());
        }
        try {
            sf.close();
            fail("#close did not fail");
        } catch (IllegalStateException expected) {
        // this is the expected outcome
        } catch (Exception e) {
            fail("#close failed, but not with the expected IllegalStateException : " + e.toString());
        }
        try {
            sf.createEntityManager();
            fail("#createEntityManager did not fail");
        } catch (IllegalStateException expected) {
        // this is the expected outcome
        } catch (Exception e) {
            fail("#createEntityManager failed, but not with the expected IllegalStateException : " + e.toString());
        }
        try {
            sf.createEntityManager(Collections.emptyMap());
            fail("#createEntityManager(Map) did not fail");
        } catch (IllegalStateException expected) {
        // this is the expected outcome
        } catch (Exception e) {
            fail("#createEntityManager(Map) failed, but not with the expected IllegalStateException : " + e.toString());
        }
    } catch (Exception e) {
        // if an exception is
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryBuilderImplementor(org.hibernate.boot.spi.SessionFactoryBuilderImplementor) MetadataSources(org.hibernate.boot.MetadataSources) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 83 with StandardServiceRegistry

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

the class EntityTransactionTests method testSetRollbackOnlyOutcomeExpectations.

@Test
public void testSetRollbackOnlyOutcomeExpectations() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.JPA_TRANSACTION_COMPLIANCE, "true").build();
    try {
        final SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).buildMetadata().buildSessionFactory();
        try {
            inSession(sessionFactory, session -> {
                final Transaction transaction = session.getTransaction();
                transaction.begin();
                try {
                    assertTrue(transaction.isActive());
                    transaction.setRollbackOnly();
                    assertTrue(transaction.isActive());
                    assertTrue(transaction.getRollbackOnly());
                } finally {
                    if (transaction.isActive()) {
                        transaction.rollback();
                    }
                }
            });
            inSession(sessionFactory, session -> {
                final Transaction transaction = session.getTransaction();
                transaction.begin();
                try {
                    assertTrue(transaction.isActive());
                    transaction.setRollbackOnly();
                    assertTrue(transaction.isActive());
                    assertTrue(transaction.getRollbackOnly());
                    // now try to commit, this should force a rollback
                    try {
                        transaction.commit();
                    } catch (RollbackException e) {
                        assertFalse(transaction.isActive());
                        assertThat(transaction.getStatus(), CoreMatchers.is(TransactionStatus.ROLLED_BACK));
                    }
                } finally {
                    if (transaction.isActive()) {
                        transaction.rollback();
                    }
                }
            });
        } finally {
            sessionFactory.close();
        }
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Transaction(org.hibernate.Transaction) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) RollbackException(javax.persistence.RollbackException) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 84 with StandardServiceRegistry

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

the class EntityTransactionTests method testGetRollbackOnlyExpectations.

@Test
public void testGetRollbackOnlyExpectations() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.JPA_TRANSACTION_COMPLIANCE, "true").build();
    try {
        final SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).buildMetadata().buildSessionFactory();
        try {
            inSession(sessionFactory, session -> {
                final Transaction transaction = session.getTransaction();
                assertFalse(transaction.isActive());
                try {
                    transaction.getRollbackOnly();
                    fail("Expecting failure #getRollbackOnly on non-active txn");
                } catch (IllegalStateException expected) {
                }
            });
        } finally {
            sessionFactory.close();
        }
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Transaction(org.hibernate.Transaction) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 85 with StandardServiceRegistry

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

the class EntityTransactionTests method testSetRollbackOnlyExpectations.

@Test
public void testSetRollbackOnlyExpectations() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.JPA_TRANSACTION_COMPLIANCE, "true").build();
    try {
        final SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).buildMetadata().buildSessionFactory();
        try {
            inSession(sessionFactory, session -> {
                final Transaction transaction = session.getTransaction();
                assertFalse(transaction.isActive());
                try {
                    transaction.setRollbackOnly();
                    fail("Expecting failure #setRollbackOnly on non-active txn");
                } catch (IllegalStateException expected) {
                }
            });
        } finally {
            sessionFactory.close();
        }
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Transaction(org.hibernate.Transaction) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Aggregations

StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)169 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)152 Test (org.junit.Test)121 MetadataSources (org.hibernate.boot.MetadataSources)118 Metadata (org.hibernate.boot.Metadata)56 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)39 PersistentClass (org.hibernate.mapping.PersistentClass)39 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)25 TestForIssue (org.hibernate.testing.TestForIssue)24 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)23 Properties (java.util.Properties)20 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)20 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)15 SessionFactory (org.hibernate.SessionFactory)14 Column (org.hibernate.mapping.Column)14 Property (org.hibernate.mapping.Property)14 Session (org.hibernate.Session)13 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)12 IdentifierGenerator (org.hibernate.id.IdentifierGenerator)11 RootClass (org.hibernate.mapping.RootClass)11