Search in sources :

Example 76 with HibernateException

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

the class BasicTypeRegistry method register.

public void register(BasicType type, String[] keys) {
    if (locked) {
        throw new HibernateException("Can not alter TypeRegistry at this time");
    }
    if (type == null) {
        throw new HibernateException("Type to register cannot be null");
    }
    if (keys == null || keys.length == 0) {
        LOG.typeDefinedNoRegistrationKeys(type);
        return;
    }
    for (String key : keys) {
        // be safe...
        if (key == null) {
            continue;
        }
        LOG.debugf("Adding type registration %s -> %s", key, type);
        final Type old = registry.put(key, type);
        if (old != null && old != type) {
            LOG.typeRegistrationOverridesPrevious(key, old);
        }
    }
}
Also used : UserType(org.hibernate.usertype.UserType) CompositeUserType(org.hibernate.usertype.CompositeUserType) HibernateException(org.hibernate.HibernateException)

Example 77 with HibernateException

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

the class JdbcTypeNameMapper method buildJdbcTypeMap.

private static Map<Integer, String> buildJdbcTypeMap() {
    HashMap<Integer, String> map = new HashMap<Integer, String>();
    Field[] fields = java.sql.Types.class.getFields();
    if (fields == null) {
        throw new HibernateException("Unexpected problem extracting JDBC type mapping codes from java.sql.Types");
    }
    for (Field field : fields) {
        try {
            final int code = field.getInt(null);
            String old = map.put(code, field.getName());
            if (old != null) {
                LOG.JavaSqlTypesMappedSameCodeMultipleTimes(code, old, field.getName());
            }
        } catch (IllegalAccessException e) {
            throw new HibernateException("Unable to access JDBC type mapping [" + field.getName() + "]", e);
        }
    }
    return Collections.unmodifiableMap(map);
}
Also used : Field(java.lang.reflect.Field) HashMap(java.util.HashMap) HibernateException(org.hibernate.HibernateException)

Example 78 with HibernateException

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

the class DataHelper method extractString.

/**
	 * Extract the contents of the given Clob as a string.
	 *
	 * @param value The clob to to be extracted from
	 *
	 * @return The content as string
	 */
public static String extractString(final Clob value) {
    try {
        final Reader characterStream = value.getCharacterStream();
        final long length = determineLengthForBufferSizing(value);
        return length > Integer.MAX_VALUE ? extractString(characterStream, Integer.MAX_VALUE) : extractString(characterStream, (int) length);
    } catch (SQLException e) {
        throw new HibernateException("Unable to access lob stream", e);
    }
}
Also used : SQLException(java.sql.SQLException) HibernateException(org.hibernate.HibernateException) Reader(java.io.Reader) StringReader(java.io.StringReader)

Example 79 with HibernateException

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

the class TypeFactorySerializationTest method testUnregisterSerializeRegisterDiffSessionFactory.

@Test
public void testUnregisterSerializeRegisterDiffSessionFactory() throws Exception {
    Configuration cfg = new Configuration().setProperty(AvailableSettings.SESSION_FACTORY_NAME, NAME).setProperty(AvailableSettings.SESSION_FACTORY_NAME_IS_JNDI, // default is true
    "false");
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory();
    assertSame(factory, SessionFactoryRegistry.INSTANCE.getNamedSessionFactory(NAME));
    // Remove the session factory from the registry
    SessionFactoryRegistry.INSTANCE.removeSessionFactory(factory.getUuid(), NAME, false, null);
    assertNull(SessionFactoryRegistry.INSTANCE.findSessionFactory(factory.getUuid(), NAME));
    TypeFactory typeFactory = factory.getTypeResolver().getTypeFactory();
    byte[] typeFactoryBytes = SerializationHelper.serialize(typeFactory);
    typeFactory = (TypeFactory) SerializationHelper.deserialize(typeFactoryBytes);
    try {
        typeFactory.resolveSessionFactory();
        fail("should have failed with HibernateException because session factory is not registered.");
    } catch (HibernateException ex) {
    // expected because the session factory is not registered.
    }
    // Now create a new session factory with the same name; it will have a different UUID.
    SessionFactoryImplementor factoryWithSameName = (SessionFactoryImplementor) cfg.buildSessionFactory();
    assertSame(factoryWithSameName, SessionFactoryRegistry.INSTANCE.getNamedSessionFactory(NAME));
    assertFalse(factory.getUuid().equals(factoryWithSameName.getUuid()));
    // Session factory resolved from typeFactory should be the new session factory
    // (because it is resolved from SessionFactoryRegistry.INSTANCE)
    assertSame(factoryWithSameName, typeFactory.resolveSessionFactory());
    factory.close();
    factoryWithSameName.close();
}
Also used : Configuration(org.hibernate.cfg.Configuration) HibernateException(org.hibernate.HibernateException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) TypeFactory(org.hibernate.type.TypeFactory) Test(org.junit.Test)

