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.netradius.hibernate.support.SaltedMessageDigestUserType.java

License:Apache License

/**
 * {@inheritDoc}/*from w  ww. j a v a  2 s.com*/
 */
@Override
public void setParameterValues(Properties properties) {
    if (properties != null) {
        super.setParameterValues(properties);
        String tmp = properties.getProperty(PARAM_SALT_SIZE);
        if (tmp != null) {
            try {
                saltSize = Integer.parseInt(tmp);
            } catch (NumberFormatException x) {
                throw new HibernateException(
                        "Invalid salt size " + saltSize + ". Salt size must be an integer.");
            }
        } else {
            saltSize = DEFAULT_SALT_SIZE;
        }
    }
}

From source file:com.netspective.medigy.model.data.EntitySeedDataPopulator.java

License:Open Source License

protected void populateEntity(final Session session, final Class entityClass, final String[] propertyList,
        final Object[][] data) throws HibernateException {
    try {//w  ww. j ava 2s .  co m
        final Hashtable pdsByName = new Hashtable();
        final BeanInfo beanInfo = Introspector.getBeanInfo(entityClass);
        final PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < descriptors.length; i++) {
            final PropertyDescriptor descriptor = descriptors[i];
            if (descriptor.getWriteMethod() != null)
                pdsByName.put(descriptor.getName(), descriptor.getWriteMethod());
        }

        for (int i = 0; i < data.length; i++) {
            final Object entityObject = entityClass.newInstance();
            for (int j = 0; j < propertyList.length; j++) {
                final Method setter = (Method) pdsByName.get(propertyList[j]);
                if (setter != null)
                    setter.invoke(entityObject, new Object[] { data[i][j] });
            }
            session.save(entityObject);
        }
    } catch (Exception e) {
        log.error(e);
        throw new HibernateException(e);
    }
}

From source file:com.netspective.medigy.util.HibernateConfiguration.java

License:Open Source License

public void registerReferenceEntitiesAndCaches() {
    final Iterator classMappings = getClassMappings();
    while (classMappings.hasNext()) {
        Class aClass = ((PersistentClass) classMappings.next()).getMappedClass(); //(Class) classMappings.next();
        if (ReferenceEntity.class.isAssignableFrom(aClass)) {
            boolean foundCache = false;
            for (final Class ic : aClass.getClasses()) {
                if (CachedReferenceEntity.class.isAssignableFrom(ic)) {
                    if (ic.isEnum()) {
                        referenceEntitiesAndCachesMap.put(aClass, ic);
                        foundCache = true;
                    } else
                        throw new HibernateException(ic + " must be an enum since " + aClass + " is a "
                                + ReferenceEntity.class.getName());

                    break;
                }/*from  w w w  .j a  v  a2  s .co  m*/

            }

            if (!foundCache)
                throw new HibernateException(aClass
                        + " is marked as a ReferenceEntity but does not contain a ReferenceEntityCache enum.");

            // TODO: find out how to ensure the new mapping for reference type is immutable and read only
            // final PersistentClass pClass = getClassMapping(aClass.getLabel());
        } else if (CustomReferenceEntity.class.isAssignableFrom(aClass)) {
            for (final Class ic : aClass.getClasses()) {
                if (CachedCustomReferenceEntity.class.isAssignableFrom(ic)) {
                    if (ic.isEnum()) {
                        customReferenceEntitiesAndCachesMap.put(aClass, ic);
                    } else
                        throw new HibernateException(ic + " must be an enum since " + aClass + " is a "
                                + CachedCustomReferenceEntity.class.getName());

                    break;
                }
            }
            // if no cache is found, its ok since these are custom
        }
    }
}

From source file:com.nextep.datadesigner.vcs.services.VCSFiles.java

License:Open Source License

/**
 * Writed the specified file to the given repository file with Oracle-specific Blob support.
 * //from   w ww .  ja v a 2 s. c  o m
 * @param conn Oracle connection
 * @param file repository file which must have been created
 * @param localFile local file to dump into the repository file
 * @throws SQLException when any database connection problems occurs
 */
