Search in sources :

Example 76 with SessionFactory

use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.

the class NamedQueryTest method testQuery.

@Test
public void testQuery() {
    Configuration cfg = new Configuration();
    cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
    cfg.addInputStream(new ReaderInputStream(new StringReader(NAMED_QUERY_HBM_XML)));
    SessionFactory sessionFactory = cfg.buildSessionFactory();
    sessionFactory.close();
}
Also used : SessionFactory(org.hibernate.SessionFactory) ReaderInputStream(org.hibernate.engine.jdbc.ReaderInputStream) Configuration(org.hibernate.cfg.Configuration) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 77 with SessionFactory

use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.

the class GeneratedValueTest method testGeneratedUuidId.

@Test
public void testGeneratedUuidId() throws Exception {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop").build();
    try {
        Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata();
        ((MetadataImpl) metadata).validate();
        PersistentClass entityBinding = metadata.getEntityBinding(TheEntity.class.getName());
        assertEquals(UUID.class, entityBinding.getIdentifier().getType().getReturnedClass());
        IdentifierGenerator generator = entityBinding.getIdentifier().createIdentifierGenerator(metadata.getIdentifierGeneratorFactory(), metadata.getDatabase().getDialect(), null, null, (RootClass) entityBinding);
        assertTyping(UUIDGenerator.class, generator);
        // now a functional test
        SessionFactory sf = metadata.buildSessionFactory();
        try {
            TheEntity theEntity = new TheEntity();
            Session s = sf.openSession();
            s.beginTransaction();
            s.save(theEntity);
            s.getTransaction().commit();
            s.close();
            assertNotNull(theEntity.id);
            s = sf.openSession();
            s.beginTransaction();
            try {
                s.delete(theEntity);
                s.getTransaction().commit();
            } catch (Exception e) {
                s.getTransaction().rollback();
                throw e;
            } finally {
                s.close();
            }
        } finally {
            try {
                sf.close();
            } catch (Exception ignore) {
            }
        }
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) MetadataImpl(org.hibernate.boot.internal.MetadataImpl) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) Session(org.hibernate.Session) Test(org.junit.Test)

Example 78 with SessionFactory

use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.

the class DeepCollectionElementTest method testInitialization.

@Test
public void testInitialization() throws Exception {
    Configuration configuration = new Configuration();
    configuration.addAnnotatedClass(A.class);
    configuration.addAnnotatedClass(B.class);
    configuration.addAnnotatedClass(C.class);
    StandardServiceRegistryImpl serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(configuration.getProperties());
    try {
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        sessionFactory.close();
    } finally {
        serviceRegistry.destroy();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) StandardServiceRegistryImpl(org.hibernate.boot.registry.internal.StandardServiceRegistryImpl) Test(org.junit.Test)

Example 79 with SessionFactory

use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.

the class OneToOneErrorTest method testWrongOneToOne.

@Test
public void testWrongOneToOne() throws Exception {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Show.class).addAnnotatedClass(ShowDescription.class);
    cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    ServiceRegistry serviceRegistry = null;
    SessionFactory sessionFactory = null;
    try {
        serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(Environment.getProperties());
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        Assert.fail("Wrong mappedBy does not fail property");
    } catch (AnnotationException e) {
    //success
    } finally {
        if (sessionFactory != null) {
            sessionFactory.close();
        }
        if (serviceRegistry != null) {
            ServiceRegistryBuilder.destroy(serviceRegistry);
        }
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) AnnotationException(org.hibernate.AnnotationException) ServiceRegistry(org.hibernate.service.ServiceRegistry) Test(org.junit.Test)

Example 80 with SessionFactory

use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.

the class AttributeConverterTest method testBasicTimestampUsage.

@Test
public void testBasicTimestampUsage() {
    Configuration cfg = new Configuration();
    cfg.addAttributeConverter(InstantConverter.class, false);
    cfg.addAnnotatedClass(IrrelevantInstantEntity.class);
    cfg.setProperty(AvailableSettings.HBM2DDL_AUTO, "create-drop");
    cfg.setProperty(AvailableSettings.GENERATE_STATISTICS, "true");
    SessionFactory sf = cfg.buildSessionFactory();
    try {
        Session session = sf.openSession();
        session.beginTransaction();
        session.save(new IrrelevantInstantEntity(1L));
        session.getTransaction().commit();
        session.close();
        sf.getStatistics().clear();
        session = sf.openSession();
        session.beginTransaction();
        IrrelevantInstantEntity e = (IrrelevantInstantEntity) session.get(IrrelevantInstantEntity.class, 1L);
        session.getTransaction().commit();
        session.close();
        assertEquals(0, sf.getStatistics().getEntityUpdateCount());
        session = sf.openSession();
        session.beginTransaction();
        session.delete(e);
        session.getTransaction().commit();
        session.close();
    } finally {
        sf.close();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

SessionFactory (org.hibernate.SessionFactory)108 Test (org.junit.Test)62 Session (org.hibernate.Session)50 Configuration (org.hibernate.cfg.Configuration)35 Transaction (org.hibernate.Transaction)20 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)19 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)13 MetadataSources (org.hibernate.boot.MetadataSources)11 HibernateEntityManagerFactory (org.hibernate.jpa.HibernateEntityManagerFactory)9 TestForIssue (org.hibernate.testing.TestForIssue)9 Properties (java.util.Properties)8 Query (org.hibernate.Query)8 Metadata (org.hibernate.boot.Metadata)8 ArrayList (java.util.ArrayList)7 InfinispanRegionFactory (org.hibernate.cache.infinispan.InfinispanRegionFactory)7 List (java.util.List)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 TimeUnit (java.util.concurrent.TimeUnit)5 AnnotationException (org.hibernate.AnnotationException)5 Collections (java.util.Collections)4