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:de.decidr.model.filters.PaginatingCriteria.java

License:Apache License

/**
 * Returns the row count for this Criteria.
 * //from   w  ww. j  av a  2  s .  c o  m
 * @return the row count
 * @throws HibernateException
 *             iff the row count cannot be retrieved.
 */
public Integer rowCount() throws HibernateException {
    Number result = (Number) clone.uniqueResult();

    if (result != null) {
        return result.intValue();
    } else {
        throw new HibernateException("The row count query did not return a number.");
    }
}

From source file:de.escidoc.core.st.business.persistence.hibernate.HibernateStagingFileDao.java

License:Open Source License

/**
 * See Interface for functional description.
 *///from  w  w w.  j a v a2s  .c o m
@Override
public StagingFile findStagingFile(final String token) throws SqlDatabaseSystemException {

    try {
        final DetachedCriteria criteria = DetachedCriteria.forClass(StagingFile.class);
        criteria.add(Restrictions.eq("token", token));
        List<StagingFile> result = getHibernateTemplate().findByCriteria(criteria);
        if (result == null || result.isEmpty()) {
            return null;
        } else if (result.size() > 1) {
            throw new HibernateException("result contains more than one record");
        } else {
            return result.get(0);
        }
    } catch (final DataAccessResourceFailureException e) {
        throw new SqlDatabaseSystemException(e);
    } catch (final HibernateException e) {
        //noinspection ThrowableResultOfMethodCallIgnored
        throw new SqlDatabaseSystemException(convertHibernateAccessException(e)); // Ignore FindBugs
    } catch (final IllegalStateException e) {
        throw new SqlDatabaseSystemException(e);
    }

}

From source file:de.fhdo.terminologie.db.UseIdOrGenerate.java

License:Apache License

@Override
public Serializable generate(SessionImplementor session, Object obj) throws HibernateException {
    if (obj == null)
        throw new HibernateException(new NullPointerException());

    if (obj instanceof TermUser) {
        if ((((TermUser) obj).getId()) == null) {
            Serializable id = super.generate(session, obj);
            return id;
        } else {//from w w w .ja  v  a 2s .c o m
            return ((TermUser) obj).getId();
        }
    } else if (obj instanceof Collaborationuser) {
        if ((((Collaborationuser) obj).getId()) == null) {
            Serializable id = super.generate(session, obj);
            return id;
        } else {
            return ((Collaborationuser) obj).getId();
        }
    }

    Serializable id = super.generate(session, obj);
    return id;
}

From source file:de.iew.stagediver.fx.database.hibernate.HBConnectionProvider.java

License:Apache License

protected void verify() {
    final boolean databaseOk = StringUtils.isNotBlank(this.database);

    if (!(databaseOk)) {
        throw new HibernateException("Configuration error: database name is required");
    }/*from www  .j  av  a  2s .  com*/
}

From source file:de.innovationgate.webgate.api.jdbc.pool.DBCPConnectionProvider.java

License:Apache License