private void writeOracleBlob(Connection conn, IRepositoryFile file, File localFile) throws SQLException {
    PreparedStatement stmt = conn
            .prepareStatement("update REP_FILES set FILE_CONTENT=?, FILESIZE=? where FILE_ID=?"); //$NON-NLS-1$
    // DatabaseMetaData md = conn.getMetaData();
    OutputStream os = null;
    FileInputStream is = null;
    BLOB tempBlob = null;
    long size = 0;
    try {
        try {

            // Get the oracle connection class for checking
            Class<?> oracleConnectionClass = Class.forName("oracle.jdbc.OracleConnection"); //$NON-NLS-1$

            // Make sure connection object is right type
            if (!oracleConnectionClass.isAssignableFrom(conn.getClass())) {
                throw new HibernateException(VCSMessages.getString("files.invalidOracleConnection") //$NON-NLS-1$
                        + VCSMessages.getString("files.invalidOracleConnection.2") + conn.getClass().getName()); //$NON-NLS-1$
            }
            // Create our temp BLOB
            tempBlob = BLOB.createTemporary(conn, true, BLOB.DURATION_SESSION);

            tempBlob.open(BLOB.MODE_READWRITE);
            os = tempBlob.getBinaryOutputStream();
            is = new FileInputStream(localFile);
            // Large 10K buffer for efficient read
            byte[] buffer = new byte[10240];
            int bytesRead = 0;

            while ((bytesRead = is.read(buffer)) >= 0) {
                os.write(buffer, 0, bytesRead);
                size += bytesRead;
            }
        } catch (ClassNotFoundException e) {
            // could not find the class with reflection
            throw new ErrorException(VCSMessages.getString("files.classUnresolved") + e.getMessage()); //$NON-NLS-1$
        } catch (FileNotFoundException e) {
            throw new ErrorException(VCSMessages.getString("files.fileUnresolved")); //$NON-NLS-1$
        } catch (IOException e) {
            throw new ErrorException(VCSMessages.getString("files.readProblem"), e); //$NON-NLS-1$
        } finally {
            safeClose(os);
            safeClose(is);
            if (tempBlob != null) {
                tempBlob.close();
            }
        }
        stmt.setBlob(1, tempBlob);
        stmt.setLong(2, size);
        stmt.setLong(3, file.getUID().rawId());
        stmt.execute();
    } finally {
        stmt.close();
        file.setFileSizeKB(size / 1024);
    }

}

From source file:com.nextep.designer.core.dao.types.ClobStringType.java

License:Open Source License

/**
 * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement,
 *      java.lang.Object, int)/*from www. j a v  a 2  s.  c o  m*/
 */
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
    DatabaseMetaData dbMetaData = st.getConnection().getMetaData();
    if (value == null) {
        st.setNull(index, sqlTypes()[0]);
    } else if (ORACLE_DRIVER_NAME.equals(dbMetaData.getDriverName())) {
        if ((dbMetaData.getDriverMajorVersion() >= ORACLE_DRIVER_MAJOR_VERSION)
                && (dbMetaData.getDriverMinorVersion() >= ORACLE_DRIVER_MINOR_VERSION)) {
            try {

                // Get the oracle connection class for checking
                Class oracleConnectionClass = Class.forName("oracle.jdbc.OracleConnection"); //$NON-NLS-1$

                Connection conn = st.getConnection();

                // Make sure connection object is right type
                // Create our CLOB
                CLOB tempClob = null;
                if (!oracleConnectionClass.isAssignableFrom(conn.getClass())) {
                    if (conn instanceof NewProxyConnection) {
                        tempClob = OracleUtils.createTemporaryCLOB(conn, true, CLOB.DURATION_SESSION);
                    } else {
                        throw new HibernateException(CoreMessages
                                .getString("clobStringType.hibernateException.connectionMustBeOracleConnection") //$NON-NLS-1$
                                + CoreMessages.getString("clobStringType.hibernateException.classIs") //$NON-NLS-1$
                                + conn.getClass().getName());
                    }
                } else {
                    tempClob = CLOB.createTemporary(conn, true, CLOB.DURATION_SESSION);
                }

                // call open(CLOB.MODE_READWRITE);
                tempClob.open(CLOB.MODE_READWRITE);

                // call the getCharacterOutpitStream method
                Writer tempClobWriter = (Writer) tempClob.getCharacterOutputStream(); // getCharacterOutputStreamMethod.invoke(
                // tempClob, null );

                // write the string to the clob
                tempClobWriter.write((String) value);
                tempClobWriter.flush();
                tempClobWriter.close();

                // get the close method
                tempClob.close();

                // add the clob to the statement
                st.setClob(index, tempClob);
            } catch (IOException e) {
                throw new HibernateException(e.getMessage());
            } catch (ClassNotFoundException e) {
                // could not find the class with reflection
                throw new HibernateException(
                        CoreMessages.getString("clobStringType.hibernateException.unableToFindClass") //$NON-NLS-1$
                                + "\n" + e.getMessage()); //$NON-NLS-1$
            }
        } else {
            throw new HibernateException(
                    CoreMessages.getString("clobStringType.hibernateException.noClobSupport")); //$NON-NLS-1$
        }
    } else {
        String str = (String) value;
        StringReader r = new StringReader(str);
        st.setCharacterStream(index, r, str.length());
    }
}

