Example usage for org.hibernate HibernateException HibernateException

List of usage examples for org.hibernate HibernateException HibernateException

Introduction

In this page you can find the example usage for org.hibernate HibernateException HibernateException.

Prototype

public HibernateException(Throwable cause) 

Source Link

Document

Constructs a HibernateException using the given message and underlying cause.

Usage

From source file:com.npower.dm.hibernate.id.HashCodeIdentifierGenerator.java

License:Open Source License

public Serializable generate(SessionImplementor session, Object entity) throws HibernateException {
    if (StringUtils.isEmpty(this.propertyName)) {
        throw new HibernateException("failure to generate hash id, the source property name is null, object: "
                + entity + ", property: " + this.propertyName);
    }//from  w  w w  .ja  v a2 s  . co m
    try {
        Object propertyValue = BeanUtils.getProperty(entity, this.propertyName);
        if (propertyValue == null || StringUtils.isEmpty(propertyValue.toString())) {
            throw new HibernateException("failure to generate hash id, the source property is enpty, object: "
                    + entity + ", " + this.propertyName + "=" + propertyValue);
        } else {
            return new Long(propertyValue.toString().hashCode());
        }
    } catch (IllegalAccessException e) {
        throw new HibernateException("failure to generate a identifier.", e);
    } catch (InvocationTargetException e) {
        throw new HibernateException("failure to generate a identifier.", e);
    } catch (NoSuchMethodException e) {
        throw new HibernateException("failure to generate a identifier.", e);
    }
}

From source file:com.npower.dm.hibernate.StartWithLetterExpression.java

License:Open Source License

public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
    if (columns.length != 1)
        throw new HibernateException("ilike may only be used with single-column properties");
    return " substr(getzhongwen(" + columns[0] + "), 0, 1 )=?";
}

From source file:com.picocontainer.persistence.hibernate.SessionFactoryLifecycleTestCase.java

License:Open Source License

@Test(expected = HibernateException.class)
public void cannotCloseSessionFactoryOnStop() throws Exception {
    final SessionFactory sessionFactory = mockery.mock(SessionFactory.class);
    mockery.checking(new Expectations() {
        {//w  w  w  .ja v a2  s . co m
            one(sessionFactory).close();
            will(throwException(new HibernateException("mock")));
        }
    });
    SessionFactoryLifecycle lifecycle = new SessionFactoryLifecycle(sessionFactory);
    lifecycle.stop();
}

From source file:com.picocontainer.persistence.hibernate.SessionLifecycleTestCase.java

License:Open Source License

@Test(expected = HibernateException.class)
public void cannotCloseSessionOnStop() throws Exception {
    final Session session = mockery.mock(Session.class);
    mockery.checking(new Expectations() {
        {/*from ww  w  . j a  v  a 2 s.c  o  m*/
            one(session).flush();
            will(throwException(new HibernateException("mock")));
        }
    });
    SessionLifecycle lifecycle = new SessionLifecycle(session);
    lifecycle.stop();
}

From source file:com.pushinginertia.commons.domain.usertype.MapAsJsonUserType.java

License:Open Source License

static String serialize(final Map<String, Object> map) throws HibernateException {
    final ObjectMapper mapper = new ObjectMapper();
    try {/*from  www . j a v a  2  s.c o  m*/
        return mapper.writeValueAsString(map);
    } catch (final IOException e) {
        throw new HibernateException("Failed to encode map as a JSON-encoded string.");
    }
}

From source file:com.quakearts.webapp.hibernate.HibernateBean.java

License:Open Source License

@SuppressWarnings("unchecked")
private Criterion handleParameter(String key, Serializable value, QueryContext context) {
    Criterion criterion;/*from   w ww. j a  v  a  2s  .  c  o m*/
    if (key == ParameterMapBuilder.CONJUNCTION && value instanceof Map) {
        Map<String, Serializable> conjoinedParameters = (Map<String, Serializable>) value;
        criterion = handleJunction(conjoinedParameters, ParameterMapBuilder.CONJUNCTION, context);
    } else if (key == ParameterMapBuilder.DISJUNCTION && value instanceof Map) {
        Map<String, Serializable> disjoinedParameters = (Map<String, Serializable>) value;
        criterion = handleJunction(disjoinedParameters, ParameterMapBuilder.DISJUNCTION, context);
    } else if (value instanceof Choice) {
        Choice choice = (Choice) value;
        if (choice.getChoices().size() == 0)
            throw new HibernateException(
                    "Invalid choice list for parameter" + key + ". Choice list must be greater than zero.");

        if (choice.getChoices().size() == 1)
            criterion = createCriterion(key, choice.getChoices().get(0), context);
        else if (choice.getChoices().size() == 2)
            criterion = Restrictions.or(createCriterion(key, choice.getChoices().get(0), context),
                    createCriterion(key, choice.getChoices().get(1), context));
        else {
            Criterion[] choiceCriteria = new Criterion[choice.getChoices().size()];
            for (int i = 0; i < choice.getChoices().size(); i++) {
                choiceCriteria[i] = createCriterion(key, choice.getChoices().get(i), context);
            }
            criterion = Restrictions.or(choiceCriteria);
        }
    } else {
        criterion = createCriterion(key, value, context);
    }

    return criterion;
}