public void configure(Map propsMap) throws HibernateException {
    try {//from  w ww  .j  a  va2  s.c o m
        log.debug("Configure DBCPConnectionProvider");
        Properties props = new Properties();
        props.putAll(propsMap);

        String jdbcUrl = (String) props.getProperty(Environment.URL);

        // DBCP properties used to create the BasicDataSource
        Properties dbcpProperties = new Properties();

        // DriverClass & url
        String jdbcDriverClass = props.getProperty(Environment.DRIVER);

        // Try to determine driver by jdbc-URL
        if (jdbcDriverClass == null) {
            Driver driver = DriverManager.getDriver(jdbcUrl);
            if (driver != null) {
                jdbcDriverClass = driver.getClass().getName();
            } else {
                throw new HibernateException("Driver class not available");
            }
        }

        dbcpProperties.put("driverClassName", jdbcDriverClass);
        dbcpProperties.put("url", jdbcUrl);

        // Username / password
        String username = props.getProperty(Environment.USER);
        if (username != null) {
            dbcpProperties.put("username", username);
        }

        String password = props.getProperty(Environment.PASS);
        if (password != null) {
            dbcpProperties.put("password", password);
        }

        // Isolation level
        String isolationLevel = props.getProperty(Environment.ISOLATION);
        if ((isolationLevel != null) && (isolationLevel.trim().length() > 0)) {
            dbcpProperties.put("defaultTransactionIsolation", isolationLevel);
        }

        // Turn off autocommit (unless autocommit property is set) 
        String autocommit = props.getProperty(AUTOCOMMIT);
        if ((autocommit != null) && (autocommit.trim().length() > 0)) {
            dbcpProperties.put("defaultAutoCommit", autocommit);
        } else {
            dbcpProperties.put("defaultAutoCommit", String.valueOf(Boolean.FALSE));
        }

        // Pool size
        String poolSize = props.getProperty(Environment.POOL_SIZE);
        if ((poolSize != null) && (poolSize.trim().length() > 0) && (Integer.parseInt(poolSize) > 0)) {
            dbcpProperties.put("maxActive", poolSize);
        }

        // Copy all "driver" properties into "connectionProperties"
        Properties driverProps = ConnectionProviderInitiator.getConnectionProperties(props);
        if (driverProps.size() > 0) {
            StringBuffer connectionProperties = new StringBuffer();
            for (Iterator iter = driverProps.keySet().iterator(); iter.hasNext();) {
                String key = (String) iter.next();
                String value = driverProps.getProperty(key);
                connectionProperties.append(key).append('=').append(value);
                if (iter.hasNext()) {
                    connectionProperties.append(';');
                }
            }
            dbcpProperties.put("connectionProperties", connectionProperties.toString());
        }

        // Copy all DBCP properties removing the prefix
        for (Iterator iter = props.keySet().iterator(); iter.hasNext();) {
            String key = String.valueOf(iter.next());
            if (key.startsWith(PREFIX)) {
                String property = key.substring(PREFIX.length());
                String value = props.getProperty(key);
                dbcpProperties.put(property, value);
            }
        }

        // Backward-compatibility
        if (props.getProperty(DBCP_PS_MAXACTIVE) != null) {
            dbcpProperties.put("poolPreparedStatements", String.valueOf(Boolean.TRUE));
            dbcpProperties.put("maxOpenPreparedStatements", props.getProperty(DBCP_PS_MAXACTIVE));
        }
        if (props.getProperty(DBCP_MAXACTIVE) != null) {
            dbcpProperties.put("maxTotal", props.getProperty(DBCP_MAXACTIVE));
        }
        if (props.getProperty(DBCP_MAXWAIT) != null) {
            dbcpProperties.put("maxWaitMillis", props.getProperty(DBCP_MAXWAIT));
        }

        // Some debug info
        if (log.isDebugEnabled()) {
            log.debug("Creating a DBCP BasicDataSource with the following DBCP factory properties:");
            StringWriter sw = new StringWriter();
            dbcpProperties.list(new PrintWriter(sw, true));
            log.debug(sw.toString());
        }

        String dbKey = (String) props.get("hibernate.dbcp.dbkey");
        String databaseServerId = (String) props.get("hibernate.dbcp.dbserver.id");

        // Enable DBCP2 JMX monitoring information
        if (dbKey != null) {
            dbcpProperties.put("jmxName",
                    JMX_DBCP2_DBPOOLS_ADDRESS + ",pool=" + JmxManager.normalizeJmxKey(dbKey));
        } else if (databaseServerId != null) {
            String entityTitle = props.getProperty("hibernate.dbcp.dbserver.title");
            dbcpProperties.put("jmxName",
                    JMX_DBCP2_SERVERPOOLS_ADDRESS + ",pool=" + JmxManager.normalizeJmxKey(entityTitle));
        }

        // Let the factory create the pool
        _ds = BasicDataSourceFactory.createDataSource(dbcpProperties);
        _ds.setLogExpiredConnections(false);

        // The BasicDataSource has lazy initialization
        // borrowing a connection will start the DataSource
        // and make sure it is configured correctly.
        Connection conn = _ds.getConnection();
        conn.close();

        // Create Legacy JMX monitoring information, provided by WGA
        if ("true".equals(props.getProperty("hibernate.dbcp.legacyJMX"))) {
            try {
                if (dbKey != null) {
                    _entityKey = dbKey;
                    _entityTitle = dbKey;
                    _jmxManager = new JmxManager(new DBCPPoolInformation(this),
                            new ObjectName(JMX_DBPOOLS_ADDRESS + ",pool=" + JmxManager.normalizeJmxKey(dbKey)));
                } else if (databaseServerId != null) {
                    _server = true;
                    _entityKey = databaseServerId;
                    _entityTitle = (String) props.get("hibernate.dbcp.dbserver.title");
                    _jmxManager = new JmxManager(new DBCPPoolInformation(this), new ObjectName(
                            JMX_SERVERPOOLS_ADDRESS + ",pool=" + JmxManager.normalizeJmxKey(_entityTitle)));
                }
            } catch (Throwable e) {
                log.error("Error enabling JMX metrics for connection pool", e);
            }
        }

    } catch (Exception e) {
        String message = "Could not create a DBCP pool";
        if (_ds != null) {
            try {
                _ds.close();
            } catch (Exception e2) {
                // ignore
            }
            _ds = null;
        }
        throw new HibernateException(message, e);
    }
    log.debug("Configure DBCPConnectionProvider complete");

}