From source file:com.npower.dm.hibernate.entity.CarrierIdentifierGenerator.java

License:Open Source License

public Serializable generate(SessionImplementor session, Object entity) throws HibernateException {
    if (!(entity instanceof Carrier)) {
        throw new HibernateException("Entity not implements Carrier interface.");
    }/*from w  ww. j a  v  a2  s.co  m*/
    Carrier carrier = (Carrier) entity;
    if (StringUtils.isEmpty(carrier.getExternalID())) {
        throw new HibernateException("Could not generate a identifier, missing carrier externalID.");
    }
    StringBuffer buf = new StringBuffer(carrier.getExternalID().toLowerCase().trim());
    int id = (int) buf.toString().hashCode();
    // Keep > 0, unsigned number
    if (id < 0) {
        id = Integer.MAX_VALUE + id;
    }
    return new Long(id);
}

From source file:com.npower.dm.hibernate.entity.ManufacturerIdentifierGenerator.java

License:Open Source License

public Serializable generate(SessionImplementor session, Object entity) throws HibernateException {
    if (!(entity instanceof Manufacturer)) {
        throw new HibernateException("Entity not implements Manufacturer interface.");
    }//  w w w . ja v a  2 s.  c  o m
    Manufacturer manufacturer = (Manufacturer) entity;
    if (StringUtils.isEmpty(manufacturer.getExternalId())) {
        throw new HibernateException("Could not generate a identifier, missing manufacturer externalID.");
    }
    StringBuffer buf = new StringBuffer(manufacturer.getExternalId().toLowerCase().trim());
    int id = (int) buf.toString().hashCode();
    // Keep > 0, unsigned number
    if (id < 0) {
        id = Integer.MAX_VALUE + id;
    }
    return new Long(id);
}

From source file:com.npower.dm.hibernate.entity.ModelIdentifierGenerator.java

License:Open Source License

public Serializable generate(SessionImplementor session, Object entity) throws HibernateException {
    if (!(entity instanceof Model)) {
        throw new HibernateException("Entity not implements Model interface.");
    }//from  w  ww.  j a va  2 s. c o  m
    Model model = (Model) entity;
    if (model.getManufacturer() == null || StringUtils.isEmpty(model.getManufacturer().getExternalId())) {
        throw new HibernateException("Could not generate a identifier, missing manufacturer.");
    }
    if (StringUtils.isEmpty(model.getManufacturerModelId())) {
        throw new HibernateException("Could not generate a identifier, missing model externalID.");
    }

    StringBuffer buf = new StringBuffer(model.getManufacturer().getExternalId().toLowerCase().trim());
    buf.append("_");
    buf.append(model.getManufacturerModelId().toLowerCase().trim());
    int id = (int) buf.toString().hashCode();
    // Keep > 0, unsigned number
    if (id < 0) {
        id = Integer.MAX_VALUE + id;
    }
    return new Long(id);
}

From source file:com.npower.dm.hibernate.entity.ServiceProviderIdentifierGenerator.java

License:Open Source License

public Serializable generate(SessionImplementor session, Object entity) throws HibernateException {
    if (!(entity instanceof ServiceProvider)) {
        throw new HibernateException("Entity not implements ServiceProvider interface.");
    }/*  w w  w  .j  a v a2s  . c om*/
    ServiceProvider sp = (ServiceProvider) entity;
    if (StringUtils.isEmpty(sp.getExternalID())) {
        throw new HibernateException("Could not generate a identifier, missing ServiceProvider externalID.");
    }
    StringBuffer buf = new StringBuffer(sp.getExternalID().toLowerCase().trim());
    int id = (int) buf.toString().hashCode();
    // Keep > 0, unsigned number
    if (id < 0) {
        id = Integer.MAX_VALUE + id;
    }
    return new Long(id);
}

From source file:com.npower.dm.hibernate.FullTextSearchExpression.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");
    int index = genIndexId(columns);
    return " contains(" + columns[0] + ", ?, " + index + ")>0";
}