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(String message, Throwable cause) 

Source Link

Document

Constructs a HibernateException using the given message and underlying cause.

Usage

From source file:org.ambraproject.hibernate.URIType.java

License:Apache License

public int hashCode(java.lang.Object o) throws org.hibernate.HibernateException {
    if (o == null) {
        return 0;
    }/*from w  w w.  j  a v  a 2  s  . c  o m*/

    if (o instanceof String) {
        try {
            return (new URI((String) o)).hashCode();
        } catch (URISyntaxException ex) {
            throw new HibernateException(ex.getMessage(), ex);
        }
    } else {
        return ((URI) o).hashCode();
    }
}

From source file:org.ambraproject.hibernate.URIType.java

License:Apache License

public Object nullSafeGet(java.sql.ResultSet resultSet, java.lang.String[] names, java.lang.Object o)
        throws org.hibernate.HibernateException, java.sql.SQLException {
    URI uri = null;/*from   w  w  w. j a va  2 s  .co  m*/

    String strURI = resultSet.getString(names[0]);

    if (!resultSet.wasNull()) {
        try {
            return new URI(strURI);
        } catch (URISyntaxException ex) {
            throw new HibernateException(ex.getMessage(), ex);
        }
    }

    return uri;
}

From source file:org.ambraproject.hibernate.URIType.java

License:Apache License

public void nullSafeSet(PreparedStatement statement, java.lang.Object value, int index)
        throws org.hibernate.HibernateException, java.sql.SQLException {
    if (value == null) {
        statement.setString(index, null);
    } else {/*w w  w.j  a va2s .  c  o  m*/
        if (value instanceof String) {
            try {
                //Test syntax
                new URI((String) value);
                statement.setString(index, (String) value);
            } catch (URISyntaxException ex) {
                throw new HibernateException(ex.getMessage(), ex);
            }
        } else {
            statement.setString(index, ((URI) value).toString());
        }
    }
}

From source file:org.ambraproject.migration.BootstrapMigratorServiceImpl.java

License:Apache License

private void migrate210() {
    log.info("Migration from 210 starting");
    //First create version table and add one row

    final boolean isSnapshot = this.isSnapshot;

    hibernateTemplate.execute(new HibernateCallback() {
        @Override/*from  w  ww  .  j  ava 2 s. com*/
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            log.debug("Creating new tables.");

            execSQLScript(session, "migrate_ambra_2_2_0_part1.sql");

            Version v = new Version();
            v.setName("Ambra 2.20");
            v.setVersion(220);
            v.setUpdateInProcess(true);
            session.save(v);

            log.debug("Tables created, now migrating data and removing old tables.");

            //We execute step #2 in a slightly different way as this file has SQL delimited in a different fashion
            //since it creates a trigger
            String sqlScript = "";
            try {
                log.debug("migrate_ambra_2_2_0_part2.sql started");
                sqlScript = getSQLScript("migrate_ambra_2_2_0_part2.sql");
                log.debug("migrate_ambra_2_2_0_part2.sql completed");
            } catch (IOException ex) {
                throw new HibernateException(ex.getMessage(), ex);
            }

            session.createSQLQuery(sqlScript).executeUpdate();

            execSQLScript(session, "migrate_ambra_2_2_0_part3.sql");

            //step 4 also creates a trigger, so we need to execute it the same as with step 2
            try {
                log.debug("migrate_ambra_2_2_0_part4.sql started");
                sqlScript = getSQLScript("migrate_ambra_2_2_0_part4.sql");
                log.debug("migrate_ambra_2_2_0_part4.sql completed");
            } catch (IOException ex) {
                throw new HibernateException(ex.getMessage(), ex);
            }
            session.createSQLQuery(sqlScript).executeUpdate();

            execSQLScript(session, "migrate_ambra_2_2_0_part5.sql");
            execSQLScript(session, "migrate_ambra_2_2_0_part6.sql");

            v.setUpdateInProcess(false);
            session.update(v);

            return null;
        }
    });

    log.info("Migration from 210 complete");
}

From source file:org.ambraproject.migration.BootstrapMigratorServiceImpl.java

License:Apache License