From source file:de.rs.hibernate.LocalizedStringType.java

License:Open Source License

@Override
public Object getPropertyValue(Object component, int propertyIndex) throws HibernateException {
    if (component == null) {
        return null;
    }//from   www.  j a  v  a  2 s . co  m

    final LocalizedString string = (LocalizedString) component;
    switch (propertyIndex) {
    case VALUE: {
        return string.getValue();
    }
    case LOCALE: {
        return string.getLocale();
    }
    default: {
        throw new HibernateException("Invalid property index [" + propertyIndex + "]");
    }
    }
}

From source file:de.rs.hibernate.LocalizedStringType.java

License:Open Source License

@Override
public void setPropertyValue(Object component, int propertyIndex, Object value) throws HibernateException {
    if (component == null) {
        return;//from  www .  j  a v  a 2  s  . c om
    }

    final LocalizedString string = (LocalizedString) component;
    switch (propertyIndex) {
    case VALUE: {
        string.setValue((String) value);
        break;
    }
    case LOCALE: {
        string.setLocale((Locale) value);
        break;
    }
    default: {
        throw new HibernateException("Invalid property index [" + propertyIndex + "]");
    }
    }
}

From source file:de.rs.hibernate.MoneyType.java

License:Open Source License

@Override
public Object getPropertyValue(Object component, int propertyIndex) throws HibernateException {
    if (component == null) {
        return null;
    }/*w  w w.ja  v  a2  s . c o  m*/

    final Money money = (Money) component;
    switch (propertyIndex) {
    case 0: {
        return money.getAmount();
    }
    case 1: {
        return money.getCurrency();
    }
    default: {
        throw new HibernateException("Invalid property index [" + propertyIndex + "]");
    }
    }
}

From source file:de.rs.hibernate.MoneyType.java

License:Open Source License

@Override
public void setPropertyValue(Object component, int propertyIndex, Object value) throws HibernateException {
    if (component == null) {
        return;//from  w  ww. j  a va 2s . co  m
    }

    final Money money = (Money) component;
    switch (propertyIndex) {
    case 0: {
        money.setAmount((BigDecimal) value);
        break;
    }
    case 1: {
        money.setCurrency((Currency) value);
        break;
    }
    default: {
        throw new HibernateException("Invalid property index [" + propertyIndex + "]");
    }
    }
}

From source file:de.tudresden.inf.rn.mobilis.server.HibernateUtil.java

License:Apache License

public static void beginTransaction() throws HibernateException {
    try {//from  w ww . ja  v  a  2 s .  c o m
        Transaction t = transactionThreadLocal.get();
        if (t == null) {
            t = getSession().beginTransaction();
            transactionThreadLocal.set(t);
        }
    } catch (Exception e) {
        throw new HibernateException(e);
    }
}