Example 80 with HibernateException

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

the class TypeFactorySerializationTest method testUnregisterSerializeRegisterDiffSessionFactoryNoName.

@Test
public void testUnregisterSerializeRegisterDiffSessionFactoryNoName() throws Exception {
    Configuration cfg = new Configuration();
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory();
    assertSame(factory, SessionFactoryRegistry.INSTANCE.getSessionFactory(factory.getUuid()));
    // Remove the session factory from the registry
    SessionFactoryRegistry.INSTANCE.removeSessionFactory(factory.getUuid(), null, false, null);
    assertNull(SessionFactoryRegistry.INSTANCE.getSessionFactory(factory.getUuid()));
    TypeFactory typeFactory = factory.getTypeResolver().getTypeFactory();
    byte[] typeFactoryBytes = SerializationHelper.serialize(typeFactory);
    typeFactory = (TypeFactory) SerializationHelper.deserialize(typeFactoryBytes);
    try {
        typeFactory.resolveSessionFactory();
        fail("should have failed with HibernateException because session factory is not registered.");
    } catch (HibernateException ex) {
    // expected because the session factory is not registered.
    }
    // Now create a new session factory with the same name; it will have a different UUID.
    SessionFactoryImplementor factoryWithDiffUuid = (SessionFactoryImplementor) cfg.buildSessionFactory();
    assertSame(factoryWithDiffUuid, SessionFactoryRegistry.INSTANCE.getSessionFactory(factoryWithDiffUuid.getUuid()));
    assertFalse(factory.getUuid().equals(factoryWithDiffUuid.getUuid()));
    // It should not be possible to resolve the session factory with no name configured.
    try {
        typeFactory.resolveSessionFactory();
        fail("should have failed with HibernateException because session factories were not registered with the same non-null name.");
    } catch (HibernateException ex) {
    // expected
    }
    factory.close();
    factoryWithDiffUuid.close();
}
Also used : Configuration(org.hibernate.cfg.Configuration) HibernateException(org.hibernate.HibernateException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) TypeFactory(org.hibernate.type.TypeFactory) Test(org.junit.Test)

Aggregations

HibernateException (org.hibernate.HibernateException)372 DAOException (org.jbei.ice.storage.DAOException)141 Session (org.hibernate.Session)72 Test (org.junit.Test)41 ArrayList (java.util.ArrayList)30 SQLException (java.sql.SQLException)27 IOException (java.io.IOException)15 TestForIssue (org.hibernate.testing.TestForIssue)15 Transaction (org.hibernate.Transaction)14 Group (org.jbei.ice.storage.model.Group)14 Type (org.hibernate.type.Type)12 PersistenceException (org.mifos.framework.exceptions.PersistenceException)12 Serializable (java.io.Serializable)11 EntityEntry (org.hibernate.engine.spi.EntityEntry)10 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)10 HashMap (java.util.HashMap)9 Method (java.lang.reflect.Method)8 Dialect (org.hibernate.dialect.Dialect)8 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)8 HibernateProxy (org.hibernate.proxy.HibernateProxy)8