private void execSQLScript(Session session, String sqlScript) throws SQLException, HibernateException {
    log.debug("{} started.", sqlScript);
    String sqlStatements[] = { "" };

    Transaction transaction = session.getTransaction();

    try {/*ww w  .java2s  .  c o  m*/
        sqlStatements = getSQLCommands(sqlScript);
    } catch (IOException ex) {
        throw new HibernateException(ex.getMessage(), ex);
    }

    transaction.begin();

    for (String sqlStatement : sqlStatements) {
        log.debug("Running: {}", sqlStatement);
        session.createSQLQuery(sqlStatement).executeUpdate();
    }

    transaction.commit();
    log.debug("{} completed.", sqlScript);
}

From source file:org.ambraproject.service.migration.BootstrapMigratorServiceImpl.java

License:Apache License

static void execSQLScript(Session session, String sqlScript) throws SQLException, HibernateException {
    log.debug("{} started.", sqlScript);
    List<String> sqlStatements;

    Transaction transaction = session.getTransaction();

    try {//from   ww  w . jav a2s .co m
        sqlStatements = getSQLCommands(sqlScript);
    } catch (IOException ex) {
        throw new HibernateException(ex.getMessage(), ex);
    }

    transaction.begin();

    for (String sqlStatement : sqlStatements) {
        log.debug("Running: {}", sqlStatement);
        session.createSQLQuery(sqlStatement).executeUpdate();
    }

    transaction.commit();
    log.debug("{} completed.", sqlScript);
}

From source file:org.andromda.persistence.hibernate.usertypes.HibernateByteBlobType.java

/**
 * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
 */// w  w  w.jav  a  2 s .  c o  m
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
        throws HibernateException, SQLException {
    final Object object;

    final InputStream inputStream = resultSet.getBinaryStream(names[0]);
    if (inputStream == null) {
        object = null;
    } else {
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        try {
            final byte[] buffer = new byte[65536];
            int read = -1;

            while ((read = inputStream.read(buffer)) > -1) {
                outputStream.write(buffer, 0, read);
            }
            outputStream.close();
        } catch (IOException exception) {
            throw new HibernateException("Unable to read blob " + names[0], exception);
        }
        object = outputStream.toByteArray();
    }

    return object;
}

From source file:org.andromda.persistence.hibernate.usertypes.HibernateEnumType.java

/**
 * @see org.hibernate.usertype.ParameterizedType#setParameterValues(java.util.Properties)
 *///from  w  w  w  .  j  a  v  a  2 s.c o m
@SuppressWarnings("unchecked")
public void setParameterValues(Properties parameters) {
    final String enumClassName = parameters.getProperty("enumClassName");
    try {
        //noinspection unchecked
        this.enumClass = (Class<Enum>) Class.forName(enumClassName);
    } catch (ClassNotFoundException cnfe) {
        throw new HibernateException("Enum class not found", cnfe);
    }
}

From source file:org.andromda.persistence.hibernate.usertypes.HibernateStringClobType.java

/**
 * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
 *///from  www  . j av a  2s . c o  m
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
        throws HibernateException, SQLException {
    final StringBuffer buffer = new StringBuffer();
    try {
        //First we get the stream
        Reader inputStream = resultSet.getCharacterStream(names[0]);
        if (inputStream == null) {
            return null;
        }
        char[] buf = new char[1024];
        int read = -1;

        while ((read = inputStream.read(buf)) > 0) {
            buffer.append(new String(buf, 0, read));
        }
        inputStream.close();
    } catch (IOException exception) {
        throw new HibernateException("Unable to read from resultset", exception);
    }
    return buffer.toString();
}

From source file:org.apache.ode.daohib.JotmTransactionFactory.java

License:Open Source License

/**
 * Given the lot of Hibernate configuration properties, resolve appropriate
 * reference to JNDI {@link InitialContext}.
 * <p/>//from ww w .j  a  va  2s.  c om
 * In general, the properties in which we are interested here all begin with
 * <tt>hibernate.jndi</tt>.  Especially important depending on your
 * environment are {@link Environment#JNDI_URL hibernate.jndi.url} and
 *  {@link Environment#JNDI_CLASS hibernate.jndi.class}
 *
 * @param properties The Hibernate config properties.
 * @return The resolved InitialContext.
 */
protected final InitialContext resolveInitialContext(Properties properties) {
    try {
        return NamingHelper.getInitialContext(properties);
    } catch (NamingException ne) {
        throw new HibernateException("Could not obtain initial context", ne);
    }
}