From source file:com.quakearts.webapp.hibernate.HibernateBean.java

License:Open Source License

private Criterion handleJunction(Map<String, Serializable> junctionParameters, String type,
        QueryContext context) {/*  w ww.ja v  a  2 s.  com*/
    ArrayList<Criterion> criteria = null;
    criteria = new ArrayList<>();

    for (Entry<String, Serializable> entry : junctionParameters.entrySet()) {
        criteria.add(handleParameter(entry.getKey(), entry.getValue(), context));
    }

    Criterion[] criteriaArray = criteria.toArray(new Criterion[criteria.size()]);
    if (type == ParameterMapBuilder.CONJUNCTION)
        return (Restrictions.and(criteriaArray));
    else if (type == ParameterMapBuilder.DISJUNCTION)
        return (Restrictions.or(criteriaArray));
    else
        throw new HibernateException("Invalid conjuction type:" + type);
}

From source file:com.sam.moca.db.hibernate.SequenceGenerator.java

License:Open Source License

public Serializable generate(SessionImplementor arg0, Object arg1) throws HibernateException {
    MocaContext moca = MocaUtils.currentContext();
    MocaResults res = null;/*  w  w  w .  j  a  v  a  2 s .  co m*/

    try {
        res = moca.executeCommand("generate next number where numcod = '" + _numcod + "'");
        if (res.next()) {
            return res.getString("nxtnum");
        }
        throw new HibernateException("sequence generation returned no data.");
    } catch (MocaException e) {
        throw new HibernateException("error generating sequence", e);
    } finally {
        if (res != null) {
            res.close();
        }
    }
}

From source file:com.tida_okinawa.corona.io.dam.hibernate.IoService.java

License:Open Source License

/**
 * ??//from  w w w .  j  a  va2 s  .co  m
 * 
 * @return SQLConnection??????????
 */
@Override
public Session getSession() {
    if (!isConnectDam()) {
        CoronaActivator activator = CoronaActivator.getDefault();
        if (activator != null) {
            final int retryCount = activator.getPreferenceStore()
                    .getInt(PreferenceInitializer.PREF_DB_RETRY_CNT);

            PrintStream out = activator.getLogger().getOutStream();
            out.println(Messages.IoService_reconnectDatabase);

            for (int i = 1; i <= retryCount; i++) {
                out.print(Messages.bind(Messages.IoService_reconnect, i));
                reConnect();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    CoronaActivator.debugLog(Messages.IoService_interrupt);
                }

                if (this.session.isConnected() && isConnectDam()) {
                    out.println(Messages.IoService_connectionSuccess);
                    return this.session;
                }
                out.println(Messages.IoService_connectionFail);
            }

            /*  */
            HibernateException connectError = new HibernateException(
                    Messages.IoService_errorReconnectDatabaseFail + " " + ER_CODE_CONNECTION_FAILED); //$NON-NLS-1$
            IStatus status = new Status(IStatus.ERROR, IoActivator.PLUGIN_ID,
                    Messages.IoService_errorReconnectDatabaseFail, connectError);
            activator.getLog().log(status);
            try {
                throw connectError;
            } catch (HibernateException e1) {
                e1.printStackTrace();
            }
        }
        throw new RuntimeException(Messages.IoService_errorEndCoronaActivator);
    }

    return this.session;
}

From source file:com.tida_okinawa.corona.io.dam.hibernate.IoService.java

License:Open Source License

/**
 * /*w  w  w.  j ava 2  s  .  com*/
 * 
 * @param project
 * @return true/false
 */
@Override
protected Boolean addProjectDam(ICoronaProject project) {
    ProjectBean result = null;

    try {
        /* ?? */
        @SuppressWarnings("unchecked")
        List<ProjectBean> list = CommonCreateQuery.getProjectQuery(project.getName()).list();
        if (list != null && list.size() > 0) {
            result = list.get(0);
        }
        if (result != null) {
            project.setId(result.getProjectId());
            project.update();
            return true;
        }

        try {
            // ?
            ProjectBean projectBean = new ProjectBean();
            projectBean.setProjectName(project.getName());

            /*  */
            this.session.beginTransaction();
            this.session.save(projectBean);
            this.session.flush();
            /*  */
            this.session.getTransaction().commit();

            // INSERT?ID???
            project.setId(projectBean.getProjectId());

            /* ? */
            updateDictionarys();

            // JUMAN?????
            List<ICoronaDic> dics = getDictionarys(IUserDic.class);
            for (ICoronaDic dic : dics) {
                if (((IUserDic) dic).getDicType().getIntValue() == DicType.JUMAN.getIntValue()) {
                    project.addDictionary(dic);
                }
            }

            return true;
        } catch (HibernateException ee) {
            ee.printStackTrace();
            throw new HibernateException(Messages.IoService_errorSetProjectInfo);
        }
    } catch (HibernateException e) {
        e.printStackTrace();
        return false;
    } finally {
        if (this.session.getTransaction().isActive()) {
            this.session.getTransaction().rollback();
        }
    }
}