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);
}
}
}
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);
}
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);
}
}
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();
}
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();
}
Aggregations