Search in sources :

Example 1 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 2 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)

Example 3 with HibernateException

use of org.hibernate.HibernateException in project head by mifos.

the class LegacyAccountDao method updateLedgerAccount.

public void updateLedgerAccount(COABO coaBo, String accountName, String glCode, String parentGlCode) throws PersistenceException {
    Session session = StaticHibernateUtil.getSessionTL();
    Transaction transaction = session.beginTransaction();
    try {
        Short newParentId = getAccountIdFromGlCode(parentGlCode);
        coaBo.setAccountName(accountName);
        GLCodeEntity glCodeEntity = coaBo.getAssociatedGlcode();
        createOrUpdate(coaBo);
        glCodeEntity.setGlcode(glCode);
        createOrUpdate(glCodeEntity);
        Query query = session.getNamedQuery(NamedQueryConstants.SET_COA_PARENT);
        query.setShort("parentId", newParentId);
        query.setShort("id", coaBo.getAccountId());
        query.executeUpdate();
        transaction.commit();
    } catch (HibernateException ex) {
        transaction.rollback();
        throw new PersistenceException(ex);
    }
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) HibernateException(org.hibernate.HibernateException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) GLCodeEntity(org.mifos.accounts.financial.business.GLCodeEntity) Session(org.hibernate.Session)

Example 4 with HibernateException

use of org.hibernate.HibernateException in project head by mifos.

the class LegacyAccountDao method getCountForGlCode.

public int getCountForGlCode(Short glCodeId) throws PersistenceException {
    Session session = StaticHibernateUtil.getSessionTL();
    int count = -1;
    try {
        Query query = session.getNamedQuery(NamedQueryConstants.COUNT_GL_CODE_REFERENCES);
        query.setShort("glCodeId", glCodeId);
        count = (Integer) query.uniqueResult();
    } catch (HibernateException ex) {
        throw new PersistenceException(ex);
    }
    return count;
}
Also used : Query(org.hibernate.Query) HibernateException(org.hibernate.HibernateException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) Session(org.hibernate.Session)

Example 5 with HibernateException

use of org.hibernate.HibernateException in project head by mifos.

the class LegacyRolesPermissionsDao method getUserRoles.

/**
     * This function returns the PersonRoles object which contains the person
     * information and the set of all the roles related to that user
     *
     * @param uid
     *            user id
     * @return PersonRoles
     * @throws HibernateProcessException
     */
public Set getUserRoles(short uid) throws SystemException, ApplicationException {
    Set roles = null;
    try {
        Session session = StaticHibernateUtil.getSessionTL();
        Query personRoles = session.getNamedQuery(NamedQueryConstants.GETPERSONROLES);
        personRoles.setShort("ID", uid);
        List<PersonRoles> lst = personRoles.list();
        if (null != lst && lst.size() > 0) {
            PersonRoles pr = lst.get(0);
            roles = pr.getRoles();
        }
    } catch (HibernateException he) {
        throw new SecurityException(SecurityConstants.GENERALERROR, he);
    }
    return roles;
}
Also used : Set(java.util.Set) Query(org.hibernate.Query) HibernateException(org.hibernate.HibernateException) PersonRoles(org.mifos.security.util.PersonRoles) SecurityException(org.mifos.framework.exceptions.SecurityException) Session(org.hibernate.Session)

Aggregations

HibernateException (org.hibernate.HibernateException)611 Session (org.hibernate.Session)285 DAOException (com.tomasio.projects.trainning.exception.DAOException)186 DAOException (org.jbei.ice.storage.DAOException)122 ArrayList (java.util.ArrayList)63 Criteria (org.hibernate.Criteria)56 Test (org.junit.Test)43 SQLException (java.sql.SQLException)39 Transaction (org.hibernate.Transaction)39 SimpleDateFormat (java.text.SimpleDateFormat)22 Query (org.hibernate.Query)20 IOException (java.io.IOException)19 ParseException (java.text.ParseException)18 TestForIssue (org.hibernate.testing.TestForIssue)16 Date (java.util.Date)13 List (java.util.List)12 PersistenceException (org.mifos.framework.exceptions.PersistenceException)12 Serializable (java.io.Serializable)11 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)11 FullTextSession (org.hibernate.search.FullTextSession)11